moveToThread का उपयोग कर किसी ऑब्जेक्ट को क्यूटी में एक थ्रेड से दूसरे स्थानांतरित करने का क्या अर्थ है? सब कुछ moveToThread का उपयोग करने से पहले भी काम करता प्रतीत होता है, जो ऑब्जेक्ट को एक थ्रेड (जीयूआई थ्रेड) से दूसरे थ्रेड (काम) तक ले जाता है और क्यूटी: ऑब्जेक्ट पर उपयुक्त स्लॉट को कॉल करता है।क्यूटी थ्रेड में सिग्नलिंग, एक जीयूआई थ्रेड है?
क्या ऑब्जेक्ट रहता है, जीयूआई थ्रेड या वर्कर थ्रेड के कारण कोई अंतर है?
संपादित करें: मैं एक छोटे से कार्यक्रम बनाया, लेकिन मुझे समझ नहीं आता कैसे QThread सिग्नल और स्लॉट समारोह के साथ काम करता है, मुझे खुशी होगी अगर आप समझा सकता है क्या उदाहरण
#include <QtGui/QApplication>
#include <QPushButton>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QString>
#include "mythread.h"
//GUI calls a thread to do some job and sub update the text box once it is done
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QHBoxLayout * pH = new QHBoxLayout(&w);
QPushButton * pushButton = new QPushButton("asdad");
QLineEdit * lineEdit = new QLineEdit("AAA");
pH->addWidget(pushButton);
pH->addWidget(lineEdit);
w.setLayout(pH);
w.show();
MyThread thread;
qDebug("Thread id %d",(int)QThread::currentThreadId());
QObject::connect(pushButton,SIGNAL(clicked()),&thread,SLOT(callRun())) ;
QObject::connect(&thread,SIGNAL(signalGUI(QString)),lineEdit,SLOT(setText(QString)));
return a.exec();
}
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QMutex>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread();
public slots:
void callRun();
void run();
signals:
void signalGUI(QString);
private:
QMutex mutex;
};
#endif // MYTHREAD_H
#include "mythread.h"
#include <QDebug>
#include <QString>
#include <QMutexLocker>
MyThread::MyThread()
{
}
void MyThread::callRun()
{
qDebug("in thread");
if(!isRunning())
{
this->start(LowestPriority);
exec();
}
else
{
run();
}
}
void MyThread::run()
{
QMutexLocker fn_scope(&mutex);
static int a = 0;
++a;
qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
this->sleep(3);
static QString number;
QString temp;
number += temp.setNum(a);
emit signalGUI(number);
}
साथ moveToThread का प्रयोग होता है
मुझे Qt हैंडलिंग moveToThread के बारे में बताते हुए कोई भी प्रलेखन नहीं देखा गया, मैंने क्यूटी हैंडल सिग्नल स्लॉट कनेक्शन प्रकार के बारे में पढ़ा है जहां ऑब्जेक्ट लाइव है। –
अद्यतन प्रतिक्रिया, मैं थोड़ा सा सरलीकृत हो सकता है, लेकिन ज्यादातर मामलों में यह काम को संभालेगा। –
@Adam आप क्या चाहते हैं "Qt आपके लिए आवश्यक है यदि आपके लिए आवश्यक है और आपने सही कनेक्शन का उपयोग किया है"। उदाहरण के अनुसार 'कनेक्ट (& थ्रेड, सिग्नल (रेंडर इमेज (कॉन्स क्यूमेज और डबल)), यह, एसएलओटी (अपडेट पिक्समैप (कॉन्स क्यूमेज और डबल)); '' थ्रेड' और 'यह' सभी ऑब्जेक्ट्स हैं एक ही धागा लेकिन कनेक्शन कैसे कतार कनेक्शन है? –