Qt cannot 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 show the SWindow object from mainWindow like this

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

      

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

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

      

does not display SWindow. How can I display SWindow?

How to call the close function of the widget?

+2


a source to share


3 answers


By default, QWidget is not a window . If it is not a window and you specify a parent, it will appear inside the parent (so in your case, it is probably hidden by other widgets inside your main window).

Have a look at windowFlags () . Or you can do your own SWindow inheritance from QDialog, depending on what you are using it for.



As for calling a function when the widget is closed: you can override closeEvent () .

+2


a source


When you execute config = new SWindow(this);

, you are setting the parent config

as an instance mainWindow

.

This means it is config

no longer a top-level widget, so it will not render outside of the instance mainWindow

(in particular, it must be the center widget or inside the instance's layout mainWindow

).



EDIT: Sorry, I missed your last question; How to call the close function of the widget

You want to override the method QWidget::closeEvent(QCloseEvent *event)

. This is called when you close the top level widget. The most practical is to emit()

signal that another class can handle it by closing it.

+2


a source


As Leiaz pointed out, you can use the windowsFlags flag when creating a widget. It will look like this:

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

      

To reimplement closeEvent:

Title:

protected:
  virtual void closeEvent ( QCloseEvent * event )

      

caste:

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

      

However, it is probably better to use signal / slots for your case here. Since you said you want to call the parent update method on some button, click the sWindow button, what you want is an EMIT signal every time the button is clicked, and connect that signal to the parent device with the parent update 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 the parent class

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

      

+2


a source







All Articles