自动刷新QTableWidget可能吗?(Refresh QTableWidget automatically possible?)
QTableWidget(在我的代码中,ipTable)项来自test_data.txt。 但test_data.txt文件每3秒更改一次。 我想自动刷新表..
如何自动更新QTableWidget ..?
这是我的代码。
#include "dialog.h" #include "ui_dialog.h" #include "addip.h" #include <QFile> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(onTimer())); timer->start(1000); setWindowTitle( "IP List" ); ui->ipTable->setColumnCount(3); refresh_table(); } Dialog::~Dialog() { delete ui; } QStringList Dialog::refresh_table() { int field; QFile file( "/home/yein/widget/test_data.txt" ); QStringList title; title << "IP" << "Protocol" << "state"; file.open( QIODevice::ReadOnly); QTextStream read(&file); ui->ipTable->clear(); ui->ipTable->setRowCount(0); ui->ipTable->setHorizontalHeaderLabels(title); while(!read.atEnd()) { QString tmp = read.readLine(); QStringList tmpList = tmp.split( "," ); ui->ipTable->insertRow(ui->ipTable->rowCount()); field = ui->ipTable->rowCount() - 1; ui->ipTable->setItem( field, IP, new QTableWidgetItem( tmpList[0] ) ); ui->ipTable->setItem( field, PROTOCOL, new QTableWidgetItem( tmpList[1] ) ); ui->ipTable->setItem( field, STATE, new QTableWidgetItem( tmpList[2] ) ); } file.close(); return table; } void Dialog::on_btnAdd_clicked() { QString protocol; QString IP; int res; addIP add(this); add.setWindowTitle( "Add IP" ); res = add.exec(); if( res == QDialog::Rejected ) return; IP = add.getIP(); protocol = add.getProtocol(); qDebug() << "IP :" << " " << IP; qDebug() << "Protocol : " << " " << protocol; write_on_file( IP,protocol ); } void Dialog::write_on_file( QString IP, QString protocol ) { QFile file( "/home/yein/widget/test_data.txt" ); file.open( QIODevice::Append ); data[0] = IP; data[1] = protocol; data[2] = "0"; // init state 0 QString _str = QString( "%1,%2,%3\n" ) .arg( data[0] ) .arg( data[1] ) .arg( data[2] ); qDebug() << _str << " "; QByteArray str; str.append(_str); file.write(str); file.close(); refresh_table(); } void Dialog::on_btnClose_clicked() { this->close(); } void Dialog::onTimer() { updateRStatusBar(); } void Dialog::updateRStatusBar() { QDateTime local(QDateTime::currentDateTime()); ui->clock->setText(local.toString()); }
QTableWidget(in my code, ipTable) Item come from test_data.txt. But test_data.txt file change in every 3seconds. I want refresh the table automatically..
How can I update QTableWidget automatically..?
This is my code.
#include "dialog.h" #include "ui_dialog.h" #include "addip.h" #include <QFile> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(onTimer())); timer->start(1000); setWindowTitle( "IP List" ); ui->ipTable->setColumnCount(3); refresh_table(); } Dialog::~Dialog() { delete ui; } QStringList Dialog::refresh_table() { int field; QFile file( "/home/yein/widget/test_data.txt" ); QStringList title; title << "IP" << "Protocol" << "state"; file.open( QIODevice::ReadOnly); QTextStream read(&file); ui->ipTable->clear(); ui->ipTable->setRowCount(0); ui->ipTable->setHorizontalHeaderLabels(title); while(!read.atEnd()) { QString tmp = read.readLine(); QStringList tmpList = tmp.split( "," ); ui->ipTable->insertRow(ui->ipTable->rowCount()); field = ui->ipTable->rowCount() - 1; ui->ipTable->setItem( field, IP, new QTableWidgetItem( tmpList[0] ) ); ui->ipTable->setItem( field, PROTOCOL, new QTableWidgetItem( tmpList[1] ) ); ui->ipTable->setItem( field, STATE, new QTableWidgetItem( tmpList[2] ) ); } file.close(); return table; } void Dialog::on_btnAdd_clicked() { QString protocol; QString IP; int res; addIP add(this); add.setWindowTitle( "Add IP" ); res = add.exec(); if( res == QDialog::Rejected ) return; IP = add.getIP(); protocol = add.getProtocol(); qDebug() << "IP :" << " " << IP; qDebug() << "Protocol : " << " " << protocol; write_on_file( IP,protocol ); } void Dialog::write_on_file( QString IP, QString protocol ) { QFile file( "/home/yein/widget/test_data.txt" ); file.open( QIODevice::Append ); data[0] = IP; data[1] = protocol; data[2] = "0"; // init state 0 QString _str = QString( "%1,%2,%3\n" ) .arg( data[0] ) .arg( data[1] ) .arg( data[2] ); qDebug() << _str << " "; QByteArray str; str.append(_str); file.write(str); file.close(); refresh_table(); } void Dialog::on_btnClose_clicked() { this->close(); } void Dialog::onTimer() { updateRStatusBar(); } void Dialog::updateRStatusBar() { QDateTime local(QDateTime::currentDateTime()); ui->clock->setText(local.toString()); }
原文:https://stackoverflow.com/questions/36643037
最满意答案
'\f'
是一个特殊字符(参见转义序列表 )。 在使用硬编码路径时,您应习惯使用r
(原始字符串):
os.startfile(r"C:\finished.py")
'\f'
is a special character (see Table of escape sequences). You should make it a habit to user
(raw strings) when working with hard-coded paths:
os.startfile(r"C:\finished.py")
相关问答
更多-
干得好, string fileToSelect = @"C:\temp.img"; string args = string.Format("/Select, \"{0}\"", fileToSelect); ProcessStartInfo pfi = new ProcessStartInfo("Explorer.exe", args); System.Diagnostics.Process.Start(pfi); 注意:在{0}参数前后添加“ \"可以使fileToSelect字符串包含空格(即“ ...
-
'\f'是一个特殊字符(参见转义序列表 )。 在使用硬编码路径时,您应习惯使用r (原始字符串): os.startfile(r"C:\finished.py") '\f' is a special character (see Table of escape sequences). You should make it a habit to use r (raw strings) when working with hard-coded paths: os.startfile(r"C:\finished. ...
-
fopen没有打开文件(fopen not opening File)[2023-09-10]
这是错的 char nameFile[strlen(filename)]; 它应该是 char nameFile[1 + strlen(filename)]; 因为strlen()不包含终止的nul字节,所以你的代码仍然非常危险,因为filename可能是NULL ,在这种情况下,因为你从未检查过那么在调用strlen()时会发生未定义的行为。 这个 source[++newLen] = '\0'; /* Just to be safe. */ 证明你知道字符串最后需要'\0' 。 所以你分配的内存少 ... -
用于生成时间范围使用pandas : import pandas as pd my_ranges = pd.date_range('2015-07-19', '2015-07-20', freq='5S') 这个例子会输出: In []: my_ranges Out[]: DatetimeIndex(['2015-07-19 00:00:00', '2015-07-19 00:00:05', '2015-07-19 00:00:10', '2015-07-19 00:00:15' ...
-
你在中途,FileOk事件就是你想要使用的。 您缺少的是将e.Cancel属性设置为true。 这使对话框保持打开状态,避免您不得不一遍又一遍地显示它。 喜欢这个: OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Jpeg files, PDF files, Word files|*.jpg;*.pdf;*.doc;*.docx"; dialog.FileOk += delegate ...
-
在Python中打开.txt文件(Opening a .txt file in Python)[2023-07-01]
def get_my_string(): """Returns the file inputFn""" inputFn = "/home/Documents/text.txt" try: with open(inputFn) as inputFileHandle: return inputFileHandle.read() except IOError: sys.stderr.write( "[myScript] ... -
尝试使用run而不是call,你的路径中有一个拼写错误。 subprocess.run(['C:\\Users\\Edvin\\AppData\\Local\\Programs\\Python\\Python35-32\\python.exe', 'C:\\Users\\Edvin\\Desktop\\test.py']) Try to use run instead of call, and you had a typo in your path. subprocess.run(['C:\\Users\ ...
-
你必须将你的inpath转换为unicode,如下所示: inpath = sys.argv[1] inpath = inpath.decode("UTF-8") filein = open(inpath, "rb") 我猜你使用的是Python 2.6,因为在Python 3中,默认情况下所有字符串都是unicode,所以这个问题不会发生。 You have to convert your inpath to unicode, like this: inpath = sys.argv[1] inpath ...
-
在python中打开JSON文件(Opening a JSON file in python)[2022-02-16]
请注意,存储在jason.json中的数据是dict而不是列表。 所以,如果你想要一个元组列表,你可以做类似的事情 with open('jason.json') as f: data = list(json.load(f).items()) print(data) 产量 [('E(3,5)', 'Persian rhythm'), ('E(3,4)', 'It is the archetypal pattern of the Cumbia from Colombia'), ('E(2,3)', ... -
你的文件名字符串中需要双反斜杠。 correctAnswers.open("C:\\CorrectAnswers"); studentAnswers.open("C:\\StudentAnswers"); You'll need double backslashes in your filename strings. correctAnswers.open("C:\\CorrectAnswers"); studentAnswers.open("C:\\StudentAnswers");