KelvinS
KelvinS

Reputation: 3071

How can I set a layout to a tab in QTabWidget?

I have created a QTabWidget (with no tabs) using the Qt Design tool, and now I need to create tabs and add widgets dynamically.

I can create the tab and add the widget using the following code:

MyCustomWidget *myCustomWidget = new MyCustomWidget(this);
ui->myTabWidget->addTab(myCustomWidget, "New Tab");

The problem is that the widget stays in the top left corner of my QTabWidget but I need to align it in the center (horizontally) of my QTabWidget.

How can I set a horizontal layout to this new tab created?

Note 1: My widget (MyCustomWidget) has a fixed size.

Note 2: I'm using Qt 5.3.

Upvotes: 3

Views: 20934

Answers (5)

Philippe Bourrez
Philippe Bourrez

Reputation: 111

You can use QT designer. Select QTabWidget and have the Tab that needs its layout changed to visible (and it has to contain elements e.g. QLabel, etc..) Select the QTabWidget-> Right-click->Layout->Layout vertically The problem is that it is more intuitive to do it on the Qwidget while you have to do it on the QTabWidget

Upvotes: 11

Dong He Zheng
Dong He Zheng

Reputation: 129

I use QT Creator 4.7.0 Based on QT 5.11.1. I can't change the layout of a tab of QTabWidget using QT designer. The all items from Lay out are disabled. I am very strange. Only method I solve this problem is to edit ui file(in my case, mainwindow.ui file). From:

<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Insert Object</string>
</attribute>
</widget>

To:

<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Insert Object</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
</layout>
</widget>

As result, the layout of tab of QTabWidget have changed. Thanks.

Upvotes: 2

ymoreau
ymoreau

Reputation: 3996

You do not need a QFrame, a simple QWidget and a centered layout will do it :

auto w = new QWidget;

auto txtEdit = new QTextEdit; // Replace with whatever custom widget
txtEdit->setFixedSize(150, 150); // Only here to simulate your fixed size

auto layout = new QVBoxLayout;
layout->addWidget(txtEdit, 0, Qt::AlignHCenter);
w->setLayout(layout);

ui->tabWidget->addTab(w, "MyTab");

Upvotes: 3

KelvinS
KelvinS

Reputation: 3071

Following @Satus idea, I create a new QFrame, set the minimum size as the size of myTabWidget, set a horizontal layout and add myCustomWidget inside it, adding two QSpacerItem (left and right):

QFrame *frame= new QFrame(this);
frame->setMinimumWidth(ui->myTabWidget->width());
frame->setMinimumHeight(ui->myTabWidget->height());

MyCustomWidget *myCustomWidget = new MyCustomWidget(frame);

QHBoxLayout *hLayout = new QHBoxLayout();

hLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding));
hLayout->addWidget(myCustomWidget);
hLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding));

frame->setLayout(hLayout);

ui->myTabWidget->addTab(frame, "New Tab");

It worked, but if someone has a more elegant way to do so, please share with us.

Upvotes: 1

Rinat Veliakhmedov
Rinat Veliakhmedov

Reputation: 1039

It will always be in the top left corner. You can either add another widget on top of yours and them put your widget in the middle of that widget, or make your widget the size of the tab and position its content in the middle. You can use setLayout method of QWidget to set your widget layout.

Upvotes: 2

Related Questions