[SOLVED] Reload contents of QTableView from textfile
-
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
-
you are triggering the wrong timer...
-
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.....
-
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?
-
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("/path/to/a/text/file"); if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) qDebug() << "Error opening file"; proc->setStandardOutputFile("/path/"); proc->start("/path/to/the/script/file"); proc->waitForFinished(); QTextStream InputDataFile(&file); 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
-
Maybe a "QFileSystemWatcher":http://doc.trolltech.com/4.7/qfilesystemwatcher.html would be better than doing timer-based polling of the file?
-
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?
-
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.