checkboxqt样式_QT开发(六⼗四)——QT样式表(三)六、QT样式表实例
1、样式表的使⽤
A、定制前景⾊和背景⾊
设置应⽤程序中的所有QLineEdit组件的背景⾊为×××
qApp->setStyleSheet("QLineEdit { background-color: yellow }");
如果只想要属性⽤于具体某个对话框的QLineEdit及⼦类组件。
myDialog->setStyleSheet("QLineEdit { background-color: yellow }");
如果只想将属性⽤于某个具体的QLineEdit组件,可以使⽤QObject::setObjectName()和ID选择器。
myDialog->setStyleSheet("QLineEdit#nameEdit { background-color: yellow }");
可以直接设置QLieEdit组件的background-color属性,忽略选择器
nameEdit->setStyleSheet("background-color: yellow");
为了确保对⽐效果,应该指定⽂本合适的颜⾊
nameEdit->setStyleSheet("color: blue; background-color: yellow");
修改选中⽂本的颜⾊如下:
nameEdit->setStyleSheet("color: blue;"
"background-color: yellow;"
"selection-color: yellow;"
"selection-background-color: blue;");
B、使⽤动态属性定制样式
为了提⽰⽤户字段是必填的,对这些字段使⽤×××作为背景⾊。要使⽤QT样式表是现实很容易的。⾸先,应该使⽤应⽤程序的样式表:
*[mandatoryField="true"] { background-color: yellow }
这意味着mandatoryField字段设置为true的组件的背景⾊为×××。
对于每个必备字段组件,我们匆忙中简单创建了⼀个mandatoryField属性并设置为true。
QLineEdit*nameEdit = newQLineEdit(this);
nameEdit->setProperty("mandatoryField", true);
QLineEdit*emailEdit = newQLineEdit(this);
emailEdit->setProperty("mandatoryField", true);
QSpinBox*ageSpinBox = newQSpinBox(this);
ageSpinBox->setProperty("mandatoryField", true);
C、使⽤盒⼦模型定制QPushButton
本节我们展⽰如何创建⼀个红⾊的按钮。
QPushButton#evilButton
{
background-color: red;
border-style: outset;
border-width: 2px;
border-radius: 10px;
border-color: beige;
font: bold 14px;
min-width: 10em;
padding: 6px;
}
QPushButton#evilButton:pressed
{
borderboxbackground-color: rgb(224, 0, 0);
border-style: inset;
}
D、定制按钮菜单指⽰器⼦控件
⼦控件可以访问组件的⼦元素。例如,按钮会使⽤QPushButton::setMenu()关联菜单与菜单指⽰器。定制红⾊按钮的菜单指⽰器如下:QPushButton#evilButton::menu-indicator
{
p_w_picpath: url(myindicator.png);
}
默认,菜单指⽰器定位在衬底区域的右下⾓。通过改变subcontrol-position、subcontrol-origin属性可以定位指⽰器。QPushButton::menu-indicator
{
p_w_picpath: url(myindicator.png);
subcontrol-position: right center;
subcontrol-origin: padding;
left: -2px;
}
Myindicator.png的位置在按钮衬底区域的右中⼼。
E、复杂选择器⽰例
设置应⽤程序样式表中QLineEdit⽂本为红⾊。
QLineEdit{ color: red }
然⽽,想要设置⼀个只读QLineEdit的⽂本颜⾊为灰⾊如下:
QLineEdit{ color: red }
QLineEdit[readOnly="true"] { color: gray }
某些情况下,如果要求注册表单中的所有QLineEdit为棕⾊。
QLineEdit{ color: red }
QLineEdit[readOnly="true"] { color: gray }
#registrationDialog QLineEdit { color: brown }
如果要求所有对话框中的QLineEdit为棕⾊。
QLineEdit{ color: red }
QLineEdit[readOnly="true"] { color: gray }
QDialogQLineEdit{ color: brown }
2、定制特殊组件
本节提供使⽤样式表定制特定组件的实例。
(1)、定制QAbstractScrollArea
任何QAbstractScrollArea组件(项视图、QTextEdit、QTextBrowser)的背景都可以使⽤bakcground属性设置。例如,设置带有滚动条滚动的背景图属性如下:
QTextEdit,QListView
{
background-color: white;
background-p_w_picpath: url(draft.png);
background-p_w_upload: scroll;
}
如果设置background-p_w_picpath属性在视⼝中固定。
QTextEdit,QListView
{
background-color: white;
background-p_w_picpath: url(draft.png);
background-p_w_upload: fixed;
}
(2)、定制QCheckBox
进⾏QCheckBox样式设置与QRadioButton样式设置相同。主要区别在于三态的QCheckBox有⼀个模糊态。
QCheckBox
{
spacing: 5px;
}
QCheckBox::indicator
width: 13px;
height: 13px;
}
QCheckBox::indicator:unchecked
{
p_w_picpath: url(:/p_w_picpaths/checkbox_unchecked.png);
}
QCheckBox::indicator:unchecked:hover
{
p_w_picpath: url(:/p_w_picpaths/checkbox_unchecked_hover.png);
}
QCheckBox::indicator:unchecked:pressed
{
p_w_picpath: url(:/p_w_picpaths/checkbox_unchecked_pressed.png);
}
QCheckBox::indicator:checked
{
p_w_picpath: url(:/p_w_picpaths/checkbox_checked.png);
}
QCheckBox::indicator:checked:hover
{
p_w_picpath: url(:/p_w_picpaths/checkbox_checked_hover.png);
}
QCheckBox::indicator:checked:pressed
{
p_w_picpath: url(:/p_w_picpaths/checkbox_checked_pressed.png);
}
QCheckBox::indicator:indeterminate:hover
{
p_w_picpath: url(:/p_w_picpaths/checkbox_indeterminate_hover.png);
}
QCheckBox::indicator:indeterminate:pressed {
p_w_picpath: url(:/p_w_picpaths/checkbox_indeterminate_pressed.png);
(3)、定制组合框QComboBox
QComboBox{
border: 1px solid gray;
border-radius: 3px;
padding: 1px 18px 1px 3px;
min-width: 6em;
}
QComboBox:editable {
background: white;
}
QComboBox:!editable,QComboBox::drop-down:editable { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
}
/* QComboBox gets the "on" state when the popup is open */ QComboBox:!editable:on,QComboBox::drop-down:editable:on { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #D3D3D3, stop: 0.4 #D8D8D8,
stop: 0.5 #DDDDDD, stop: 1.0 #E1E1E1);
}
QComboBox:on { /* shift the text when the popup opens */ padding-top: 3px;
padding-left: 4px;
}
QComboBox::drop-down {
subcontrol-origin: padding;
subcontrol-position: top right;
width: 15px;
border-left-width: 1px;
border-left-color: darkgray;
border-left-style: solid; /* just a single line */
border-top-right-radius: 3px; /* same radius as the QComboBox */

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。