gpt4 book ai didi

qt5 : how to create and display custom qdialog from static function within a qthread

转载 作者:行者123 更新时间:2023-12-04 04:47:29 28 4
gpt4 key购买 nike

假设您创建了一个新线程,然后在它启动后调用一个静态函数。在该静态函数中,您需要创建并显示自定义 qdialog。您如何创建它以使其没有父级并位于正确的线程中?

构造函数将父级设置为 0,但它仍然报告有关无法在不同线程中为父级创建子级的错误。由于它是一个静态函数,我无法使用“this”对象,如果没有“this”,我将无法检索当前线程或线程 ID。我以为我可以调用 myCustomDialog->moveToThread() 但我不知道如何从静态函数中确定正确的线程。

如果我使用 QMessageBox 静态函数之一,一切正常。例如,调用 QMessageBox::information(0, tr("Title"), tr("Message")) 不会报告任何错误。如何编写自定义 qdialog 以使其功能类似于 qmessagebox 静态函数?

有没有办法从 qApp 对象中检索所有正在运行的线程的列表?还有其他建议吗?

static int myFunction();

int myObject::myFunction()
{
myCustomDialog *mcd = new myCustomDialog();
// as soon as I call exec() it reports an error and crashes
mcd->exec();
// QObject: Cannot create children for a parent that is in a different thread.

// can't call mcd->moveToThread() without knowing the current thread
// how can I determine the current thread from this static function?
// if parent = 0 then why is this error occurring?
return 0;
}

myCustomDialog(QWidget *parent = 0);

myCustomDialog::myCustomDialog(QWidget *parent) : QDialog(parent)
{
// create widgets and layout here
}

最佳答案

示例...您可以调整它以从静态函数进行调用,但我认为没有必要。以下一种方式使用线程是一种很好的做法:您应该在 GUI 线程中创建对话框,并使用 Qt::BlockingQueuedConnection 将它的 exec() 槽连接到您的信号。

worker .h

#include <QObject>

class Worker
: public QObject
{
Q_OBJECT

signals:
void showDialog();

public:
Worker( QObject *parent = NULL );
~Worker();

public slots:
void doLongWork();

private:
};

worker .cpp
#include <QThread>
#include <QMessageBox>
#include <QDebug>
#include <QCoreApplication>

#include <Windows.h>


Worker::Worker( QObject *parent )
: QObject( parent )
{
}

Worker::~Worker()
{
}

void Worker::doLongWork() // You may override QThread::run with same effect, but it is bad practice
{
qDebug() << "Worker thread id = " << QThread::currentThreadId();
::MessageBoxA( NULL, "Click to show dialog in 3 seconds", NULL, 0 );
::Sleep( 3000 );
emit showDialog(); // "Showing" dialog from non-GUI thread. And wait for close
::MessageBoxA( NULL, "Dialog closed!", NULL, 0 );
qApp->quit();
}

主程序
#include <QApplication>
#include <QDialog>
#include <QThread>
#include <QDebug>

#include "Worker.h"


int main(int argc, char *argv[])
{
QApplication a( argc, argv );
a.setQuitOnLastWindowClosed( false );

QDialog dlg;
QThread workerThread;
Worker worker;

qDebug() << "Main thread id = " << QThread::currentThreadId();

QObject::connect( &workerThread, SIGNAL( started() ), &worker, SLOT( doLongWork() ) );
QObject::connect( &worker, SIGNAL( showDialog() ), &dlg, SLOT( exec() ), Qt::BlockingQueuedConnection ); // !!!See connection type!!!

worker.moveToThread( &workerThread );
workerThread.start();

return a.exec();
}

注意:WinAPI MessageBoxes 仅用于可视化线程确实在等待对话框关闭。

关于qt5 : how to create and display custom qdialog from static function within a qthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17959240/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com