gpt4 book ai didi

c++ - 从不同线程创建 QMainWindow

转载 作者:行者123 更新时间:2023-11-28 06:25:31 25 4
gpt4 key购买 nike

我尝试创建类似窗口池的东西。您可以在程序中的任何地方使用这些窗口来显示图形和绘图等。小部件运行良好,但目前的主要问题是创建池的尝试受挫。一个非 QObject-Object 应该表示一个 QMainWindow 以切断与 Qt 的绑定(bind)。

我无法创建小部件 -> 我尝试了 invokeMethode、connect 和 QTimer,但没有任何效果。有时方法不会被调用,或者我不在 gui 线程中……有什么想法吗?

编辑 2 - 新版本:

标题:

#pragma once
#include <QMainWindow>
#include <QTimer>

class MyWindow : QObject
{
Q_OBJECT
public:
MyWindow();
};

class QWindowPool : public QObject
{
Q_OBJECT
public:
QWindowPool();
public slots:
void createWindow();
};

class QWindow : public QMainWindow
{
Q_OBJECT
};

CPP: #包括

#include <QApplication>
#include <QTimer>
#include <QtConcurrent/qtconcurrentrun.h>

#include <iostream>
#include <future>

static QWindowPool *pool = new QWindowPool();

QWindowPool::QWindowPool() {

// check if app is running
if (!QApplication::instance()) {
bool appOnline = false;
QtConcurrent::run([&appOnline](){
int c = 0;
new QApplication(c, NULL);
appOnline = true;
qApp->exec();
});
while (!appOnline) {}
}
moveToThread(QApplication::instance()->thread());
}

void QWindowPool::createWindow() {
printf("window created\n");
new QWindow();
}

MyWindow::MyWindow() {
QTimer::singleShot(0, pool, SLOT(createWindow()));
}

int main()
{
MyWindow mw;
std::thread t1([](){
MyWindow mw;

std::thread t2([](){
MyWindow mw;
});

t2.join();
});
t1.join();

std::cin.ignore();
return 0;
}

现在代码做了应该做的事。我可以在不同的线程中创建小部件。但是有两种情况,我会堆叠在:

  • 有人(任何想使用这个库的人)在我之前创建 QApplication 并且从不调用 qApp->exec

  • 有人想用 Widget 创建自己的 UI,但在我的 qt::concurrent gui 线程中不起作用。他可能不会接受这一点。

我想要的是在最终的应用程序中:用户应该有可能在他的代码和任何线程中的任何地方编写:

MyWindow mw(dataToDisplay)

应该创建窗口并显示给他。

最佳答案

Qt 将只允许您从主 GUI 线程创建小部件,这在文档中明确提到(强调我的):

As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a "worker thread" because it is used to offload processing work from the main thread.

关于c++ - 从不同线程创建 QMainWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28602040/

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