tags:

views:

41

answers:

3

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?

+2  A: 

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.

Brian Roach
Is there a way to call mainWindow from SWindow without making SWindow a child of mainWindow?
Blin
I'm not sure what you mean by "call mainWindow" - that's your classname. What exactly are you trying to do?
Brian Roach
Call an instance of mainWindow wich created an instance of SWindow. I need to call mainWindow's renew() function when clicking SWindow's button.
Blin
That's what signals and slots are for. You need to define some signals that your SWindow will emit (e.g. `void buttonClicked()`). When you instantiate SWindow inside your mainWindow class, you'll connect the signal from SWindow to a slot in mainWindow - `connect(config, SIGNAL(buttonClicked()), this, SLOT(renew()))`. Inside the function in `SWindow` that handles the button click, you do a `emit buttonClicked()`.
Brian Roach
+1  A: 

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().

Leiaz
The problem is why he was trying to do this. He was trying to be able to call `((mainWindow*)parent())->renew()` from inside his `SWindow` class which is horribly bad.
Brian Roach
+1  A: 

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();
}
Will