goji
goji

Reputation: 7092

Qt - Why won't my widgets line up using a vertical layout inside a splitter?

As per the screenshot below, why won't my widgets line up using a vertical layout inside a splitter?

Window screenshot

/********************************************************************************
** Form generated from reading UI file 'test.ui'
**
** Created: Tue Apr 12 16:51:51 2011
**      by: Qt User Interface Compiler version 4.6.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_TEST_H
#define UI_TEST_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLineEdit>
#include <QtGui/QListWidget>
#include <QtGui/QMainWindow>
#include <QtGui/QSplitter>
#include <QtGui/QTreeWidget>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralwidget;
    QGridLayout *gridLayout;
    QSplitter *splitter;
    QTreeWidget *treeWidget;
    QWidget *widget;
    QVBoxLayout *verticalLayout;
    QLineEdit *lineEdit;
    QListWidget *listWidget;

    void setupUi(QMainWindow *MainWindow)
    {
    if (MainWindow->objectName().isEmpty())
        MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    MainWindow->resize(800, 600);
    centralwidget = new QWidget(MainWindow);
    centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    gridLayout = new QGridLayout(centralwidget);
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    splitter = new QSplitter(centralwidget);
    splitter->setObjectName(QString::fromUtf8("splitter"));
    splitter->setOrientation(Qt::Horizontal);
    treeWidget = new QTreeWidget(splitter);
    QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem();
    __qtreewidgetitem->setText(0, QString::fromUtf8("1"));
    treeWidget->setHeaderItem(__qtreewidgetitem);
    treeWidget->setObjectName(QString::fromUtf8("treeWidget"));
    splitter->addWidget(treeWidget);
    widget = new QWidget(splitter);
    widget->setObjectName(QString::fromUtf8("widget"));
    verticalLayout = new QVBoxLayout(widget);
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    verticalLayout->setContentsMargins(0, 0, 0, 0);
    lineEdit = new QLineEdit(widget);
    lineEdit->setObjectName(QString::fromUtf8("lineEdit"));

    verticalLayout->addWidget(lineEdit);

    listWidget = new QListWidget(widget);
    listWidget->setObjectName(QString::fromUtf8("listWidget"));

    verticalLayout->addWidget(listWidget);

    splitter->addWidget(widget);

    gridLayout->addWidget(splitter, 0, 0, 1, 1);

    MainWindow->setCentralWidget(centralwidget);

    retranslateUi(MainWindow);

    QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
    MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_TEST_H

This is just a dummy ui uploaded here for the purpose of the question. In my real ui design I've tried every imaginable combination, but as soon as I start using splitters, it all goes out of alignment.

Upvotes: 0

Views: 2537

Answers (5)

milianw
milianw

Reputation: 5326

make sure verticalLayout has no margin (which it has by default).

verticalLayout->setContentsMargins(0, 0, 0, 0);

Upvotes: 0

Fred
Fred

Reputation: 4954

The widget on the right side of the splitter (the one containing the QLineEdit and the QListWidget) probably has default values for the layout. In QtCreator, select the QWidget, then in the property editor, scroll all the way down to the Layout section, and set the 4 values for layoutLeftMargin, layoutTopMargin, layoutRightMargin and layoutBottomMargin to 0.

Upvotes: 0

JCooper
JCooper

Reputation: 6525

I pulled up designer and made your layout without any trouble at all. As shown in the hierarchy in the top right of my screenshot, I created a line edit and a text edit. Joined those in a vertical layout. Created a treeWidget and joined that with the layout in a horizontal splitter.

I was able to get it to look like yours by editing the vertical layout properties so that there was a nonzero layout margin. But it looks like the generated code is explicitly setting that to zero in yours...

Widgets line up here

Upvotes: 1

Judge Maygarden
Judge Maygarden

Reputation: 27573

Make sure that the tree widget and the widget with the vertical layout both have the same size policy (e.g. QSizePolicy::MinimumExpanding).

Upvotes: 0

Patrice Bernassola
Patrice Bernassola

Reputation: 14416

You are creating layout but never assign them.

If you want your widgets to be vertical aligned in your splitter, you need to set a vertical layout to the splitter and then add all your sub-widgets to the layout.

Upvotes: 0

Related Questions