c++ - Qt update value every second -



c++ - Qt update value every second -

i want update value of label every 1 sec command, been trying display via while loop...however ui not load , not work...

any suggestions/help appreciated...

below test code...

#include "mainwindow.h" #include "ui_mainwindow.h" #include <qprocess> #include <qstring> #include <qtcore/qtextstream> #include <qregularexpression> #include <qregularexpressionmatch> #include <qregularexpressionvalidator> #include <iostream> mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent) , ui(new ui::mainwindow) { ui->setupui(this); txmessage(); } mainwindow::~mainwindow() { delete ui; } void mainwindow::txmessage() { qstring command = "bash -c \"netstat -i | grep ens33 | awk \'{print $3}\'\""; int temp = 0; droc = new qprocess; while (temp != 1) { droc->start(command); droc->waitforfinished(); qstring value = droc->readallstandardoutput(); ui->label_3->settext(value); } }

you must homecoming command event loop. while loop create application unresponsive. you're leaking process instance. at to the lowest degree utilize awk total potential: netstat -i | awk '/ens33/ { print $3; exit 0 }'. command string invalid anyway, since \' not valid c/c++ escape sequence. don't need escape single quotes. you're invoking 3 processes no reason @ (bash, grep , awk). qt capable of extracting info want. you're allocating things on heap no reason either. note absence of explicit new , delete in code below. @ least, should using qscopedpointer or std::unique_ptr (but never std::auto_ptr!).

the next self-contained illustration works on both qt 4 , qt 5.

#include <qlabel> #include <qhboxlayout> #include <qbasictimer> #include <qprocess> #include <qapplication> class widget : public qwidget { q_object qhboxlayout m_layout; qlabel m_label; qbasictimer m_timer; qprocess m_process; void timerevent(qtimerevent * ev) { if (ev->timerid() == m_timer.timerid()) txmessage(); } void txmessage() { m_timer.stop(); m_process.start("netstat", qstringlist() << "-i", qprocess::readonly); } q_slot void finished(int rc) { starttimer(); if (rc != 0) { m_label.settext("error"); } else { qstring output = qstring::fromlocal8bit(m_process.readall()); qstringlist lines = output.split('\n', qstring::skipemptyparts); foreach (qstring line, lines) { if (!line.contains("ens33")) continue; qstringlist args = line.split(' ', qstring::skipemptyparts); if (args.count() >= 3) { m_label.settext(args.at(3)); return; } } } m_label.settext("..."); } void starttimer() { #if qt_version>=qt_version_check(5,0,0) m_timer.start(1000, qt::coarsetimer, this); #else m_timer.start(1000, this); #endif } public: widget(qwidget * parent = 0) : qwidget(parent), m_layout(this), m_label("...") { m_layout.addwidget(&m_label); starttimer(); connect(&m_process, signal(finished(int)), slot(finished(int))); } }; int main(int argc, char ** argv) { qapplication app(argc, argv); widget w; w.show(); homecoming app.exec(); } #include "main.moc"

c++ c qt qt5

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -