Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Close / Stop MDIChild in its constructor



  • I want to avoid to see empty data tables in my app. So I want to stop MDIChild creation while it is created.
    As sample:

    MdiChildDiskInfo::MdiChildDiskInfo(QWidget* parent, const QString &device)
        :strBurnDrive(device){
          setAttribute(Qt::WA_DeleteOnClose);
    
          readDiskInfo();
    
    }
    
    void MdiChildDiskInfo::readDiskInfo()
    {
       int nError = 0;
       .....
       if( nError == 1 ) this->parentWidget()->close();
    }
    

    The problem here is, if I run this code the whole app is closed. Without parentWidget() it leaves empty dialogs. So how can I programmatically stop MDIChild creation?



  • @pixbyte
    What "dialogs", there are no dialogs here? The code should probably be one of

    if( nError == 1 ) this->close();
    if( nError == 1 ) this->hide();
    

    Maybe it does not like you closing/hiding from the constructor, I don't know. Temporarily try it from elsewhere to test.

    You might also want to read e.g. https://www.qtcentre.org/threads/12409-Close-a-QMdiSubwindow . I'm not sure if it relates to what you see.



  • Dialog or widgets...I think everyone knows what I mean.


  • Lifetime Qt Champion

    @pixbyte said in Close / Stop MDIChild in its constructor:

    Dialog or widgets...I think everyone knows what I mean.

    Yep, but for dialogs, there are Accept and Reject etc so in some use cases its really not the same :)
    Anyway

    if whole application closes, please try setting
    setQuitOnLastWindowClosed(false); on the application.
    https://doc.qt.io/qt-5/qguiapplication.html#quitOnLastWindowClosed-prop
    and see if that is what you are experiencing.

    That said, its really not super to try to cancel construction from within the ctor.
    However, it should work. ( i think)

    Is there a reason you cant just do

    bool MdiChildDiskInfo::readDiskInfo() .. <<< let it return a status if empty or not

    MdiChildDiskInfo * di = new MdiChildDiskInfo(..)
    if (! di->readDiskInfo() )
    di->DeleteLater(); // nothing to show get rid of it
    else
    di->show();
    (psudo code)


Log in to reply