Qt 制表符关闭按钮位置
我想在我的Qt应用程序中设置选项卡的样式,如下所示: 我使用了以下样式表:Qt 制表符关闭按钮位置,qt,qtstylesheets,qtabbar,Qt,Qtstylesheets,Qtabbar,我想在我的Qt应用程序中设置选项卡的样式,如下所示: 我使用了以下样式表: QTabBar{background-color: #fff; border-top: 0px;} QTabBar::tab { border-image: url(:/New_UI/tab_inactive.png) 7 17 7 2; margin-left: 2px; border-right: 17px; border-top: 5px; border-bottom: 5
QTabBar{background-color: #fff; border-top: 0px;}
QTabBar::tab {
border-image: url(:/New_UI/tab_inactive.png) 7 17 7 2;
margin-left: 2px;
border-right: 17px;
border-top: 5px;
border-bottom: 5px;
font: 400 9.2pt "Segoe UI";
color: #ccc;
padding: 0px 13px 0px 5px;
max-height: 26px;
}
QTabBar::tab:selected, QTabBar::tab:hover {
border-image: url(:/New_UI/tab_active.png) 6 17 6 2;
}
QTabBar::close-button {
image: url(:/New_UI/tab_close.png);
subcontrol-origin: padding;
subcontrol-position: right;
width: 13px;
height: 13px;
}
结果如下(关闭按钮的位置不是我想要的):
我做错了什么&我怎样才能得到我想要的结果 编辑:我知道这篇文章很旧,但我希望它能帮助其他人 经过几次测试后,我认为有一种方法可以做到这一点,但它不使用
Qt样式表
:
QTabWidget
进行子类化,以完全访问受保护的功能QWidget
或QPushButton
作为关闭按钮marginright
)tabBar()->setTabButton(index,QTabBar::RightSide,closeButton)代码>
MyTab::MyTab(QWidget *parent) : QTabWidget(parent)
{
/// Create your button
QPushButton *close = new QPushButton(this);
// Add a tab
addTab(new QWidget(), QIcon(), "Tab 1");
setStyleSheet("QTabBar::tab { width : 150px;}");
// Size and move your button
close->setStyleSheet("max-height: 14px; max-width: 15px; margin-right: 50px;");
// Add your button to the tab
tabBar()->setTabButton(0, QTabBar::RightSide, close);
}
最后,在主窗口中,我将自己的TabWidget添加到布局中:
ui->layout->addWidget(new MyTab(this));
结果是:
但是现在您必须通过连接按钮手动处理关闭操作,并获取
removeTab(index)
调用的索引。我正在做与您相同的事情,这是我的样式表:
QTabBar::close-button{
image:url(:tabclose.png);
margin-right:4px;
}
不要使用“width”和“height”属性,这两个属性在这里不起作用,在子控件上设置“image:url()”会隐式设置子控件的宽度和高度(SVG中的图像除外)
使用“右边距”属性控制距选项卡右边缘的距离 这是一个纯样式表解决方案,无需手动创建按钮:
QTabBar::close-button {
image: url(:/tab-close.png);
padding-left: -13px;
}
如果您检查Qt源代码,图像绘制代码只使用填充值,而不使用边距值。添加自定义按钮是一个很好的答案。但是如果您使用边距来确定关闭按钮的位置,关闭按钮的鼠标区域将不正常,因此我在小部件中添加了一个项目和按钮,最后将此小部件添加到TabWidget
void TabBarCloseable::tabInserted(int index)
{
QWidget *widget = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(this);
widget->setLayout(layout);
QToolButton *closeBtn = new QToolButton(this);
layout->addWidget(closeBtn);
layout->insertSpacing(1, 15);
closeBtn->setStyleSheet("max-height: 16px; max-width: 16px;");
this->setTabButton(index, QTabBar::RightSide, widget);
QTabBar::tabInserted(index);
}
你垫错了
顶
屁股
QTabBar::tab {
min-width: 25ex;
padding: 10px 0px 10px 10px;
}
为关闭按钮设置正确的填充是否无效?顺便说一句,这是一个漂亮的选项卡栏。我尝试将填充设置为正确,但如果我将值10px设置为正确填充,它会由于某些原因减小关闭按钮的大小:(增加选项卡栏右侧的边距如何?也不太好,因为关闭按钮不是选项卡本身的子控件,而是选项卡栏的子控件,是否可以移动关闭按钮的位置?我尝试了很多方法,关于填充、边距、边框设置,即使移动也看不到它。这正是谢谢你的回答,正如你所看到的,英语不是我的母语,所以非常感谢你的编辑。有趣的是,我会尝试返回。不,它不起作用,当我进一步增加边距(6px)时,关闭按钮开始缩小,而不是从选项卡的右边缘向左移动
QTabBar::tab {
min-width: 25ex;
padding: 10px 0px 10px 10px;
}