I think you should prefer using a signal. Here is one of many ways to organise this:
#include <QtGui>
{
Q_OBJECT
public:
connect(this, SIGNAL(clicked()), SLOT(doClicked()));
}
private slots:
void doClicked() {
emit addAnotherMdiChild(edit);
}
signals:
void addAnotherMdiChild
(QWidget *child
);
};
{
Q_OBJECT
public:
m_area = new QMdiArea(this);
setCentralWidget(m_area);
PushButton *button = new PushButton(this);
button->setText("Add another widget");
connect(button,
SIGNAL(addAnotherMdiChild
(QWidget *)),
SLOT(doAddAnotherMdiChild
(QWidget *)));
doAddAnotherMdiChild(button);
}
public slots:
void doAddAnotherMdiChild
(QWidget *child
) { (void) m_area->addSubWindow(child);
child->show();
}
private:
QMdiArea *m_area;
};
int main(int argc, char **argv) {
MainWindow w;
w.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
class PushButton: public QPushButton
{
Q_OBJECT
public:
PushButton(QWidget *p = 0): QPushButton(p) {
connect(this, SIGNAL(clicked()), SLOT(doClicked()));
}
private slots:
void doClicked() {
QTextEdit *edit = new QTextEdit(this); // could be any widget
emit addAnotherMdiChild(edit);
}
signals:
void addAnotherMdiChild(QWidget *child);
};
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
m_area = new QMdiArea(this);
setCentralWidget(m_area);
PushButton *button = new PushButton(this);
button->setText("Add another widget");
connect(button, SIGNAL(addAnotherMdiChild(QWidget *)), SLOT(doAddAnotherMdiChild(QWidget *)));
doAddAnotherMdiChild(button);
}
public slots:
void doAddAnotherMdiChild(QWidget *child) {
(void) m_area->addSubWindow(child);
child->show();
}
private:
QMdiArea *m_area;
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks