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

时间:2018-04-30 14:53:05

标签: c++ qt qt5 qfile

我通过从diffeent文件中提取信息并将每个文件上的数据保存到生成的文件中的一行来生成文件。然后我将文件读入我的程序并在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;
 }

 }

1 个答案:

答案 0 :(得分:0)

要提取您在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;
 }