3

I have a QTabWidget and I would like to have two push buttons in the top right corner. I use this code to add a vertical Layout as the corner widget and add two buttons to it:

QWidget* cornerWidget = new QWidget();
QVBoxLayout* vbox = new QVBoxLayout();
QPushButton* button1 = new QPushButton("Button 1");
QPushButton* button2 = new QPushButton("Button 2");

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

ui->myTabWidget->setCornerWidget(cornerWidget);
cornerWidget->setLayout(vbox);
cornerWidget->show();

However when I run my program, no widget shows up in the top right corner at all.

If I use this simplified code to add only one push button, it works flawlessly and shows my button:

QPushButton* button1 = new QPushButton("Button 1")
ui->myTabWidget->setCornerWidget(button1);
scopchanov
  • 7,966
  • 10
  • 40
  • 68
Zciurus
  • 786
  • 4
  • 23

1 Answers1

3

Cause

The place for the corner widget is restricted. You use a vertical layout and its contents margins move the buttons down, out of sight.

Solution

Use a horizontal layout and set the contents margins to 0.

Example

Here is an example I wrote for you to demonstrate how the proposed solution could be implemented:

auto *cornerWidget = new QWidget(this);
auto *hbox = new QHBoxLayout(cornerWidget);
auto *button1 = new QPushButton(tr("Button 1"), this);
auto *button2 = new QPushButton(tr("Button 2"), this);

hbox->addWidget(button1);
hbox->addWidget(button2);
hbox->setContentsMargins(0, 0, 0, 0);

ui->myTabWidget->setCornerWidget(cornerWidget);

Result

The given example produces the following result:

Application window

scopchanov
  • 7,966
  • 10
  • 40
  • 68