我想更新我的QTableWidget,但我不能。
TableWidget数据来自文件(test_data.txt)。
当我按下添加按钮时,添加" ip&协议"然后数据追加test_data.txt 但是我的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" );
QStringList title;
title << "IP" << "Protocol" << "state";
ui->ipTable->setColumnCount(3);
ui->ipTable->setHorizontalHeaderLabels(title);
refresh_table();
}
Dialog::~Dialog()
{
delete ui;
}
QStringList Dialog::refresh_table()
{
int field;
QFile file( "/home/yein/widget/test_data.txt" );
file.open( QIODevice::ReadOnly);
QTextStream read(&file);
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();
}
void Dialog::on_btnClose_clicked()
{
this->close();
}
void Dialog::onTimer()
{
updateRStatusBar();
}
void Dialog::updateRStatusBar()
{
QDateTime local(QDateTime::currentDateTime());
ui->clock->setText(local.toString());
}