Creating new window understanding question



  • Hi there,

    I have a problem creating a new window from an existing one (this is most probably a dumb newbie question). I have the following code:

    qBandHelper.cpp
    @
    qBandHelper::qBandHelper(QWidget *parent)
    {
    Ui_qBandHelper::setupUi(this);

    connect(action_Members, SIGNAL( triggered() ), this, SLOT( showMemberDialog() ) );
    

    }

    void qBandHelper::showMemberDialog( void )
    {
    QDialog *dlg = new QDialog;
    qBandMembers *memb = new qBandMembers(dlg);

    memb->show();
    

    }
    @

    qBandMembers.cpp
    @
    qBandMembers::qBandMembers(QDialog *dlg)
    {
    Ui_Members::setupUi(dlg);

    connect(qBandMembers::buttonBox, SIGNAL(rejected()), this, SLOT(cancel()) );
    connect(qBandMembers::buttonBox, SIGNAL(accepted()), this, SLOT(save()) );
    

    }
    @

    (this is only the relevant parts, of course). The qBandMembers class inherits from the qBandHelper class, but thats not relevant to the problem I have:

    If I klick the button for the member window, I get a copy of the original window (not exactly if the classes don't inherit, of course), but not the Ui I defined in any way.

    Can somebody clearify my misunderstanding(s)?

    BTW: I can post the Ui-Files (generates to the Designer), if needed.

    Thanks a lot in advance!

    Best regards Jan



  • Hi there again,

    I found out, if I use
    @
    void qBandHelper::showMemberDialog( void )
    {
    QDialog *dlg = new QDialog;
    qBandMembers *memb = new qBandMembers(dlg);

    dlg->show();
    

    }
    @
    the correct window is created, but obviously when I want to destroy it by clicking OK/Cancel in the window (there is a buttonBox in there, which calls QWidget::close(), only the qBandMember objects are destroyed, but not the window itself.

    There must be an easier (or more likely) correct way to generate/destroy a subwindow...



  • Have a look for "Qt::WA_DeleteOnClose":http://doc.qt.digia.com/qt/qt.html#WidgetAttribute-enum

    bq. Makes Qt delete this widget when the widget has accepted the close event (see QWidget::closeEvent()).

    Add the follwing line before you call show

    @dlg->setAttribute(Qt::WA_DeleteOnClose);@



    1. If you want to create a dialog you should inherit QDialog (Add new -> Qt -> Qt Designer Form Class -> QDialog)
    2. Adding your widget via designer is: add standard parent (QWidget for example) -> right click -> promote to... (the rest is obvious)
    3. You should create objects with parents ("Read manual for more info":http://qt-project.org/doc/qt-4.8/qobject.html#QObject)
    4. If you do not want to use designer, you still should use layouts (something like this):
      @QDialog *dlg = new QDialog(this);
      QHBoxLayout *layout = new QHBoxLayout(dlg);
      qBandMembers *memb = new qBandMembers(dlg);
      layout->addWidget(memb)

    dlg->show();@

    P.S.: Qt::WA_DeleteOnClose is also a very good choice; it is used, if you want to delete your dialog without deleting it's parent.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.