PDA

View Full Version : midi child does not close when I call close()



qlands
28th July 2011, 10:21
Hi,

I have a Mdi child with a close button. I connected the signal clicked() of the button to the slot close() of the Mdi child.

But when I click close the button disappears and the window does not close!

The MdiChild is based on a QMainWindow.

Any idea why?!!!

Thanks.
Carlos

FelixB
28th July 2011, 10:49
show the code of the connection. you probably connected wrong objects.

qlands
28th July 2011, 10:57
Hi,



#include "documentwindow.h"
#include "ui_documentwindow.h"

documentWindow::documentWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::documentWindow)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));
}

documentWindow::~documentWindow()
{
}


So when I click in the pushButton the window does not close!

FelixB
28th July 2011, 11:06
Hi,

the code looks good to me. the problem might be related to the mdi-stuff you are doing. can you show the mdi-code?

Felix

qlands
28th July 2011, 12:00
Hi,

the MdiChild is declared as:


#include <QMainWindow>
#include <impgenmaint.h>

namespace Ui {
class documentWindow;
}

class documentWindow : public QMainWindow
{
Q_OBJECT

public:
explicit documentWindow(QWidget *parent = 0);
~documentWindow();

private:
Ui::documentWindow *ui;
};


The implementation is the same I previously posted.

I copied the QT mdi example, thus the code the created the mdi child is:


documentWindow *MainWindow::createMdiChild()
{
documentWindow *child = new documentWindow;
mdiArea->addSubWindow(child);
return child;
}


I call createMdiChild as:


documentWindow *child = createMdiChild();
child->show();


Thanks.
Carlos.

ChrisW67
29th July 2011, 04:16
I find it annoying that closing the widget contained by a QMdiSubWindow does not close the sub window itself, but that's the way it works.

Try connecting to the close() slot of the QMDiSubWindow returned by QMdiArea::addSubWindow(). You will need to give documentWindow a closeMe() signal that is exposed so that you can make the connection in MainWindow::createMdiChild(). Then connect your push button signal to the new closeMe() signal to have it repeated outside documentWindow (yes I mean connect a signal to a signal).



#include <QtGui>
#include <QDebug>

class docWindow: public QMainWindow {
Q_OBJECT

public:
docWindow(QWidget *p = 0): QMainWindow(p) {
QPushButton *b = new QPushButton("Close", this);
connect(b, SIGNAL(clicked()), this, SIGNAL(closeMe()));
setCentralWidget(b);
}
~docWindow() { }

signals:
void closeMe();

};


class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
m_mdiArea = new QMdiArea(this);
setCentralWidget(m_mdiArea);

docWindow *dw = new docWindow(this);
QMdiSubWindow *sw = m_mdiArea->addSubWindow(dw);
connect(dw, SIGNAL(closeMe()), sw, SLOT(close()));
}
private:
QMdiArea *m_mdiArea;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow m;
m.show();

return app.exec();
}
#include "main.moc"

qlands
29th July 2011, 14:29
Thanks... What I did instead of calling this.close() I call this->parentWidget.close() because as you said close will close the widget but not the parent window.

ChrisW67
29th July 2011, 22:25
As long as you never use docWindow without a parent, or check for that circumstance, that will be fine. You could do something like:


public slots:
void closeMe() {
QMdiSubWindows *sw = qobject_cast<QMdiSubWindow*>(parentWidget());
if (sw)
sw->close();
else
close();
}


The app I borrowed from used the window either as an MDI child, a top-level window, or buried in a more complex UI and connected closeMe() accordingly.