QWidget window fails to open when "SetParent" is not null

  • My program displays a main window object when launched. On that window is a QPushButton control that should open a dialog window. When the below code executes though, nothing happens. After playing for a few minutes, I found that the problem occurs when "setParent" is executed. As long as that line is commented out, the code works fine. Once I uncomment it, the window fails to open.

    Any idea of what I'm doing wrong?

    cityDialogWindow = new cityDialog;
    cityDialogWindow->setParent( this );

  • Moderators

    What type of widget does "this" refer to?

  • Oops. My apologies. The main window object also inherits from QWidget. I've put the class header below. It's nothing fancy.

    #include <InterfaceWidgets/gameturnwidget.h>
    #include <InterfaceWidgets/economywidget.h>
    #include <InterfaceWidgets/citydialog.h>
    #include <Helper/graphicshelper.h>
    #include <QHBoxLayout>
    #include <QVBoxLayout>
    #include <QString>
    #include <QPushButton>

    class mainWindow : public QWidget


    gameTurnWidget *gameTurn;
    economyWidget *economyInfo;
    cityDialog *cityDialogWindow;

    QPushButton *quitButton;
    QPushButton *nextTurnButton;
    QPushButton *showCityDialog;
    QPushButton *createButton(QString caption);
    QHBoxLayout *mainLayout;
    graphicsHelper helper;

    public slots:
    void showCityWindow();



  • The usual way is to pass the parent widget in the constructor:

    // change your constructor declaration to

    mainWindow(QWidget *parent = 0);

    // change your constructor defintion to

    mainWindow::mainWindow(QWidget *parent) : QWidget(parent)

    // and construct your object this way:

    cityDialogWindow = new mainWindow(this);

Log in to reply

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