c++ - 读取文件并保存行和列的内容并将其保存为Qt c ++中的变量
问题描述
我通过从不同文件中提取信息并将每个文件上的数据保存到生成文件中的一行来生成一个文件。然后我将文件读入我的程序并在 QTableview 中显示。我创建了一个 QItemdelegate,它为我的 Qtableview 中的每一行添加了一个按钮。因此,在单击每个按钮时,我希望弹出一个 QDialog,它应该读取生成该行内容的匹配文件的内容。文件的名称,即变量的内容,是我希望程序在定位文件时使用的名称。
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton button;
QRect r = option.rect;//getting the rect of the cell
int x,y,w,h;
x = r.left() + r.width() - 70;//the X coordinate
y = r.top();//the Y coordinate
w = 70;//button width
h = 30;//button height
button.rect = QRect(x,y,w,h);
button.text = "View log";
button.state = QStyle::State_Enabled;
QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
}
bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
Q_UNUSED(model)
if( event->type() == QEvent::MouseButtonRelease )
{
QMouseEvent * e = (QMouseEvent *)event;
int clickX = e->x();
int clickY = e->y();
QRect r = option.rect;//getting the rect of the cell
int x,y,w,h;
x = r.left() + r.width() - 30;//the X coordinate
y = r.top();//the Y coordinate
w = 30;//button width
h = 30;//button height
if( clickX > x && clickX < x + w )
if( clickY > y && clickY < y + h )
{
int r = index.row(); // <---- row
int c = index.column(); // <---- column
QFile file("/home/uboho/monitor_test_module/logs/jobSummary");
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
// file line counter
QTextStream in(&file); // read to text stream
while (!in.atEnd()) {
// read one line from textstream(separated by "\n")
QString fileLine = in.readLine();
// parse the read line into separate pieces(tokens) with "," as the delimiter
QStringList lineToken = fileLine.split(",", QString::SkipEmptyParts);
// load parsed data to model accordingly
if (lineToken.size() == 6)
{
for (int row=r; int column=0; column< 2; column++;)
{
QStandardItem *item = new QStandardItem(lineToken[column]);
//mytablemodel3->setItem(Max_Number_of_Lines, column, item);
}
}
QDialog * d = new QDialog();
d->setGeometry(300,0,100,100);
d->show();
d->setWindowTitle("Log viewer");
}
}
}
return true;
}
}
解决方案
要提取您r-th
在行中并且在第一个逗号之前的文本,您只需遍历QTextStream
to 定位行,然后根据使用的分隔符分隔文本,在您的情况下是离开循环后的逗号。
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton button;
QRect r = option.rect;//getting the rect of the cell
button.rect = QRect(r.topRight() - QPoint(70, 0), QSize(70, 30));
button.text = "View log";
button.state = QStyle::State_Enabled;
QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
}
bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
Q_UNUSED(model)
if( event->type() == QEvent::MouseButtonRelease )
{
QMouseEvent * e = (QMouseEvent *)event;
QPoint p = e->pos();
QRect r = option.rect;//getting the rect of the cell
QRect re(r.topRight() - QPoint(30, 0), QSize(30, 30));
if(re.contains(p)){
int r = index.row(); // <---- row
int c = index.column(); // <---- column
QString filename; // text of the first column and r-row
QFile file("/home/uboho/monitor_test_module/logs/jobSummary");
int counter = 0;
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
QTextStream in(&file); // read to text stream
while (!in.atEnd()) {
QString fileLine = in.readLine();
if(counter == r){
filename = fileLine.split(",", QString::SkipEmptyParts).first();
break;
}
counter++;
}
}
QDialog * d = new QDialog();
d->setGeometry(300,0,100,100);
d->show();
d->setWindowTitle("Log viewer");
}
}
return true;
}
推荐阅读
- python-3.x - discord.py - 停止玩游戏后删除角色
- date - 如何在 PowerBI powerquery 中逐季更改?
- svg - 如何确定在 svg 中绘制的两个形状是否相互重叠?
- java - java调试器不能在可视化音频代码中工作
- javascript - 红点跟随鼠标并在图案周围移动
- python - matplotlib quiver plot 在使用 set_UVC 方法后表现不同
- google-apps-script - 如何让不和谐的机器人从谷歌电子表格中读出一个单元格
- python - Django单元测试:MultipleChoiceField依赖于setUp期间创建的对象
- r - 如何使用 PerformanceAnalytics 计算具有 NA 的等权重投资组合回报?
- javascript - 将 js 对象发送到 firebase 的最佳方法是什么?