【发布时间】:2020-08-04 09:21:44
【问题描述】:
我查看了每个线程,但没有一个有效。他们中的一些人在讨论如何从 Qt Designer 中进行操作(我已经知道该怎么做,但它没有用,因为选项卡是动态添加的)。 他们中的一些人讨论如何通过代码来做到这一点,但仍然没有任何效果。
重要的代码在 appendTab() 里面:
TabWidgetCSD::TabWidgetCSD(QWidget *parent)
: QTabWidget(parent)
{
TabBarCSD* bar = new TabBarCSD();
setTabBar(bar);
// create absolute button to the right. it will change the position everytime something happens
// and it will will take the last index
QIcon icon(svgToColorIcon(":/images/plus.svg"));
openTabBtn = new QPushButton(icon, "", this);
openTabBtn->setFlat(true);
openTabBtn->setFixedSize(QSize(29, 29));
openTabBtn->setIconSize(QSize(25, 25));
connect(openTabBtn, &QPushButton::clicked, this, [=, this](){
appendTab();
});
appendTab();
}
void TabWidgetCSD::appendTab()
{
// create page and get it's index
auto l = new QVBoxLayout();
auto mainMenuForm = new MainMenuForm;
l->addWidget(mainMenuForm);
setLayout(l);
auto tabIdx = addTab(mainMenuForm, "");
QIcon icon(svgToColorIcon(":/images/times.svg"));
QLabel* label = new QLabel;
label->setText("Main Window");
label->setAlignment(Qt::AlignCenter);
label->setFixedWidth(175);
auto btn = new QPushButton(icon, "");
btn->setFlat(true);
btn->connect(btn, &QPushButton::clicked, this, [=, this]() {
removeTab(tabIdx);
});
auto hbox = new QHBoxLayout();
hbox->setAlignment(Qt::AlignCenter);
hbox->addWidget(label);
hbox->addWidget(btn);
auto wrapper = new QWidget();
wrapper->setLayout(hbox);
wrapper->setFixedSize(220, 30);
tabBar()->setTabButton(tabIdx, QTabBar::LeftSide, wrapper);
}
【问题讨论】:
标签: c++ qt qwidget qtabwidget