在UI里面引用QTextEdit控件,在对其进行样式设计的时候,通常也会对其滚动条(
horizontalScrollBar、verticalScrollBar) 进行样式设计。
例如我们对QTextEdit 的垂直滚动条进行样式设计,样式表会这样写:
- QTextEdit#textEdit
- {
- background:rgba(0,0,0,0.1);
- color:rgba(255,255,255,0.8);
-
- border-radius:2px;
- border:none;
- font-size:14px;
- font-family:"微软雅黑";
- }
-
- QScrollBar:vertical
- {
- border: none;
- background: transparent;
- width: 6px;
- }
-
- QScrollBar::handle:vertical
- {
- background-color: rgba(255,255,255,1);
- min-height: 30px;
- border-radius:3px;
- width: 6px;
- }
-
- QScrollBar::add-line:vertical
- {
- border: none;
- height: 0px;
- }
-
- QScrollBar::sub-line:vertical
- {
- border: none;
- height: 0px;
- }
-
- QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical
- {
- background:transparent;
- border-radius:3px;
- }
在Qt Designer 里面看是正常的, 如图:
但是当运行程序(Ctrl + R)时,样式表就没有生效,如图:
============================ 解决方案=================================
方案1:在类的构造函数里对滚动条进行polish抛光操作。
- ReadingTimeSettingDialog::ReadingTimeSettingDialog(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::ReadingTimeSettingDialog)
- {
- ui->setupUi(this);
-
- ui->textEdit->verticalScrollBar()->style()->polish(ui->textEdit->verticalScrollBar());
- }
方案2:
(1)将QTextEdit 的原有滚动条隐藏,设置滚动条策略未 ScrollBarAlwaysOff
(2) 添加新控件Vertical Scroll Bar放置在QTextEdit的左侧
(3) 设置verticalScrollBar的样式表
(4)构造函数里面设置verticalScrollBar为QTextEdit的滚动条
- ReadingTimeSettingDialog::ReadingTimeSettingDialog(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::ReadingTimeSettingDialog)
- {
- ui->setupUi(this);
- ui->textEdit->setVerticalScrollBar(ui->verticalScrollBar);
- }