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);
The place for the corner widget is restricted. You use a vertical layout and its contents margins move the buttons down, out of sight.
Use a horizontal layout and set the contents margins to 0
.
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);
The given example produces the following result:
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다