qt中全局设置滚动条样式
阅读数:102 评论数:0
跳转到新版页面分类
C/C++
正文
一、概述
在 Qt 中,如果你希望全局定义滚动条的样式,使得所有的滚动条(如 QScrollBar
、QScrollArea
和其他带有滚动条的控件)都应用相同的样式,可以通过设置应用程序的样式表来实现。
二、使用应用程序样式表
#include <QApplication>
#include <QMainWindow>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// 设置全局滚动条样式
app.setStyleSheet(
"QScrollBar:vertical {"
" border: 1px solid #999999;"
" background: #f0f0f0;"
" width: 15px;"
" margin: 0px 0px 0px 0px;"
"}"
"QScrollBar::handle:vertical {"
" background: #b0b0b0;"
" min-height: 20px;"
"}"
"QScrollBar::add-line:vertical {"
" background: #c0c0c0;"
" height: 20px;"
" subcontrol-position: bottom;"
" subcontrol-origin: margin;"
"}"
"QScrollBar::sub-line:vertical {"
" background: #c0c0c0;"
" height: 20px;"
" subcontrol-position: top;"
" subcontrol-origin: margin;"
"}"
"QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical {"
" border: 2px solid grey;"
" width: 3px;"
" height: 3px;"
" background: white;"
"}"
"QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
" background: none;"
"}"
"QScrollBar:horizontal {"
" border: 1px solid #999999;"
" background: #f0f0f0;"
" height: 15px;"
" margin: 0px 0px 0px 0px;"
"}"
"QScrollBar::handle:horizontal {"
" background: #b0b0b0;"
" min-width: 20px;"
"}"
"QScrollBar::add-line:horizontal {"
" background: #c0c0c0;"
" width: 20px;"
" subcontrol-position: right;"
" subcontrol-origin: margin;"
"}"
"QScrollBar::sub-line:horizontal {"
" background: #c0c0c0;"
" width: 20px;"
" subcontrol-position: left;"
" subcontrol-origin: margin;"
"}"
"QScrollBar::left-arrow:horizontal, QScrollBar::right-arrow:horizontal {"
" border: 2px solid grey;"
" width: 3px;"
" height: 3px;"
" background: white;"
"}"
"QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal {"
" background: none;"
"}"
);
QMainWindow mainWindow;
mainWindow.setWindowTitle("Global ScrollBar Style Example");
QWidget *centralWidget = new QWidget;
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
for (int i = 0; i < 20; ++i) {
layout->addWidget(new QLabel(QString("Label %1").arg(i)));
}
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(centralWidget);
scrollArea->setWidgetResizable(true);
mainWindow.setCentralWidget(scrollArea);
mainWindow.resize(300, 200);
mainWindow.show();
return app.exec();
}
QScrollBar:vertical
:选择垂直滚动条。border
:设置滚动条的边框。background
:设置滚动条的背景色。width
:设置滚动条的宽度。margin
:设置滚动条的边距。QScrollBar::handle:vertical
:选择垂直滚动条的滑块(handle)。min-height
:设置滑块的最小高度。QScrollBar::add-line:vertical
:选择垂直滚动条的向下箭头。height
:设置箭头的高度。subcontrol-position
:设置子控件的位置。subcontrol-origin
:设置子控件的原点。QScrollBar::sub-line:vertical
:选择垂直滚动条的向上箭头。QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical
:选择垂直滚动条的箭头。width
和height
:设置箭头的宽度和高度。QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical
:选择滚动条的空白区域。
相关推荐
一、概述
它提供了一个可滚动的视图,可以用来显示那些不完全适合在当前窗口大小下显示的内容。
二、基本使用方法
1、创建和设置QScrollArea
QScrollArea *scrollArea =