I have my main thread which is in charge with the GUI and my worker thread which is in charge with doing stuff with my webcam.
I am able to emit a signal from my main thread to my webcam thread, but it never receives it! Could somebody explain me why?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setThreadsAndObj();
setConnects();
startCamera();
}
void MainWindow::setThreadsAndObj()
{
cameraClassPtr = new cameraClass();
cameraThread = new QThread();
}
void MainWindow::setConnects()
{
....
connect(this, &MainWindow::savePicture, cameraClassPtr, &cameraClass::takePicture);
}
void MainWindow::startCamera()
{
//camera start
connect( cameraThread, &QThread::started, cameraClassPtr, &cameraClass::getVideoFrame );
connect( cameraThread, &QThread::finished, cameraClassPtr, &QThread::deleteLater );
connect( cameraThread, &QThread::finished, cameraThread, &QThread::deleteLater );
//receive camera frames
connect(cameraClassPtr, &cameraClass::videoFrameToGui, this, &MainWindow::updateCameraStream);
//start threads
cameraClassPtr->moveToThread(cameraThread);
cameraThread->start();
}
void MainWindow::foo()
{
emit savePicture();
}
void cameraClass::takePicture()
{
qDebug()<<"camera class received signal"; //THIS NEVER GETS PRINTED
}
Could someone explain me why my signal is never received and "camera class received signal" is never printed and how to solve this?
I had this loop in my worker thread:
...
static cv::VideoCapture cap(-1);
if( !cap.isOpened() )
{
qDebug()<< "Could not initialize capturing...\n";
}
qDebug()<<"starting live camera";
cap.set(CV_CAP_PROP_FRAME_WIDTH,240);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,320);
while(1)
{
cap >> imgFrame;
int fd = open("/home/John/Desktop/mainImglock.txt", O_RDWR |O_CREAT, 0666);
int rc = flock(fd, LOCK_EX | LOCK_NB);
if(rc==0)
{
cv::imwrite("/home/John/Desktop/camera.jpg", imgFrame);
}
flock(fd, LOCK_UN);
close(fd);
}
I solved the issue by adding: QApplication::processEvents(); in the while(1) loop, so that the event loop isn't hanging.
You need to subclass QThread and call QThread::exec(); in order to process events in this thread.
Related
I am creating an example to understand threads in Qt and want my worker thread to sleep for 1 second between each increment so I can see the debug output. But the sleep makes my main GUI thread non-responsive.
Here is my slot functoin in OddCounter class.
void OddCounter::count()
{
for (int i = 0; i < 10; i++)
{
counter += 2;
qDebug() << counter;
QThread::sleep( 1 );
}
}
My mainwindow class which calls this thread is:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
oddCounter = new OddCounter;
connect(this, SIGNAL(startOddCounter()), evenCounter, SLOT(count()), Qt::QueuedConnection );
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
OddCounter oddCounter;
oddCounter.moveToThread( &thread );
thread.start();
emit startOddCounter();
}
The problem is when I press the button, the counter works and displays the next increment after each second passed but the mainwindow is non responsive all this time! This is not right! I want my mainwindow to be responsive and only the thread should sleep. How do I do that?
You have an error in your code: you are creating another OddCounter which you move to the different thread but your original oddCounter to which the signal is connected still lives in the main thread. You should change your code to this:
void MainWindow::on_pushButton_clicked()
{
oddCounter->moveToThread( &thread );
thread.start();
emit startOddCounter();
}
I am coding a fairly simple application that uses Qt with OpenCV. I have single window that contains a widget which displays a video feed captured from a webcam. The webcam video capture is running in an infinite loop in a separate thread so as not to consume the UI thread.
When I close the window (using the normal "X" button on the top right of the window - this app is being developed in Windows 7), it doesn't seem to be shutting down the program correctly. The window does close visibly, but I put a breakpoint in the destructor of the main window, and the breakpoint never gets hit. Additionally, the thread which does video capture continues to run (I know this because the thread outputs to stdout periodically). Only when I click "stop debugging" in the Qt development environment does it cause everything to completely shut down.
Here is my worker object (not subclassing from QThread):
class Worker : public QObject
{
Q_OBJECT
private:
VideoCapture *cap;
bool finished;
QMutex mutex;
public:
Worker ()
{
cap = new VideoCapture(0);
finished = false;
}
bool isFinished ()
{
QMutexLocker locker (&mutex);
return finished;
}
public slots:
void doWork ()
{
Mat frame;
while(!isFinished())
{
// ...some code that outputs to stdout deleted for clarity...
(*cap) >> frame;
emit resultReady(frame);
}
}
void setFinished (bool f)
{
QMutexLocker locker (&mutex);
finished = f;
}
signals:
void resultReady (Mat frame);
};
Here is the header file for my main window:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
CVImageWidget* imageWidget;
Worker *worker;
QThread workerThread;
public slots:
void handleResults (Mat frame);
signals:
void operate ();
void finishSignal (bool f);
private:
Ui::MainWindow *ui;
};
And the class implementation:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Create a widget for this window.
QWidget *wdg = new QWidget(this);
QGridLayout *grid = new QGridLayout(wdg);
// ...code creating widgets deleted for clarity...
this->setCentralWidget(wdg);
//Start video capture
qRegisterMetaType<Mat>("Mat");
worker = new Worker();
worker->moveToThread(&workerThread);
connect(&workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(this, SIGNAL(operate()), worker, SLOT(doWork()));
connect(worker, SIGNAL(resultReady(Mat)), this, SLOT(handleResults(Mat)));
connect(this, SIGNAL(finishSignal(bool)), worker, SLOT(setFinished(bool)));
workerThread.start();
emit operate();
}
MainWindow::~MainWindow()
{
emit finishSignal(true);
workerThread.quit();
workerThread.wait();
delete ui;
}
void MainWindow::handleResults(Mat frame)
{
imageWidget->showImage(frame);
}
Any help understanding why the program doesn't shut down properly would be appreciated. Thanks!
I have some problems with threading inside QT.
#include <QCoreApplication>
#include "handler.hpp"
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
Handler* handler = new Handler();
handler->StartThread();
return a.exec();
}
What I expect is that with handler->StartThread() statement the function within my thread start to write debug messages and once the internal timer within handler finishes I get the nice line [Press ...] and then a return code of 0. However this is not happening. What I get is:
I WORK... ( 0x1540 )
Worker has finished. ( 0x6f4 )
I WORK... ( 0x1540 )
Worker has finished. ( 0x6f4 )
I WORK... ( 0x1540 )
Worker has finished. ( 0x6f4 )
I WORK... ( 0x1540 )
Worker has finished. ( 0x6f4 )
Thread stopped.
And of course when I stop the execution of the application, the return code is: -1073741510. Obviously not that nice of a zero.
Anyway here's the rest of the application code:
Handler.hpp
#ifndef HANDLER_HPP
#define HANDLER_HPP
#include <QObject>
#include <QThread>
#include <QDebug>
#include <QTimer>
#include "testclass.hpp"
class Handler : public QObject
{
Q_OBJECT
public:
Handler();
~Handler();
void StartThread();
public slots:
void functionFinished();
void threadTerminated();
private:
QTimer* shutdown;
QTimer* timer;
QThread* thread;
MyClass* worker;
};
#endif // HANDLER_HPP
Handler.cpp
#include "handler.hpp"
Handler::Handler() {
shutdown = new QTimer();
thread = new QThread();
timer = new QTimer();
worker = new MyClass();
worker->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), worker, SLOT(runAgain()));
QObject::connect(timer, SIGNAL(timeout()), worker, SLOT(runAgain()));
QObject::connect(worker, SIGNAL(iFinished()), this, SLOT(functionFinished()));
QObject::connect(shutdown, SIGNAL(timeout()), thread, SLOT(quit()));
QObject::connect(thread, SIGNAL(finished()), this, SLOT(threadTerminated()));
shutdown->start(20000);
}
Handler::~Handler() {
QObject::disconnect(thread, SIGNAL(started()), worker, SLOT(runAgain()));
QObject::disconnect(timer, SIGNAL(timeout()), worker, SLOT(runAgain()));
QObject::disconnect(worker, SIGNAL(iFinished()), this, SLOT(functionFinished()));
QObject::disconnect(shutdown, SIGNAL(timeout()), thread, SLOT(quit()));
QObject::disconnect(thread, SIGNAL(finished()), this, SLOT(threadTerminated()));
if (shutdown != 0) {
delete shutdown;
shutdown = 0;
}
if (timer != 0) {
delete timer;
timer = 0;
}
if (thread != 0) {
delete thread;
thread = 0;
}
if (worker != 0) {
delete worker;
worker = 0;
}
}
void Handler::functionFinished() {
qDebug() << "Worker has finished. (" << QThread::currentThreadId() << ")";
timer->start(5000);
}
void Handler::threadTerminated() {
qDebug() << "Thread stopped.";
}
void Handler::StartThread() {
thread->start();
}
MyClass (header - testclass.hpp)
#ifndef TESTCLASS_HPP
#define TESTCLASS_HPP
#include <QTimer>
#include <QObject>
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass();
public slots:
void runAgain();
signals:
void iFinished();
private:
void doWork();
};
#endif // TESTCLASS_HPP
MyClass Source - testclass.cpp
#include "testclass.hpp"
#include <QThread>
#include <QDebug>
MyClass::MyClass() {
}
void MyClass::runAgain() {
doWork();
}
void MyClass::doWork() {
qDebug() << "I WORK...\t(" << QThread::currentThreadId() << ")";
emit iFinished();
}
I've previosly read that it's not a good idea to inherit the classes to be ran inside a thread directly from QThread and so I came up with this solution but it still gets fishy though it's pretty nice. I'm open to any recommendation, this is my first time with QT so better to learn now than feel sorry later.
Oh my bad, I forgot the concrete actual question. Why doesn't the execution ends with a nice exit code of 0?
What I expect is that with handler->StartThread() statement the function within my thread start to write debug messages and once the internal timer within handler finishes I get the nice line [Press ...] and then a return code of 0. However this is not happening.
The reason you're not getting a finished command prompt of "[Press ...]" is due to the QCoreApplication and its exec() call. As the Qt docs state: -
Enters the main event loop and waits until exit() is called
So, you've created a second thread, set it to do some work and finish, but the main thread is still running. You need to exit the main thread.
And of course when I stop the execution of the application, the return code is: -1073741510
It sounds like you're killing the main thread with something like "Ctrl+C". Calling QCoreApplication::exit() when your 2nd thread finishes and has cleaned up, should help here.
I have a multiserverapp that works fine so far. I got 4 cpp files.
Main.cpp constructs the program. MainWindow.cpp constructs the ui and starts (via buttonclick) MyServer.cpp. MyServer.cpp creates a thread and starts MyThread.cpp.
My aim is to show several major steps (like the "server started", "new connection", etc..) on a textBrowser.
I pass the outputs from MyServer.cpp via emit updateUI("server started"); to mainwindow.cpp where the output gets catched by:
//Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myserver.h"
#include "mythread.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::AppendToBrowser(const QString text)
{
ui->textBrowser->append(text);
}
void MainWindow::on_startButton_clicked()
{
MyServer* mServer = new MyServer;
connect(mServer, SIGNAL(updateUI(const QString)), this, SLOT(AppendToBrowser(const QString)));
mServer->StartServer();
ui->textBrowser->setPlainText("Server Started");
}
That works just right because the connect command is just in the mainwindow.cpp itself.
The problem starts one step "deeper" in the mythread.cpp.
I created another signal in the
//MyThread.h
signals:
void updateUI_serv(const QString text);
and connected it in the MyServer.cpp with the MainWindow.cpp.
//MyServer.cpp
#include "myserver.h"
#include "mainwindow.h"
MyServer::MyServer(QObject *parent) :
QTcpServer(parent)
{
}
void MyServer::StartServer()
{
if(!this->listen(QHostAddress::Any,1234))
{
qDebug("Server Error");
}
else
{
qDebug("Server started");
}
}
void MyServer::incomingConnection(int socketDescriptor)
{
qDebug("new connection");
MyThread *thread = new MyThread(socketDescriptor,this);
MainWindow *mMain = new MainWindow;
connect(thread, SIGNAL(updateUI_serv(const QString)),mMain ,SLOT(AppendToBrowser(const QString)));
//flags thread for selfdeletion
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
//calls run
thread->start();
emit updateUI("thread started!");
}
// MyThread.cpp
#include "mythread.h"
#include "mainwindow.h"
#include "myserver.h"
MyThread::MyThread(int ID, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
emit updateUI_serv("start");
}
void MyThread::run()
{
//thread stars here
qDebug("Starting thread");
socket = new QTcpSocket();
emit updateUI_serv("hallo");
//set socketdescriptor number
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
emit error(socket->error());
return;
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
qDebug("client connected");
exec();
}
void MyThread::readyRead()
{
QByteArray Data = socket->readAll();
QString Datain = QString::fromLatin1(Data);
qDebug("Date in:");
emit updateUI_serv("data in:");
socket->write(Data);
}
void MyThread::disconnected()
{
qDebug("Disconnected");
socket->deleteLater();
exit(0);
}
The connect command lays here "in between" the signal (updateUI_serv from mythread.cpp) and the slot (AppendToBrowser from mainwindow.cpp) file.
At this point the program crashes as soon as I try to write data (as a client via telnet) to the serverapp.
I tried to set the connect command into the mainwindow and the mythread as well, but both times I get different problems (like debugging problems, or the text does just not show up in the textBrowser).
Thanks so far.
Eventually the myServer-Object is not running in the MainThread, therefor accessing an ui element from another thread crashes your app.
You can make sure only Messages from mainThread will get displayed by adding the following code to your AppendToBrowser Slot:
if( QApplication::instance()->thread() == QThread::currentThread() )
ui->textBrowser->setPlainText("whateverTextThereShallBe");
else
return;
//You should not run into this else...
This if section checks if the current object calling the update is the mainThread. The else-section checks for erros. If you are running in the else-section you are trying to change ui-elements form a thread which is not the mainThread (UI-Thread). Connect your SIGNAL in server to another SIGNAL (SIGNAL->SIGNAL connection) and add a connect to SIGNAL(SERVER) -> SLOT(MainWindow) in your MainWindow.cpp. Eventually try your connect-call with the 5th. parameter for Queued Connection (Qt::QueuedConnection IIRC).
Ahh i got it on my own.
I solved the problem by creating a NEW function ( void forwarding(const Qstring); ) and in that function i emitted it with the ordinary emit updateUI(text); .. stuff works finaly!
I have an app with a splash screen that I keep up for 2 seconds using the thread sleep method. In my MianWindow I have a object that listens to the sslErrors signal then calls a slot that displays the ssl error. I don't want that dialog with the errors displaying until after the splash screen has closed. This application has to also be able to run from a network folder.
On my local machine it behaves the way I want it to everytime but on a network drive sometimes the SSL error dialog opens while the splash screen is displayed, so when I click the No button on the dialog (for ignoring the errors) my app crashes :S
Main.cpp
#include "mainwindow.h"
#include "single_application.h"
#include <QtCore>
#include <QApplication>
#include <QSplashScreen>
#include <QStringList>
#include <QMessageBox>
const QString CSM_VERSION = QString("v1.0i");
class MyInitThread : public QThread
{
protected:
void run(void)
{
this->sleep(2);
}
};
int main(int argc, char *argv[]) try
{
bool my_global_bool = true;
SingleApplication a(argc, argv, "CSM_CYTO_VIEWER_RUN_ONCE");
a.setProperty("my_global_bool", my_global_bool);
if (a.isRunning())
{
QMessageBox::information(0, "Cyto Viewer " + CSM_VERSION, "Cyto Viewer is already running.");
return 0;
}
//QApplication a(argc, argv);
QApplication::setWindowIcon(QIcon(":/logo/csm.ico"));
QApplication::setApplicationName(QString("Cyto Viewer"));
QApplication::setApplicationVersion(QString(CSM_VERSION));
//Check if system has a system tray
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
QMessageBox::critical(0, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray "
"on this system."));
return 1;
}
Q_ASSERT( QSslSocket::supportsSsl() );
MainWindow w;
QPixmap pixmap(":/splash/splash.png");
QSplashScreen splash(pixmap,Qt::WindowStaysOnTopHint);
w.setWindowOpacity(0);
w.setWindowTitle("Cyto Viewer " + CSM_VERSION);
splash.show();
QEventLoop loop;
MyInitThread *thread = new MyInitThread();
QObject::connect(thread, SIGNAL(finished()), &loop, SLOT(quit()));
QObject::connect(thread, SIGNAL(finished()), &splash, SLOT(close()));
QObject::connect(thread, SIGNAL(finished()), &w, SLOT(slInit()));
thread->start();
loop.exec();
w.show();
return a.exec();
}
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(view->page()->networkAccessManager(), SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError> & )),
this, SLOT(onSslErrors(QNetworkReply*, const QList<QSslError> & )));
}
SSL errors slot
void MainWindow::onSslErrors(QNetworkReply* reply, const QList<QSslError> &errors)
{
if(SSL_ONCE != true){
QString errorString;
bool firstError = false;
//TODO: Get this displaying cirtificate errors
foreach (const QSslError e, errors)
{
if(firstError != true) {
if (!errorString.isEmpty())
errorString += "";
if(e.error() != e.NoError){
errorString += "<ul style=\"background: url(minus-small.png);\"><li style=\"background: url(minus-small.png);\">" + e.errorString() + "</li></ul>";
}
}
}
//Delay MessageBox until the splash screen closes (2 seconds)
//delay(2.5);
QEventLoop loop;
MainWinThread *thread = new MainWinThread();
connect(thread, SIGNAL(finished()), &loop, SLOT(quit()));
//Detect any SSL errors from the network reply
thread->start();
loop.exec(); //Do event processing until the thread has finished!
QMessageBox sslQuestion;
//Fix for QMessageBox width
QSpacerItem horizontalSpacer(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
sslQuestion.setText("Cyto Viewer " + CSM_VERSION);
QGridLayout* layout = (QGridLayout*)sslQuestion.layout();
sslQuestion.setInformativeText("The following SSL error(s) occured: " + errorString + "Do you wish to continue?");
sslQuestion.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
sslQuestion.setIconPixmap(QPixmap(":/logo/lock-ssl.png"));
sslQuestion.setDefaultButton(QMessageBox::No);
sslQuestion.setWindowFlags(Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
sslQuestion.setMinimumWidth(800);
layout->addItem(&horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
int SSLChoice = sslQuestion.exec();
if (SSLChoice == QMessageBox::No) {
reply->abort();
delete systray;
exit(EXIT_FAILURE);
//qApp->quit();
//return;
} else {
reply->ignoreSslErrors(/*errors*/);
SSL_ONCE = true;
}
} else {
reply->ignoreSslErrors();
return;
}
}
My slinit slot (for setting up system tray/hidingmain window)
void MainWindow::slInit()
{
setWindowOpacity(1);
//Set min size, this can be changed through registry
this->setMinimumSize(800, 640);
//System tray
aboutAction = new QAction("About", this);
quitAction = new QAction("Exit", this);
connect (aboutAction, SIGNAL(triggered()), this, SLOT(about()));
connect (quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(aboutAction);
//trayIconMenu->addAction(printAction);
trayIconMenu->addAction(quitAction);
systray = new QSystemTrayIcon(this);
connect(systray, SIGNAL(messageClicked()), this, SLOT(messageClicked()));
QIcon icon(":/logo/csm.ico");
systray->setIcon(icon);
systray->setContextMenu (trayIconMenu);
systray->show();
if(systray->isVisible() /*&& !csmURL.isEmpty()*/){;
QString cdate = QDate::currentDate().toString("yyyy");
trayMsg = "<qt>Cyto Viewer "+CSM_VERSION+" is a browser produced by Perceptive Instruments© " + cdate + " specifically for Cyto Study Manager</b>.</qt>";
}
}