PDA

View Full Version : How I can add subwindows to QMdiArea by Clicking a QPushButton



mrondon
14th June 2010, 01:53
Hi to all!!


How I can add subwindows to a QMdiArea by clicking a QPushButton

Any help will be appreciated!!!!

Thanks.

Ginsengelf
14th June 2010, 06:54
Hi, make a slot, connect pushbutton's clicked() signal to it and call addSubWindow().

Ginsengelf

mrondon
14th June 2010, 19:40
Thanks!

ReqFormDet *MainWindow::sLoadReqFormDet()
{
ReqFormDet *reqfd = new ReqFormDet;
ui->mdiArea->addSubWindow(reqfd);
ui->mdiArea->DontMaximizeSubWindowOnActivation;
reqfd->show();
reqfd->activateWindow();
return reqfd;
}

void SearchForm::onDoubleClickOpen()
{
MainWindow *w = new MainWindow;
w->sLoadReqFormDet();
}

connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(onDoubleClickOpen()));

this is my code but it does not show the windows on mdiArea. It only work by clicking on QAction Button on main toolbar

Could you show me how to do it through a example?

ChrisW67
15th June 2010, 03:43
The code snippet you posted won't compile so it is not clear you have a running program. You should post code that compiles and use code tags. You need to think about what should happen and where: do you really want to create a new main window every time you click the button?


#include <QtCore>
#include <QtGui>

class SubWindowWidget: public QLabel {
public:
SubWindowWidget() {
setText("I'm a sub-window");
}
};

class MainWindow: public QMainWindow {
Q_OBJECT

public:
MainWindow() {
QWidget *w = new QWidget(this);
QPushButton *p = new QPushButton("New Sub-window", this);
mdi = new QMdiArea(this);

QVBoxLayout *l = new QVBoxLayout(w);
l->addWidget(p);
l->addWidget(mdi);
w->setLayout(l);
setCentralWidget(w);

connect(p, SIGNAL(clicked()), this, SLOT(addSubWin()));
}
private:
QMdiArea *mdi;

private slots:
void addSubWin() {
SubWindowWidget *sw = new SubWindowWidget;
mdi->addSubWindow(sw);
sw->show();
}
};

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

MainWindow mw;
mw.show();
app.exec();
}
#include "main.moc"

agathiyaa
15th June 2010, 04:25
This should fix your problem.




QMdiSubWindow* pSubWindow=ui->mdiArea->addSubWindow(reqfd);
if(pSubWindow)
pSubWindow->show();



Note: ReqFormDet *reqfd = new ReqFormDet; is set as internal widget of QMdiSubWindow when you call mdiArea->addSubWindow(reqfd);

mrondon
15th June 2010, 21:32
Thanks great samples!!