qt多线程

线程函数结束,线程并未结束, 线程需要quit或者terminate才会结束,并且发送finished信号;或者删除线程内存,在点击启动的时候new一个新的线程

qt开启一个多线程

thread的start方法

QThread thread;
QObject:
Newthread myobject = new Newthread;
myobject->movetothread(&thread);
thread.start();
connect(&thread, &QThread::started, myobject, &Newthread::run);
//如果每次都start例如点击,每次都start,就都会触发started,并且创建一个新线程。这样会导致quit和wait的时候出错。好像会一直等待。使用terminate
//经过了解qt线程无法第二次start
只有run在子线程中

信号槽的方法

QThread thread;
QObject:
Newthread myobject = new Newthread;
myobject->movetothread(&thread);
connect(this, &Mainwindow::signal1, myobject, &Newthread::slot1);
connect(this, &Mainwindow::signal1, myobject, &Newthread::slot1);
thread.start();
凡是用信号和槽绑定的slot都运行在子线程中
Table of Contents