hide qdialog and show mainwindow

烂漫一生 提交于 2019-12-13 07:46:16

问题


I have a Qdialog in which i get some inputs to use on my mainwindow. so it must appear first than mainwindow.

the problem is that my mainwindow does not show up. here's my main.cpp

#include <QtGui/QApplication>

#include "planevolume.h"
#include "dialog.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    Dialog *dialog= new Dialog;
    dialog->show();

    planevolume mainwindow;

    bool dialogcheck = dialog->isHidden();

    if (dialogcheck==1)
    {
        mainwindow.show();
    }
    else
    {
    }

    return app.exec();
}

I have a pushbutton that when pressed hides the qdialog and if it is hidden than the mainwindow should show up, right?

here's the SLOT i used:

void Dialog::startplanevolume()
{
    if (xMax==0 || yMax==0 || zMax==0 || xMMax==0 || yMMax==0 || zMMax==0)
    {
        ui->label_17->setText("Error: Can't start, invalid measures");
    }
    else
    {
        hide();
    }
}

the mainwindow can only start after that button is clicked as only then I have the inputs to the main winodw


回答1:


So the problem here is that calling dialog->show() does not block execution. The minute that call is made, it moves on to the next method. You need to block execution until the user finishes putting input in.

Make your main like this:

QApplication app(argc, argv);

Dialog *dialog= new Dialog;
if ( dialog->exec() ) {
    planevolume mainwindow;
    mainwindow.show();
    return app.exec();
}
return 0;

And in your dialog class, make your method look like:

void Dialog::startplanevolume() 
{
    if (xMax==0 || yMax==0 || zMax==0 || xMMax==0 || yMMax==0 || zMMax==0) 
    {
        ui->label_17->setText("Error: Can't start, invalid measures");
    }
    else 
    {
        this->accept();  // close the dialog with a result of 1
    }
}



回答2:


When you press the button, you call your Dialog::startplanevolume, yes, but that's it. You don't go back to the main loop.

If you want to display your mainwindow, you may want to call a planevolume.show() in your Dialog::startplanevolume, just after the hide.

It might be tricky if your objects are in different files, though. So maybe you could define a signal like DialogChecked, emit this signal in your Dialog::startplanevolume (after the hide, of course...), and modify your main so that it would call mainwindow.setVisible(1) when receiving a DialogChecked.




回答3:


The PushButton action may happen only after app.exec() is called. It makes no sense testing dialog properties before the main loop is entered.

The expected behavior may be reached by setting up the components to start sequentially in an asynchronous way. In Qt world, this means using signals and slots.

connect(dialog, SIGNAL(accept()), &mainwindow, SLOT(show()));


来源:https://stackoverflow.com/questions/12180617/hide-qdialog-and-show-mainwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!