QT:如何一次打开多个窗口(QWidgets)?

发布于 2024-09-28 21:38:49 字数 656 浏览 1 评论 0原文

我正在做 Web 界面测试程序,该程序应该同时在两个 webkit 窗口中打开两个 url。

我已经编写了测试自动化的代码。

1) 用户按下“Go”按钮,webkit (QWidget) 窗口打开

2) TestBot 类对象执行测试

3) 关闭

现在我的问题:单击“Go”按钮后,如何打开两个(或三个或更多)webkit (QWidget) 窗口,我的意思是,如何同时启动多个 TestBot,以便它们并行完成所有工作?

我明白,我需要查看多线程,我想到我需要将 QThread 继承到我的 TestBot 类定义中,作为“class TestBot : public QThread”,但是这是正确的解决方案吗?我做得对吗?接下来该怎么办?

我不能将代码编写为:

QThread process1;
QThread process2;
process1->start();
//some code here
process1->quit();

process2->start();
//some code here
process2->quit();

让一切并行工作吗?

我是Winapp世界的新手,我来自Web编程。希望得到您的帮助!

I'm doing web interface testing program which should open two urls in two webkit windows simultaneously.

I already did the code for the test automation.

1) User pushes 'Go' button and webkit (QWidget) window opens

2) TestBot class object performs tests

3) Closes

Now my question: After clicking on the button 'Go', how do I open two (or three or more) webkit (QWidget) windows, I mean, how do I launch several TestBots simultaneously so they do all the work parallel?

I understood, that I need to look at multithreads, I came up I need to inherit QThread into my TestBot class definition as 'class TestBot : public QThread', but is this right solution and do I do it right? What's to do next?

Can't I just write code as:

QThread process1;
QThread process2;
process1->start();
//some code here
process1->quit();

process2->start();
//some code here
process2->quit();

to make everything work parallel?

I'm a newbie in Winapp world, I came from Web programming. Hope for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-10-05 21:38:49

为了一次显示多个窗口,只需将它们排列起来并显示即可。

void ShowMultiple()
{
    QWidget *win1 = new QWidget();
    QWidget *win2 = new QWidget();
    QWidget *win3 = new QWidget();

    win1->show();
    win2->show();
    win3->show();
}

运行此代码后,应该会显示 3 个新的(空白)窗口。但是,如果您尝试执行一些需要很长时间才能显示窗口的代码,情况可能会发生变化。在这种情况下,您可能需要查看线程或 Qt::Concurrent 示例,请记住您真的、真的不能干扰任何其他线程中的 UI。

In order to show multiple windows at once, just line them up and show them.

void ShowMultiple()
{
    QWidget *win1 = new QWidget();
    QWidget *win2 = new QWidget();
    QWidget *win3 = new QWidget();

    win1->show();
    win2->show();
    win3->show();
}

After this code runs, there should be 3 new (blank) windows shown. However, if you are trying to execute some code that takes a long time along with showing the windows, things might change. In that case, you might want to look at the threads or Qt::Concurrent examples, bearing in mind that you really, really can't mess with the UI in any other thread.

荒人说梦 2024-10-05 21:38:49

您可以尝试使用 QtConcurrent 命名空间 中的函数来执行异步任务,特别是run 一个。

You can try to use the functions into the QtConcurrent namespace for asynchronous tasks, specially the run one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文