使用moveToThread在Qt中将对象从一个线程移动到另一个线程是什么意思?甚至在使用moveToThread之前,一切似乎都工作,moveToThread将对象从一个线程(GUI线程)移动到另一个线程(工作),Qt:connect调用对象上的相应插槽.
由于对象所在的位置,GUI线程或工作线程,有什么区别吗?
编辑:我做了一个小程序,但我不明白QThread如何与Signal和插槽功能一起工作,如果你能解释一下moveToThread的用法,我将不胜感激
#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 …Run Code Online (Sandbox Code Playgroud) #include<filename> and #include<filename.h在C++中使用>有什么区别?使用了哪两个,为什么要使用它?
为什么要在C++函数中最后添加默认参数?
适用于以下类型的信号和插槽
signals:
void textChanged(const QString &);
public slots:
void setText(const QString & text)
Run Code Online (Sandbox Code Playgroud)
textChanged和setText的参数类型似乎无法使用const和&.与仅使用QString相比,常量和参考资格是否有任何区别?
QObject::connect(a,SIGNAL(textChanged(QString)),b,SLOT(setText(QString)));
QObject::connect(a,SIGNAL(textChanged(const QString &)),b,SLOT(setText(const QString &)));
Run Code Online (Sandbox Code Playgroud)
编辑:当SIGNAL或SLOT中使用不兼容的类型时,我没有注意到输出窗口显示错误消息.我认为信号槽机制能够在编译时检测参数类型错误.
是否有任何库有助于在C++应用程序中通过契约原则实现设计?
特别是,我正在寻找一个可以使用原理的库,就像这样.
class someclass {};
class base
{
int a;
int *pint;
someclass objsomeclass;
someclass* psomeclass;
public:
base()
{
objsomeclass = someclass();
psomeclass = new someclass();
pint = new int();
throw "constructor failed";
a = 43;
}
}
int main()
{
base temp();
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,构造函数抛出.哪些对象会泄露,以及如何避免内存泄漏?
int main()
{
base *temp = new base();
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中怎么样?构造函数抛出后如何避免内存泄漏?
c++ ×7
qt ×2
add-in ×1
arguments ×1
class-design ×1
connection ×1
constructor ×1
default ×1
exception ×1
function ×1
ide ×1
include ×1
java ×1
memory-leaks ×1
namespaces ×1
pyqt ×1
qthread ×1
resources ×1
semantics ×1
templates ×1
terminology ×1
types ×1