读取文件并保存行和列的内容并将其保存为Qt c++中的变量

read a file and save the content of a row and column and save it as a variable in 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 来定位行,然后根据使用的分隔文本分隔符,在你的例子中是你离开循环后的逗号。

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;
 }