Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] Reload contents of QTableView from textfile

    General and Desktop
    4
    8
    4003
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      amban last edited by

      Hi Friends,
      I have a QTableView which is generated by reading a text file line-by-line. This text file is changing pretty often. I have a refresh button with which i want to reload the table i.e. again read the textfile and generate the table.....
      I followed previous discussions and added this :

      @QTimer *mytimer = new QTimer(this);

      //code to read a txt file line by line and generating a QTableView

      connect(mytimer, SIGNAL(timeout()),
               this, SLOT(update()) );
               guitimer->start(1000);
      

      @
      But this is not working. Pls suggest how to correct this ?

      Thanks

      1 Reply Last reply Reply Quote 0
      • G
        giesbert last edited by

        you are triggering the wrong timer...

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply Reply Quote 0
        • A
          amban last edited by

          I made an error in posting ....
          @QTimer *mytimer = new QTimer(this);

          //code to read a txt file line by line and generating a QTableView

          connect(mytimer, SIGNAL(timeout()),
                   this, SLOT(update()) );
                   mytimer->start(1000);
          

          @

          But it doesnt work.....

          1 Reply Last reply Reply Quote 0
          • G
            giesbert last edited by

            It is a bit less code to say what's wrong.

            what is this?

            has this a sot called update?

            do you get some debugger messages?

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply Reply Quote 0
            • A
              amban last edited by

              This is the code
              @
              PendingTab::PendingTab(QWidget *parent)
              :QWidget(parent)
              {

              QTimer *guitimer = new QTimer(this);
              QVBoxLayout *mainLayout = new QVBoxLayout;
              
              refreshbutton = new QPushButton(tr("Refresh"));
              findbutton = new QPushButton(tr("Find"));
              findstring = new QLineEdit;
              
              QHBoxLayout *findLayout = new QHBoxLayout;
              findLayout->addWidget(findstring);
              findLayout->addWidget(findbutton);
              
              mytable = new QTableView();
              mytablemodel = new QStandardItemModel();
              mytablemodel->setRowCount(0);
              
              int Max_num_of_Columns(10);
              int Max_Number_of_Lines(0);
              mytablemodel->setColumnCount(Max_num_of_Columns);
              
              QStringList header_list ;
              header_list << ......
              mytablemodel->setHorizontalHeaderLabels(header_list);
              
              QProcess *proc = new QProcess();
              
              QFile file&#40;"/path/to/a/text/file"&#41;;
              if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
                   qDebug() << "Error opening file";
              
              proc->setStandardOutputFile&#40;"/path/"&#41;;
              proc->start("/path/to/the/script/file");
              proc->waitForFinished();
              
              QTextStream InputDataFile&#40;&file&#41;;
              while (!InputDataFile.atEnd())
              {
              
                  QString line = InputDataFile.readLine();
              
                  line = line.trimmed();
                  QStringList fields = line.split(QRegExp("\\s+"));
              
                  if (fields.size() == Max_num_of_Columns)
                  {
                      for (int column=0; column< Max_num_of_Columns; column++)
                      {
                          QStandardItem *item = new QStandardItem(fields[column]);
                          mytablemodel->setItem(Max_Number_of_Lines, column, item);
              
                      }
                      Max_Number_of_Lines++ ;
                  }
              
              }
              
              mytable->setModel(mytablemodel);
              mytable->setAlternatingRowColors(true);
              mainLayout->addWidget(mytable);
              mainLayout->addWidget(refreshbutton);
              mainLayout->addLayout(findLayout);
              setLayout(mainLayout);
              
              file.close();
              
              connect(guitimer, SIGNAL(timeout()),
                       this, SLOT(update()) );
                       guitimer->start(1000);
              
              connect(refreshbutton, SIGNAL(clicked()),
                        this, SLOT(update()));
              
              connect(findbutton, SIGNAL(clicked()),
                      this, SLOT(findButtonClicked()));
              
              connect(findstring, SIGNAL(textChanged(QString)), // For changing the background of the searched key
                      this, SLOT(clearColoredRows()));
              
              mytable->setSortingEnabled(true);
              

              }
              @

              I am using the default update() slot which autopopulated for slots related to "this" i.e. my widget

              1 Reply Last reply Reply Quote 0
              • T
                tobias.hunger last edited by

                Maybe a "QFileSystemWatcher":http://doc.trolltech.com/4.7/qfilesystemwatcher.html would be better than doing timer-based polling of the file?

                1 Reply Last reply Reply Quote 0
                • W
                  wongk last edited by

                  Update causes the widget to repaint, which has nothing to do with the data in your model. Where are you updating the data in your model?

                  1 Reply Last reply Reply Quote 0
                  • A
                    amban last edited by

                    Thanks all for your help. I was unaware that repainting widget doesnt do anything to the data model. So i created a new slot loadTable() and moved the loading table data to the slot. So now i do

                    @connect(guitimer, SIGNAL(timeout()), this, SLOT(loadTable())); @

                    Thanks a lot. It works fine.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post