I wrote this code , so I could monitor an ip address. I am using Qt and I want to make it, so when I press the button start it will ping the ip and return a value, like 1, which means that it is the minimum ping or whatever. The problem is that I cant put in a loop the change of a label. I tried QTimer but I couldn't figure it out. This is the code that I wrote:
#include "ui_mainwindow.h"
#include<iostream>
#include<vector>
#include<string>
#include<ctime>
#include<fstream>
#include<QMessageBox>
#include<Windows.h>
#include<QTimer>
#include<QDebug>
using namespace std;
int check(string file){
string a,b;
int x,c;
vector<int>ms;
ifstream fin(file);
while(!fin.eof()){
getline(fin,a);
if(a[0]=='P'){
if(a=="PING: transmit failed. General failure. ")
return -1;
}
if(a[0]=='R'){
if(a=="Request timed out.")
return -1;
for(unsigned int i=0;i<a.size();i++){
if(a[i]=='t'){
x=i+4;
while(a[x]!='m'){
b+=a[x];
x++;
}
c=stoi(b);
ms.push_back(c);
}
}
}
}
for(unsigned int i=0;i<ms.size();i++){
if(ms[i]<20)
return 0;
else if(ms[i]>20 && ms[i]<60)
return 1;
else
return 2;
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked(){
ofstream fout("traffic.txt");
string file_name="result.txt";
QString a="ping ";
QString b = ui->lineEdit->text();
string result;
int j=0;
a+=b;
string x=a.toStdString();
QTimer timer;
constexpr int interval =10;
timer.callOnTimeout([&]{
//qInfo()<<"ok";
system((x+">" + file_name).c_str());
//time_t now = time(0);
//tm *ltm = localtime(&now);
j=check(file_name);
//Sleep(3000);
if(j==0)
ui->label_2->setText("ok");
else if(j==1)
ui->label_2->setText("minimum");
else if(j==-1)
QMessageBox::critical(this,"Error","request time out.Check the ip");
else
QMessageBox::critical(this,"bad","bad");
//Sleep(30000);
timer.start(interval);
});
timer.start(interval);
}
You should connect your timer timeout signal to your lambda function instead of calling timer.callOnTimeout
:
connect(&timer, &QTimer::timeout, this, [this]() {
.... your code
} );
also you don't need to start your timer again in your lambda function, because since you start it once, it will run continuously for it interval till you call timer.stop()
.
Keep in mind that creating a Timer every time you push your button will create multiple instance of Timer that run simultaneously and will cause overload or memory overusing! try to use it more efficiently.
void MainWindow::on_pushButton_clicked(){
ofstream fout("traffic.txt");
QTimer timer;
constexpr int interval =10;
connect(&timer, &QTimer::timeout, this, [this]() {
//qInfo()<<"ok";
string file_name="result.txt";
QString a="ping ";
QString b = ui->lineEdit->text();
string result;
int j=0;
a+=b;
string x=a.toStdString();
system((x+">" + file_name).c_str());
//time_t now = time(0);
//tm *ltm = localtime(&now);
j=check(file_name);
//Sleep(3000);
if(j==0)
ui->label_2->setText("ok");
else if(j==1)
ui->label_2->setText("minimum");
else if(j==-1)
QMessageBox::critical(this,"Error","request time out.Check the ip");
else
QMessageBox::critical(this,"bad","bad");
//Sleep(30000);
});
timer.start(interval);
}