Here are the code snippets. The thread class that will do some work:
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QWidget* parent) : QThread(parent) {}
signals:
void results(size_t added);
private:
void run() override { emit results(7); }
};
The main window class:
class MyMain : public QMainWindow
{
Q_OBJECT
public:
MyMain(QWidget* parent = Q_NULLPTR) : QMainWindow(parent) {}
private slots:
void imagesAdded(size_t added) { qDebug() << "images added"; }
private:
void test() {
MyThread* thread = new MyThread(this);
connect(thread, &MyThread::results, this, &MyMain::imagesAdded);
connect(thread, &MyThread::finished, thread, &QObject::deleteLater);
thread->start();
}
};
In the debugger, I can see the "results" signal connects successfully and that the signal is emitted, however, it is never received.
you forgot to write something like moveToThread and exec() and ...
as I underestand from your question I write this example and run it:
In mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
#include <QThread>
class MyThread: public QThread
{
Q_OBJECT
public:
MyThread();
protected:
void run();
signals:
void results(int);
};
#endif // MYTHREAD_H
In mythread.cpp
#include "mythread.h"
MyThread::MyThread():
QThread(nullptr)
{
moveToThread(this);
}
void MyThread::run()
{
emit results(7);
exec();
}
In mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui
{
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void imagesAdded(int added)
{
qDebug() << "images added" << added;
}
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
In mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mythread.h"
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MyThread *thread = new MyThread();
connect(thread, &MyThread::results, this, &MainWindow::imagesAdded);
connect(thread, &MyThread::finished, thread, &QObject::deleteLater);
thread->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
The output :
Related
I'm using the Qt application. When I'm trying to call the subclass from the connect, subclass (doWork) function it's not getting called. please refer to my sample code and help me to solve the issue.
I'm using the Qt application. When I'm trying to call the subclass from the connect, subclass (doWork) function it's not getting called. please refer to my sample code and help me to solve the issue.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "QMainWindow"
#include <QThread>
#include "Worker.h"
namespace Ui { class MainWindow; }
class MainWindow: public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow(); private:
Ui::MainWindow *ui;
QThread *thread;
Worker *worker;
signals:
void requestUpdate(int initial);
};
#endif // MAINWINDOW_H
Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
thread = new QThread();
worker = new Worker();
worker->moveToThread(thread);
connect(this, SIGNAL(requestUpdate(int)), worker, SLOT(doWork(int)));
int enc = 10;
emit requestUpdate(enc);
}
MainWindow::~MainWindow()
{
delete thread;
delete worker;
delete UI;
}
Worker.h
#ifndef WORKER_H
#define WORKER_H
#include<QObject>
class Worker: public QObject
{
Q_OBJECT
signals:
void workRequested();
public slots:
void doWork(int initial);
};
#endif // WORKER_H
Worker.cpp
#include "Worker.h"
#include <QTimer>
#include <QEventLoop>
#include <QThread>
#include <QDebug>
Worker::Worker(QObject *parent) :
QObject(parent)
{
_working =false;
_abort = false;
}
void Worker::doWork(int initial)
{
qDebug() << "initial" << endl;
}
Thanks & Regards,
Haji
I know this question asked many times but still confused and cannot find a solution.
I have a MainWindow and a class.
In my class I have a signal which I emit in the method call.
Problem: Slot method is not firing.
Here is my code.
BWorker.h
#pragma once
#include <QObject>
class BWorker : public QObject
{
Q_OBJECT
public:
BWorker(QObject *parent);
~BWorker();
void doSomething();
signals:
void signalSomething();
};
BWorker.cpp
#include "BWorker.h"
BWorker::BWorker(QObject *parent)
: QObject(parent)
{
}
BWorker::~BWorker()
{
}
void BWorker::doSomething()
{
emit signalSomething();
}
QtGuiApplication1.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
public slots:
void workDone();
private:
Ui::QtGuiApplication1Class ui;
};
QtGuiApplication1.cpp
#include "QtGuiApplication1.h"
#include "BWorker.h"
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
BWorker bworker(this);
connect(&bworker, SIGNAL(bworker.signalSomething), this, SLOT(workDone()));
bworker.doSomething();
}
void QtGuiApplication1::workDone() {
}
Your problem is here:
SIGNAL(bworker.signalSomething)
This is not valid and you should see a message in terminal saying that there is no such signal. The correct syntax for your case would be:
SIGNAL(signalSomething())
Please post your code by copy-pasting it. Do not edit the code: The problem could be emerged from where you edited.
The code of mine worked fine.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void workDone();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "worker.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Worker worker(this);
connect(&worker, SIGNAL(signalSomething()), this, SLOT(workDone()));
//connect(&worker, &Worker::signalSomething, this, &MainWindow::workDone);
worker.doSomething();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::workDone()
{
qDebug() << "Done.";
}
worker.h:
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = 0);
void doSomething();
signals:
void signalSomething();
public slots:
};
#endif // WORKER_H
worker.cpp:
#include "worker.h"
Worker::Worker(QObject *parent) : QObject(parent)
{
}
void Worker::doSomething()
{
emit signalSomething();
}
Application Output:
Debugging starts
Done.
Debugging has finished
Printed out "Done." means the codes are working.
I applied an C++ example of working with threads in Qt 5.7. All things are good except two things:
1- I used Signal & Slot to update my label in the main form. the problem is that is no effect. Really, I don't know where is the issue.
2- The loop works fine, but when I exit my program I see (throught the "Application Output") that the loop still work (I think that's related with the started thread).
This my little example:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void updateLabelText(const QString TheString);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "worker.h"
//#include <QDebug>
#include <QThread>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QThread *workerThread = new QThread;
Worker *worker = new Worker;
worker->moveToThread(workerThread);
connect(worker, SIGNAL(sendText(const QString)), this, SLOT(updateLabelText(QString)));
workerThread->start();
}
void MainWindow::updateLabelText(const QString TheString)
{
ui->label->setText(TheString);
}
worker.h
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker();
public slots:
void doWork();
signals:
void sendText(const QString);
};
#endif // WORKER_H
worker.cpp
#include "worker.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QThread>
#include <QMessageBox>
#include <QApplication>
Worker::Worker()
{
doWork();
}
void Worker::doWork()
{
for (int i = 0; i<999999; i++) {
emit sendText(QString::number(i));
qDebug() << "The number is : " + QString::number(i);
qApp->processEvents();
//QThread::msleep(5);
}
}
How can I fix this?
Thanks.
In your code, doWork() function is called from the Worker's constructor, the constructor is invoked in the main thread (that is done in the line Worker* worker= new Worker;).
Of course, that is not what you meant to do, since it will cause the main thread to execute the for loop in doWork() before even reaching into the connect call.
Instead of calling doWork() from the Worker's constructor, you should connect the thread's started() signal to doWork() slot, then call thread->start() after moving the Worker object to the new thread. This will leverage Qt cross-thread signals to invoke doWork() in the new thread as soon as it starts.
Here is how your code should look like:
#include <QtWidgets>
//QThread wrapper for safe destruction
//see http://stackoverflow.com/a/19666329
class Thread : public QThread{
using QThread::run; //final
public:
Thread(QObject* parent= nullptr): QThread(parent){}
~Thread(){ quit(); wait();}
};
class Worker : public QObject{
Q_OBJECT
public:
explicit Worker(QObject* parent= nullptr): QObject(parent){}
~Worker()= default;
Q_SIGNAL void sendText(QString text);
Q_SIGNAL void workFinished();
Q_SLOT void doWork(){
for (int i = 0; i<1000; i++) {
emit sendText(QString::number(i));
QThread::msleep(5);
}
emit workFinished();
}
};
class Widget : public QWidget{
Q_OBJECT
public:
explicit Widget(QWidget* parent= nullptr): QWidget(parent){
layout.addWidget(&buttonWork);
layout.addWidget(&label);
connect(&buttonWork, &QPushButton::clicked,
this, &Widget::buttonWorkClicked);
}
~Widget()= default;
Q_SLOT void buttonWorkClicked(){
Thread* thread= new Thread(this);
Worker* worker= new Worker;
worker->moveToThread(thread);
//invoke doWork as soon as the thread is started
connect(thread, &QThread::started, worker, &Worker::doWork);
connect(worker, &Worker::sendText, this, &Widget::updateLabelText);
//quit the thread when work is finished
connect(worker, &Worker::workFinished, thread, &QThread::quit);
//destroy thread and worker object when work is finished
connect(thread, &QThread::finished, thread, &QObject::deleteLater);
connect(thread, &QThread::finished, worker, &QObject::deleteLater);
//start the thread
thread->start();
}
Q_SLOT void updateLabelText(QString text){
label.setText(text);
}
private:
QVBoxLayout layout{this};
QPushButton buttonWork{"Work"};
QLabel label{"No work yet"};
};
int main(int argc, char* argv[]){
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
I solved my problem by following the comment of Mr #Mike.
Simply, I removed the doWork() function from the instructor. Then, I added new connection between the thread's started() SIGNAL and worker's doWork() SLOT.
So thes are the changes:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "worker.h"
#include <QThread>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QThread *workerThread = new QThread;
Worker *worker = new Worker;
connect(workerThread, SIGNAL(started()), worker, SLOT(doWork()),Qt::QueuedConnection);
connect(worker, SIGNAL(sendText(const QString)), this, SLOT(updateLabelText(const QString)),Qt::QueuedConnection);
worker->moveToThread(workerThread);
workerThread->start();
}
void MainWindow::updateLabelText(const QString TheString)
{
ui->label->setText(TheString);
}
worker.cpp
#include "worker.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QThread>
#include <QMessageBox>
#include <QApplication>
Worker::Worker()
{
doWork();
}
void Worker::doWork()
{
for (int i = 0; i<999999; i++) {
emit sendText(QString::number(i));
qDebug() << "The number is : " + QString::number(i);
qApp->processEvents();
QThread::msleep(5);
}
}
I am trying to implement signal/slot connection which will show alert in editLine. I have no idea why the slot is not executing.
I tried: connect(mKlient, SIGNAL(showInfo(QString)), qApp, SLOT(aboutQt()));
To be sure that the signal is working correctly. So I think that something is wrong with the slots. Below is the code:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <klient.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Klient *mKlient;
private:
Ui::MainWindow *ui;
public slots:
void onShowInfo(QString);
private slots:
//void on_pushButton_clicked();
void on_pushButtonConnect_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
onShowInfo("This alert is working"); // works
//-------------------Here is the signal/slot connection-------------------------------------
mKlient = new Klient(this);
connect(mKlient,SIGNAL(showInfo(QString)),this, SLOT(onShowInfo(QString))); //not working - no idea why :/
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onShowInfo(QString text){
ui->lineEdit->setText(text);
}
void MainWindow::on_pushButtonConnect_clicked()
{
if(ui->radioButtonTCP->isChecked()){
mKlient->disconnect(this);
mKlient->connectToServer(ui->lineEditIpAddress->text(), ui->lineEditTcpPort->text().toInt());
}
}
klient.h
#ifndef KLIENT_H
#define KLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QAbstractSocket>
class Klient : public QObject
{
Q_OBJECT
public:
explicit Klient(QObject *parent = 0);
void connectToServer(QString,int);
bool isConnectedFlag;
signals:
void showInfo(QString);
public slots:
void connected();
private:
QTcpSocket *mSocket;
};
#endif // KLIENT_H
klient.cpp
#include "klient.h"
Klient::Klient(QObject *parent) :
QObject(parent)
{
isConnectedFlag = false;
}
void Klient::connectToServer(QString address, int port){
mSocket = new QTcpSocket(this);
connect(mSocket, SIGNAL(connected()),this,SLOT(connected()));
mSocket->connectToHost(address,port);
if(!mSocket->waitForConnected(1000)){
//error mSocket->errorString();
}
}
void Klient::connected(){
emit showInfo("Connected"); //this information should be on lineEdit
mSocket->write("siemanko"); //executing properly
isConnectedFlag = true;
}
}
I was following this tutorial to find a bug, but no results:
https://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/
Am I doing something wrong? I am new to Qt so maybe accessing to the GUI is more complicated than I thought.
I found the answer...
In
void MainWindow::on_pushButtonConnect_clicked()
{
if(ui->radioButtonTCP->isChecked()){
mKlient->disconnect(this);
mKlient->connectToServer(ui->lineEditIpAddress->text(), ui->lineEditTcpPort->text().toInt());
}
}
I thought that I am disconnecting TCP connection. Unfortunatelly I was disconnecting signal/slot connection.
Should I delete my question ? How to do this ?
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
cThread = new QThread(this);
cObject = new MyObject();
cObject->moveToThread(cThread);
QObject::connect(ui->pushButton_3, SIGNAL(clicked()),
this, SLOT(close())
);
QObject::connect(cThread, SIGNAL(started()),
cObject, SLOT(doWork())
);
QObject::connect(ui->pushButton_4, SIGNAL(clicked()),
this, SLOT(runThreadSlot())
);
QObject::connect(cThread, SIGNAL(finished()),
cThread, SLOT(deleteLater())
);
QObject::connect(cThread, SIGNAL(finished()),
cObject, SLOT(deleteLater())
);
QObject::connect(cObject, SIGNAL(setStatusBarSignal(QString)),
this, SLOT(setStatusBarSlot(QString))
);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::runThreadSlot()
{
cThread->start();
}
void MainWindow::setStatusBarSlot(QString text)
{
ui->statusBar->showMessage(text);
}
myobject.cpp:
#include "myobject.h"
MyObject::MyObject(QObject *parent) :
QObject(parent)
{
}
void MyObject::doWork()
{
emit setStatusBarSignal(QString::number((qint32) QThread::currentThreadId()));
QThread::currentThread()->quit();
return;
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "myobject.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void runThreadSlot();
void setStatusBarSlot(QString);
private:
Ui::MainWindow *ui;
QThread* cThread;
MyObject* cObject;
};
#endif // MAINWINDOW_H
myobject.h:
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QtCore>
class MyObject : public QObject
{
Q_OBJECT
public:
explicit MyObject(QObject *parent = 0);
signals:
void setStatusBarSignal(QString);
public slots:
void doWork();
};
#endif // MYOBJECT_H
So the pattern is:
pushButton_4 clicked() ---> runThreadSlot() ---> cThread start()
The thread immediately kills itself with QThread::currentThread()->quit();, but when I click on pushButton_4 again, the application crashes.
This is most likely your problem.
QObject::connect(cThread, SIGNAL(finished()),
cThread, SLOT(deleteLater())
);
QObject::connect(cThread, SIGNAL(finished()),
cObject, SLOT(deleteLater())
);
Think about what happens after the finished signal is emitted.