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.
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.