Search code examples
c++qtqt5qsplitterqsizepolicy

Qt5 - Resizing of QSplitter with QSizePolicy


I want to create a Widget with 3 sections: The mainwindow contains a horizontal QSplitter which contains a vertical QSplitter. But I didn't get that far since the first splitter doesn't maximize in the Window. I read the documentation and still can't figure it out.

MainWindow::MainWindow() {
    setWindowTitle(ProgramVars::getName() + " - " + ProgramVars::getVersion());
    setWindowIcon(ProgramVars::getIcon());

    content = new QSplitter(this);
    content->setLineWidth(2);
    content->setMidLineWidth(2);

    QPixmap pixmap;
    pixmap.load("../res/icon.png"); //just for testing
    l1 = new QLabel;
    l2 = new QLabel;
    l1->setPixmap(pixmap);
    l2->setPixmap(pixmap);

    content->addWidget(l1);
    content->addWidget(l2);

    content->setAutoFillBackground(true);
    QPalette palette(QPalette::Background, Qt::red); //making size visible
    content->setPalette(palette);

    content->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
    content->setMaximumSize(size());
}

(I think that adding the complete source files is not necessary but I will add them if you want.)


Solution

  • I just found the answer for my questions:

    1st

    I have to add subwidgets to the layout provided by QMainWindow and not directly as a child.

    MainWindow::MainWindow() {
        content = new QSplitter();
        content->setLineWidth(2);
        content->setMidLineWidth(2);
    
        **setCentralWidget(content);**
        content->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
    }
    

    I shortened the code since the rest is not important.

    2nd

    And this post answered my second one.