QT: How to open several windows (QWidgets) at once

2019-04-14 23:25发布

问题:

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!

回答1:

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



回答2:

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.