Unsolved Creating custom slots and signals
-
Anyway, after using code suggested by sierdzo,
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QTimer *timer = new QTimer(this); timer->setInterval(3000); timer->setSingleShot(false); // Using a lambda, but you can (should) create your own slot for this, of course QObject::connect(timer, &QTimer::timeout, this, [this] { QProcess *process = new QProcess(this); process->start("cat /proc/loadavg"); QObject::connect(process, &QProcess::finished, this, &MainWindow::updateCpu); }); cores = getCpuCores(); } void MainWindow::updateCpu(int exitCode, QProcess::ExitStatus exitStatus) { QProcess *process = qobject_cast<QProcess *>(sender()); QString result = process->readAllStandardOutput().trimmed(); result.truncate(4); ui->lcdNumber->display(result); process->deleteLater(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMouseEvent> #include <QMainWindow> #include <QProcess> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); int getCpuCores(); void updateCpu(int exitCode, QProcess::ExitStatus exitStatus); void updateRAM(QString value); void updateNetwork(QString value); void updateFanSpeed(QString value); int cores; ~MainWindow(); private slots: void on_actionExit_triggered(); private: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); int m_nMouseClick_X_Coordinate; int m_nMouseClick_Y_Coordinate; Ui::MainWindow *ui; bool isMouseDown = false; }; #endif // MAINWINDOW_H
Qtcreator shows me this:
No matching function call for call to "connect"
-
Hint from @SGaist is far better :-)
No matching function call for call to "connect"
Which connect? QtC will show you exactly the line where compilation failed.
-
This part:
QObject::connect(process, &QProcess::finished, this, &MainWindow::updateCpu);
-
What is the exact error ?
-
/home/bot/Hardware_mon/mainwindow.cpp:19: error: no matching function for call to ‘MainWindow::connect(QProcess*&, <unresolved overloaded function type>, MainWindow*, void (MainWindow::*)(int, QProcess::ExitStatus))’ QObject::connect(process, &QProcess::finished, this, &MainWindow::updateCpu); ^
-
The documentation has the proper code for this connect.
-
So something like this?
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [=](int exitCode, QProcess::ExitStatus exitStatus){ QProcess *process = new QProcess(this); process->start("cat /proc/loadavg"); QObject::connect(process, &QProcess::finished, this, &MainWindow::updateCpu); });
that connect looks even more confusing, i'm even more lost now
-
Hi @Fuchsiaff: Then try this:
QByteArray avg; QFile f("/proc/loadavg"); if (f.open(QIODevice::ReadOnly)) { avg = f.readAll(); f.close(); } // avg now contains the content of /proc/loadavg
-
QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished)
reads as: use the overloaded version of finished having the matching signature:
QProcess::finished(int, QProcess::ExitStatus)
-
I got it to compile with this code
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QTimer *timer = new QTimer(this); timer->setInterval(3000); timer->setSingleShot(false); // Using a lambda, but you can (should) create your own slot for this, of course QObject::connect(timer, &QTimer::timeout, this, [this] { QProcess *process = new QProcess(this); process->start("cat /proc/loadavg"); qDebug() << "hi"; QObject::connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(updateCpu(int, QProcess::ExitStatus))); }); cores = getCpuCores(); }
But now, looking at the application output section, the string "hi" isn't printed so i'm quessing the timer never reached the timeout signal, but why?
-
From the code you posted, you don't start
timer
.