Blin
Blin

Reputation: 698

Qt, can't display child widget

I have two widgets defined as follows

class mainWindow : public QWidget
{
    Q_OBJECT
public:
    mainWindow();
    void readConfig();
private:
    SWindow *config;
    QVector <QString> filePath;
    QVector <QLabel*> alias,procStatus;
    QVector <int> delay;
    QGridLayout *mainLayout;
    QVector<QPushButton*> stopButton,restartButton;
    QVector<QProcess*> proc;
    QSignalMapper *stateSignalMapper, *stopSignalMapper, *restartSignalMapper;
public slots:
    void openSettings();
    void startRunning();
    void statusChange(int);
    void stopProc(int);
    void restartProc(int);
    void renew();
};
class SWindow : public QWidget
{
    Q_OBJECT
public:
    SWindow(QWidget *parent=0);
    void readConfig();
    void addLine(int);
private:
    QVector<QPushButton*> selectButton;
    QVector<QLabel*> filePath;
    QVector<QLineEdit*> alias;
    QSignalMapper *selectSignalMapper;
    QVector<QSpinBox*> delay;
    QGridLayout *mainLayout;
public slots:
    void selectFile(int);
    void saveFile();
    void addLineSlot();
};

when i create and display SWindow object from mainWindow like this

void mainWindow::openSettings()
{
    config = new SWindow();
    config->show();
}

everything is ok, but now i need to access the mainWindow from SWindow, and

void mainWindow::openSettings()
{
    config = new SWindow(this);
    config->show();
}

doesn't display SWindow. How can i display SWindow?

How do i call a function on widget close?

Upvotes: 2

Views: 2730

Answers (3)

Leiaz
Leiaz

Reputation: 2917

By default a QWidget isn't a window. If it is not a window and you specify a parent, it will be displayed inside the parent (so in your case it is probably hidden by other widgets inside your mainWindow).

Look at windowFlags() too. Or you could make your SWindow inherit from QDialog, depending on what you use it for.

As for calling a function on widget close : you could reimplement closeEvent().

Upvotes: 2

Will
Will

Reputation: 871

As noted by Leiaz, you can use the windowsFlags flag when you create the widget. It would look like this:

void mainWindow::openSettings()
{
    config = new SWindow(this, Qt::window);
    config->show();
}

To reimplement the closeEvent:

header:

protected:
  virtual void closeEvent ( QCloseEvent * event )

cpp:

 void sWindow::closeEvent(QCloseEvent *event)
 {
     this->parentWidget()->SomeFunction();
     qWidget::closeEvent(event);
 }

However, its probably better to use signal/slots for your case here. Since you said you want to call the parent's renew method on some button click in sWindow, what you want is to EMIT a signal everytime the button is clicked, and connect this signal in the parent with the parent's refresh slot.

void sWindow::sWindow()
{
  ...
  connect(ui.button, SIGNAL(clicked()), this, SLOT(btnClicked()));
}
void sWindow::btnClicked()
{
  // whatever else the button is supposed to do
  emit buttonClicked();
}

and in your parent class

void mainWindow::openSettings()
{
    config = new SWindow(this, Qt::window);
    connect(config, SIGNAL(buttonClicked()), this, SLOT(refresh()));
    config->show();
}

Upvotes: 2

Brian Roach
Brian Roach

Reputation: 76898

When you do config = new SWindow(this); you're setting the parent of config to be the instance of mainWindow.

This means config is no longer a top-level widget, therefore it won't display outside the mainWindow instance (specifically, it would need to be the central widget or inside the mainWindow instance's layout to be displayed).

EDIT: Sorry - I missed your last question; How do i call a function on widget close

You will want to override the QWidget::closeEvent(QCloseEvent *event) method. This gets called when you close a top-level widget. The most practical thing to do is emit() a signal so that another class can handle it having been closed.

Upvotes: 2

Related Questions