Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Reload contents of QTableView from textfile
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Reload contents of QTableView from textfile

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 4.5k Views 1 Watching
  • 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 Offline
    A Offline
    amban
    wrote on last edited by
    #1

    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
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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
      0
      • A Offline
        A Offline
        amban
        wrote on last edited by
        #3

        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
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          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
          0
          • A Offline
            A Offline
            amban
            wrote on last edited by
            #5

            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
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on last edited by
              #6

              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
              0
              • W Offline
                W Offline
                wongk
                wrote on last edited by
                #7

                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
                0
                • A Offline
                  A Offline
                  amban
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved