Qt4 or Qt5?
can any one tell me whats wrong in the following code
mainwindow.h
mainwindow.cpp#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QThread>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private slots:
void on_pushButton_clicked();
void debug();
private:
Ui::MainWindow *ui;
};
class MyThread : public QThread
{
Q_OBJECT
public:
void run();
signals:
void v_update();
void value();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MyThread thread;
connect(&thread,SIGNAL(v_update()),this,SLOT(debug ()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::debug()
{
qDebug()<<"hello";
}
void MyThread::run()
{
for (int i=0;i<=10;i++)
{
emit this->v_update();
}
this->quit();
}
void MainWindow:n_pushButton_clicked()
{
MyThread thread;
thread.run();
}
MyThread thread; will be destroyed when it goes out of ctor.
You need to create an object in heap.
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
even that too dont work.
Problem is that thread is no emitting the signal.
Where are you connecting the threads signal to the debug slot ?
in the constructor giving it a reference of a thread?
But then in your onClickButton method you are creating another thread and not connecting signals and slots.
So easily solved by removing what you wrote in the constructor ie :
Now in your OnClickButton method add the same code you deleted ie:Originally Posted by shivendra46d
Even for better practice create the thread in a heap rather than a stack.Qt Code:
void MainWindow::on_pushButton_clicked() { MyThread thread; connect(&thread,SIGNAL(v_update()),this,SLOT(debug ())); thread.run(); }To copy to clipboard, switch view to plain text mode
Good Luck.
Last edited by toufic.dbouk; 24th October 2013 at 13:48.
I haven't written Qt multithreaded apps so far. I didn't need them but, AFAIK from writing multithreaded apps under other environments...
MyThread must not be on the stack - neither in the ctor nor in the push button handler. The thread.run() triggers the thread and returns immediately. Therefore, the push button handler creates a thread, connects it, runs it but then kills it immediately and destroys the thread object - or rather crashes the app in Qt. See documentation of QThread.
(1) MyThread must be a member of MyWindow so that the thread object gets destroyed when MyWindow does (and MyThread has already finished). Even then MyThread needs some "stop me ASAP" flag which the thread is testing periodically and which you set in the MyWindow dtor and then wait until you get isFinished() from MyThread.
(2) Another possibility is creating MyThread dynamically, again with the "stop me ASAP" flag and setting the flag in the MyThread dtor (and wainting with return from the dtor until you get isFinished()).
@Radek,
You went too far, his question was simple. And the mentioned method doesn't lead to a crash of the application if taken of properly.
Emit a finish signal when the threads job is done and connect it to the desired slot , maybe a deleteLater slot depending on the implementation.
Normally i wouldn't do threads in this way, i would subclass QObject and use moveToThread when needed then start the thread with the correct signals and slots to take care of managing and deleting properly.
Its a sure thing that its for the best to create it on the heap for several reasons.
The users method is not wrong but it has its own cases to use it same as all other implementations.
Yeah, except it doesn't run anything in separate thread this wayThe users method is not wrong (...)
OP: can you answer wysota's question ? I think he wanted to help you with this errorQtConcurrent: No such file or directory
Why ?Originally Posted by stampede
Deriving a class from QThread, re-implementing the QThread::run() method, and using QThread::start() to run it will make sure that that method is run in a different thread.
So all he has to do is create the thread in a heap and call start().
And yes he hasn't answered the main question i am still waiting whether he is using Qt4 or Qt5.
I mean this way, how it is currently implementedAnyway, i think it will be better for OP to use QtConcurrent and give QThread another try later.
I agree with you.
My badi was helping him step by step so i thought you meant the whole approach doesnt work...
I am trying on both the version Qt 4 as well as on Qt 5
Hey can you tell me what i am doing wrong because when ever i try to use connect() signal slot in the call back of push button i get some kind of error why is it so ?
Added after 12 minutes:
This is for all those who want to use some thing like this to play with signal and slot and threads also
Last edited by shivendra46d; 24th October 2013 at 19:20.
Hey thanks a lot buddy
It solved the problem, but i want to do it more properly so please tell me
my email id
shivendra46d@gmail.com
i would like to keep in touch with you pelople
You can keep in touch on this forum its more helpful than an email over gmail![]()
does running a function in separate thread which carries out heavy computation and which when in main thread freezes the ui . will help in over coming the problem? is there any other way to remove freezing of ui?
i went through some tutorial but not of good help
Yes, if implemented properly. Keep in mind that you will have to synchronize with main thread if you want to display the results.does running a function in separate thread which carries out heavy computation and which when in main thread freezes the ui . will help in over coming the problem?
If it is only a single function, I'd use QtConcurrent to run it. Few lines of code and you have your processing done in separate thread.
Depends on what is causing the GI freezing in your heavy computation method.Originally Posted by shivendra46d
Yes, when you run it in a different thread obviously the main GUI wont freeze.Originally Posted by shivendra46d
Read more tutorials, read Qt documentation and reference. Check this link for example Keeping the GUI ResponsiveOriginally Posted by shivendra46d
Good Luck.
Bookmarks