7

How can i add two child Widget objects in equal portion of QMainWindow.

MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)

{   TreeArea *ta= new TreeArea(this);
    TreeArea *ta1= new TreeArea(this);
.
.
.
  TreeArea::TreeArea(QWidget *parent) :
 QWidget(parent)
{
.
.
.
NorthCat
  • 9,643
  • 16
  • 47
  • 50
anj
  • 355
  • 2
  • 5
  • 20

3 Answers3

16

As e-zinc suggested you have to use layout. Say you want to insert two widgets into the mainwindow.

QHBoxLayout *layout = new QHBoxLayout;

QPushButton *button1 = new QPushButton("button1");
QPushButton *button2 = new QPushButton("button2");

layout->addWidget(button1);
layout->addWidget(button2);

setCentralWidget(new QWidget);
centralWidget()->setLayout(layout);

This will layout widgets horizontally and you will get this result: QHBoxLayoutExample

And if you want to layout them vertically use QVBoxLayout

I would strongly suggest reading the documentation. Layout Management in Qt

Neox
  • 2,000
  • 13
  • 12
  • I am working on a custom Titlebar, and I think this is the initial way to go: using layout to start putting all widget there – swdev Jun 29 '14 at 15:28
5

Use QMainWindow::setCentralWidget(QWidget *) to add your own control.

NorthCat
  • 9,643
  • 16
  • 47
  • 50
Haiyuan Li
  • 377
  • 2
  • 10
0

////////if you want to create from main.cpp////////

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

    QMainWindow* MainWindow = new QMainWindow(NULL);
    QWidget*     cwidget    = new QWidget(MainWindow);
    QPushButton* button1    = new QPushButton(cwidget);
    QPushButton* button2    = new QPushButton(cwidget);

    button1->setText("Button1");
    button2->setText("Button2");

    button1->move(10, 100);
    button2->move(10, 200);

    MainWindow->setCentralWidget(cwidget);
    MainWindow->resize(400, 300);
    MainWindow->show();

    return app.exec();
}

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

    QMainWindow* MainWindow = new QMainWindow(NULL);
    QWidget*     cwidget    = new QWidget(MainWindow);
    QHBoxLayout* layout     = new QHBoxLayout; //horizontal layout
    QPushButton* button1    = new QPushButton("button1");
    QPushButton* button2    = new QPushButton("button2");

    layout->addWidget(button1);
    layout->addWidget(button2);

    MainWindow->setCentralWidget(cwidget);
    MainWindow->centralWidget()->setLayout(layout); //centralWidget() is getcentralWidget()
    MainWindow->resize(400, 300);

    MainWindow->show();

    return app.exec();
}
#endif