2016-04-15 1 views
0

QTableWidget (dans mon code, ipTable) L'élément provient de test_data.txt. Mais le fichier test_data.txt change toutes les 3 secondes. Je veux actualiser la table automatiquement ..Actualiser QTableWidget automatiquement possible?

Comment mettre à jour automatiquement QTableWidget ..?

Ceci est mon 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()); 
} 

Répondre

1

Une option consiste à utiliser

QFileSystemWatcher::fileChanged(const QString &path) 

et recevoir un signal, chaque fois que le fichier est modifié. Cette recommandation dépend de la fréquence à laquelle le fichier est modifié et du nombre de fichiers que vous souhaitez regarder.

+0

Merci pour votre réponse !! –

+0

J'ajoute ce code dans Dialog :: Dialog (parent QWidget *): Ca marche mais je travaille une fois ... je fais mal? 'watcher = new QFileSystemWatcher (this); Connect (observateur, SIGNAL (fileChanged (const QString &)), ceci, SLOT (refresh_table())); watcher-> addPath ("/ home/yein/widget/test_data.txt"); ' –

+0

Cela fonctionne seulement la première fois? Ton code me semble plutôt bon. Changez-vous le fichier? Ou est-ce que vous le supprimez et le recréer quelque part? – Paraboloid87