Qt Run Slot In Another Thread

Posted By admin On 11/04/22
  1. Qt Run Slot In Another Thread Set
  2. Qt Run Slot In Another Thread Size
Multithreaded programming is also a useful paradigm for performing time-consuming operations without freezing the user interface of an application. QThread

If you need to execute some code in another thread, then for this you need to create a separate class that will be transferred to another thread using the moveToThread method. Also, I want to note right away that you cannot transfer GUI objects to other threads. Qt programs have two kinds of threads: Main stream. GUI thread Workflows. Then this might be possible. However, the signal-slot mimic will more or less generate a direct function call which will be executed in the thread you are emitting the signal. I do not see a way to tell the thread that it has to execute the function in another thread. I guess you need to separate the different tasks in two objects.

is the easy-to-use class in Qt allow user to implement things in a sperated thread.
'As of Qt4, you can use QThreadQt run slot in another thread pattern to start your own event loops. This might sound somewhat uninteresting at first, but it means you can have your own signals and slots outside the main thread.'
In GUI applications, the main thread is also called the GUI thread because it's the only thread that is allowed to perform GUI-related operations. You can implement, for instance, of a algorithm executed in a blocking way, into a sperated thread. Therefore, your GUI will not be frozen during the execution of algorithm. Then connect the main thread and algorithm thread by signal and slots. Namely, you can now emit a signal in one thread and receive it in a slot in a different thread.
The following is a simple example, in which a second-timer is wrapped in a QThread and a QWidget in main thread can start and stop the timer in any time.
//-----------------------------
Qt Run Slot In Another ThreadCookingClock.cpp in timer thread
//-----------------------------

CookingClock::CookingClock(QObject *parent )
:QThread( parent)
,iSeconds_(10)
,iSecondsAccu_(0)
{
timer_.setInterval(1000); //second timer
connect( &timer_, SIGNAL(timeout()), this, SLOT(countSeconds()));
QThread::start();
}
void CookingClock::run()
{
exec();
}
void CookingClock::countSeconds()
{
iSecondsAccu_++;
emit( secondTicked(iSeconds_ - iSecondsAccu_));
if(iSecondsAccu_>=iSeconds_)
{
emit( timeIsUp());
timer_.stop();
}
}
void CookingClock::stop()
{
if(timer_.isActive())
{
timer_.stop();
}
}
void CookingClock::start()
{
iSecondsAccu_ = 0;
timer_.start();
}

//-----------------------------
CookingClockWidget.cpp in main threadQt run slot in another thread size

Qt Run Slot In Another Thread Set

//-----------------------------

CookingClockWidget::CookingClockWidget(QWidget *parent)
:QWidget(parent)
, clock_(0)
{
clock_ = new CookingClock_v2();
QPushButton* pButtonStart = new QPushButton('start');
QPushButton* pButtonStop = new QPushButton('stop');
connect( pButtonStart, SIGNAL(clicked()), clock_, SLOT(start()));
connect( pButtonStop, SIGNAL(clicked()), clock_, SLOT(stop()));
connect( clock_, SIGNAL(secondTicked(int)), this, SLOT(print(int)));
QHBoxLayout* pLayout = new QHBoxLayout;
pLayout->addWidget(pButtonStart);
pLayout->addWidget(pButtonStop);
setLayout(pLayout);
}
void CookingClockWidget::print( int second)
{
qDebug('second : %d', second);
}

Qt Run Slot In Another Thread Size

Reference: