QTabwidget';s选项卡文本对齐不工作
我试图在左侧显示打开的选项卡文本。我在样式表中写了这个。但它不起作用 这是我使用的样式表QTabwidget';s选项卡文本对齐不工作,qt,qtabwidget,Qt,Qtabwidget,我试图在左侧显示打开的选项卡文本。我在样式表中写了这个。但它不起作用 这是我使用的样式表 QTabWidget QTabBar{ background-color: #373738; height: 30px; } QTabWidget QTabBar::tab{ text-align: left; background-color: #136ba2; border-style: 1px rgb(67, 67, 67); height: 30px
QTabWidget QTabBar{
background-color: #373738;
height: 30px;
}
QTabWidget QTabBar::tab{
text-align: left;
background-color: #136ba2;
border-style: 1px rgb(67, 67, 67);
height: 30px;
width: 130px;
color: #136ba2;
padding: 0px 5px 0px 5px;
}
QTabWidget QTabBar::tab:selected{
background-color: #5A5B5C;
font-weight: bold;
color: #ffffff;
}
QTabWidget QTabBar::tab:!selected{
background-color:#353536;
color: #B4B4B4;
}
QTabWidget QTabBar::tab:hover{
background-color: #5A5B5C;
color: #ffffff;
}
我认为样式表是不可能的,根据文档: 文本对齐[…]当前仅QPushButton和QProgressBar支持此属性
这可以通过使用tab按钮小部件显示选项卡文本来完成 有一个带有左对齐文本的标签:
QLabel * button = new QLabel("text");
button->setAlignment(Qt::AlignLeft);
如有必要,将选项卡文本设置为空字符串:
ui->tabWidget->tabBar()->setTabText(index, "");
将标签设置为选项卡按钮小部件:
ui->tabWidget->tabBar()->setTabButton(index, QTabBar::LeftSide, button);
您仍然可以设置标签样式,在选项卡小部件样式表中添加QLabel条目:
QLabel{ color: white; }
这里唯一的问题是:无法使用CSS根据选择设置标签文本的样式(即使所选选项卡文本加粗)。但是仍然可以捕获选项卡小部件currentChanged
信号:
void Form::on_tabWidget_currentChanged(int index)
{
for(int i=0; i<ui->tabWidget->tabBar()->count(); i++)
{
QWidget * button = ui->tabWidget->tabBar()->tabButton(i, QTabBar::LeftSide);
if(button != nullptr)
{
QFont font = button->font();
font.setBold(i == index);
button->setFont(font);
}
}
}
void Form::on\u tabWidget\u currentChanged(int index)
{
对于(inti=0;itabWidget->tabBar()->count();i++)
{
QWidget*button=ui->tabWidget->tabBar()->tabButton(i,QTabBar::LeftSide);
如果(按钮!=nullptr)
{
QFont font=按钮->字体();
font.setBold(i==索引);
按钮->设置字体(字体);
}
}
}
有没有其他方法可以实现这一点?其他人在我看来没有任何答案,似乎没有什么比处理自己的文字绘画更直接的了。您可以检查此问题的答案: