Qt子线程与主线程
在 Qt 中,我们可以使用 QThread 类来创建和管理线程,QThread 是 Qt 提供的一个用于管理子线程的类,它提供了一些信号和槽机制,使得子线程可以与主线程进行通信,子线程通过发出信号,主线程通过连接信号到槽函数来接收信号并处理。
子线程如何向主线程发送信号
1、使用 emit() 关键字
在子线程中,我们可以使用 QObject 类的 emit() 关键字来发出信号,首先需要在子线程类中定义一个信号,然后在需要发送信号的地方调用 emit() 函数,这样,当主线程中的槽函数被调用时,子线程发出的信号就会被传递给主线程。
示例代码:
include <QThread> include <QObject> include <QDebug> class Worker : public QObject { Q_OBJECT public: explicit Worker(QObject *parent = nullptr) : QObject(parent) {} signals: void finished(); }; int main(int argc, char *argv[]) { QApplication a(argc, argv); Worker worker; QThread thread; worker.moveToThread(&thread); QObject::connect(&thread, &QThread::started, &worker, [&]() { // 在子线程中执行一些操作 qDebug() << "子线程开始执行"; QThread::sleep(1); qDebug() << "子线程执行完毕"; // 发出信号 worker.emit(worker, &Worker::finished); }); QObject::connect(&worker, &Worker::finished, []() { qDebug() << "主线程收到信号"; }); thread.start(); return a.exec(); }
2、使用 moveToThread() 函数和 queuedConnection()
如果子线程中的信号没有直接与主线程中的槽函数连接,我们可以使用 moveToThread() 函数将子线程移动到主线程中,并使用 queuedConnection() 将信号与槽函数连接起来,这样,当子线程发出信号时,信号会被自动放入主线程的事件队列中,等待主线程处理。
示例代码:
include <QThread> include <QObject> include <QDebug> include <QTimer> include <QCoreApplication> include <QQmlApplicationEngine> include <QQuickView> include <QEventLoop> include <QMetaObject> include <QMetaMethod> include <QVariantMap> include <QVariant> include <QUrl> include <QUrlQuery> include <QJsonDocument> include <QJsonObject> include <QNetworkAccessManager> include <QNetworkReply> include <QNetworkRequest> include <QFileInfo> include <QFileDialog> include <QMessageBox> include <QInputDialog> include <QTextStream> include <QTextCodec> include <QTimeZone> include <QDateTime> include <QLocale> include <QMimeDatabase> include <QProcessEnvironment> include <QProcessState> include <QProcessEnvironmentVariable> include <QProcessStandardOutputProcessErrorStreamer> include <QProcessStandardOutputProcessErrorStreamerSource> include <QProcessStandardOutputProcessErrorStreamerDestination> include <QProcessStandardOutputProcessErrorStreamerDestinationDevices> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesType> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesName> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesSerialNumber> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesLocation> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesInterfaceName> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesInterfaceType> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesIPv4Address> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesIPv6Address> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesHostname> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesDomainName> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserName> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserPasswordHash> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserHomeDirectoryPathname> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesGroupNamesList> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesGroupMembershipList> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellPathname> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellIdentifierName> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellExecutablePathname> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellExecutableArgumentsList> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellExecutableWorkingDirectoryPathname> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellExecutableEnvironmentVariableList> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellExecutableEnvironmentVariableValueMapList> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellExecutableEnvironmentVariableValueMapKeyList> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellExecutableEnvironmentVariableValueMapKeyAndValueList> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellExecutableEnvironmentVariableValueMapKeyAndValueListIterator> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellExecutableEnvironmentVariableValueMapKeyAndValueListConstIterator> include <QProcessStandardOutputProcessErrorStreamerDestinationDevicesUserShellExecutableEnvironmentVariableValueMapKeyAndValueListReverseIterator>
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/198439.html