qtcraeter+中qpushbutton的样式
Qt中的QToolButton

Qt中的QToolButtonui->toolButton->setMenu(menu);//设置按钮的弹出菜单ui->toolButton->setPopupMode(QToolButton::DelayedPopup);//设置弹出菜单的⽅式,默认情况下,设置为DelayedPopup(延迟弹出)// QToolButton::DelayedPopup 0 按下按钮⼀定时间后(超时风格依赖,参考:QStyle::SH_ToolButton_PopupDelay),显⽰菜单。
//⼀个典型案例:浏览器中⼯具栏的“后退”按钮// QToolButton::MenuButtonPopup 1 这种模式下,⼯具按钮显⽰⼀个特殊的箭头以指⽰菜单是否存在,按下按钮的箭头部分时显⽰菜单。
// QToolButton::InstantPopup 2 按下⼯具按钮时菜单显⽰,⽆延迟。
这种模式下,按钮⾃⾝的动作不触发。
ui->toolButton->setToolButtonStyle(Qt::ToolButtonIconOnly);#if 0设置按钮风格,只显⽰⼀个图标,⽂本或⽂本位于图标旁边、下⽅。
默认值是Qt::ToolButtonIconOnly。
枚举Qt::ToolButtonStyle:常量值描述Qt::ToolButtonIconOnly 0 只显⽰图标Qt::ToolButtonTextOnly 1 只显⽰⽂本Qt::ToolButtonTextBesideIcon 2 ⽂本显⽰在图标旁边Qt::ToolButtonTextUnderIcon 3 ⽂本显⽰在图标下边Qt::ToolButtonFollowStyle 4 遵循QStyle::StyleHint#endifui->toolButton->setArrowType(Qt::NoArrow);# if 0设置按钮是否显⽰⼀个箭头,⽽不是⼀个正常的图标。
Qt中漂亮的几款QSS

/* === Shared === */QStackedWidget, QLabel, QPushButton, QRadioButton, QCheckBox, QGroupBox, QStatusBar, QToolButton, QComboBox, QDialog {background-color: #222222;color: #BBBBBB;font-family: "Segoe UI";}/* === QWidget === */QWidget:window {background: #222222;color: #BBBBBB;font-family: "Segoe UI";}/* === QToolTip === */QToolTip {background-color: #000000;border: 2px solid #333333;color: yellow;}/* === QPushButton === */QPushButton {border: 1px solid #333333;padding: 4px;min-width: 65px;min-height: 12px;}QPushButton:hover {background-color: #333333;border-color: #444444;}QPushButton:pressed {background-color: #111111;border-color: #333333;color: yellow;}QPushButton:disabled {color: #333333;}/* === Checkable items === */QCheckBox::indicator, QRadioButton::indicator, QTreeView::indicator {width: 16px;height: 16px;background-color: #111111;border: 1px solid #333333;}QRadioButton::indicator {border-radius: 8px;}QCheckBox::indicator::checked, QRadioButton::indicator::checked, QTreeView::indicator ::checked {background-color: qradialgradient(cx:0.5, cy:0.5, fx:0.25, fy:0.15, radius:0.3, stop:0 #B BBBBB, stop:1 #111111);}QCheckBox::indicator:disabled, QRadioButton::indicator:disabled, QTreeView::indicator: disabled {background-color: #444444;}QCheckBox::indicator::checked:disabled, QRadioButton::indicator::checked:disabled, QT reeView::indicator::checked:disabled {background-color: qradialgradient(cx:0.5, cy:0.5, fx:0.25, fy:0.15, radius:0.3, stop:0 #B BBBBB, stop:1 #444444);}/* === QComboBox === */QComboBox {background-color: black;border: 1px solid #333333;color: white;padding:1px 2em 1px 3px;}QComboBox::drop-down {subcontrol-origin: padding;subcontrol-position: top right;border-left: 1px solid #333333;}QComboBox::down-arrow {border: 2px solid #333333; width: 6px;height: 6px;background: #5f5f5f;}/* === QGroupBox === */ QGroupBox {border: 2px solid #333333; margin-top: 2ex;}QGroupBox::title {color: yellow;subcontrol-origin: margin; subcontrol-position: top left; margin-left: 5px;}/* === QTabWidget === */ QTabWidget::pane {background: #222222;border: 2px solid #333333; }/* === QTabBar === */ QTabBar::tab {background: transparent;border: 1px solid #333333; border-bottom: none;color: #BBBBBB;padding-left: 5px;padding-right: 10px;padding-top: 3px;padding-bottom: 3px;}QTabBar::tab:hover {background-color: #333333; border: 1px solid #444444; border-bottom: none;}QTabBar::tab:selected {background-color: #111111;border: 1px solid #333333;border-top: 1px solid yellow;border-bottom: none;color: yellow}/* === QToolBar === */QToolBar {background-color: #222222;border: none;padding: 1px;}QToolBar:handle {background: #222222;border-left: 1px dotted yellow;color: #BBBBBB;}QToolBar::separator {width: 6px;background-color: #222222;}/* === QToolButton === */QToolButton {border: 1px solid #333333;margin: 1px;}QToolButton:hover {background-color: #333333;border: 1px solid #444444;}QToolButton[popupMode="1"] { /* only for MenuButtonPopup */ padding-right: 20px; /* make way for the popup button */}QToolButton::menu-button {border-left: 1px solid #333333;background: transparent;width: 16px;}QToolButton::menu-button:hover {border-left: 1px solid #444444;background: transparent;width: 16px;}QToolButton:checked, QToolButton:pressed { background-color: #111111;color: yellow;}/* === QMenu === */QMenu {background-color: black;border: 1px solid gray;color: white;padding: 1px;}QMenu::item {padding: 2px 25px 2px 20px;border: 1px solid transparent;}QMenu::item:disabled {color: #666666;}QMenu::item:selected {border-color: gray;background: #222222;}QMenu::icon:checked {}QMenu::separator {height: 1px;background: #222222;margin-left: 10px;margin-right: 10px;margin-top: 1px;margin-bottom: 1px;}QMenu::indicator {width: 13px;height: 13px;}/* === QMenuBar === */ QMenuBar {background-color: black;color: white;}QMenuBar::item {background: transparent;}QMenuBar::item:disabled {color: gray;}QMenuBar::item:selected {background: #222222;}QMenuBar::item:pressed {background: #444444;}/* === QScrollBar:vertical === */ QScrollBar:vertical {background: #111111;width: 16px;margin: 16px 0 16px 0;}QScrollBar::handle:vertical {background: #555555;min-height: 16px;}background: #444444;height: 16px;subcontrol-position: bottom;subcontrol-origin: margin;}QScrollBar::sub-line:vertical {background: #444444;height: 16px;subcontrol-position: top;subcontrol-origin: margin;}QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: none;}QScrollBar:up-arrow:vertical, QScrollBar:down-arrow:vertical { border: 2px solid #333333;width: 6px;height: 6px;background: #5f5f5f;}/* === QScrollBar:horizontal === */QScrollBar:horizontal {background: #111111;height: 16px;margin: 0 16px 0 16px;}QScrollBar::handle:horizontal {background: #555555;min-width: 16px;}QScrollBar::add-line:horizontal {background: #444444;width: 16px;subcontrol-position: right;subcontrol-origin: margin;}background: #444444;width: 16px;subcontrol-position: left;subcontrol-origin: margin;}QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal {background: none;}QScrollBar:left-arrow:horizontal, QScrollBar:right-arrow:horizontal {border: 2px solid #333333;width: 6px;height: 6px;background: #5f5f5f;}/* =================== */QLineEdit, QListView, QTreeView, QTableView, QAbstractSpinBox {background-color: black;color: #BBBBBB;border: 1px solid #333333;}QAbstractScrollArea, QLineEdit, QTextEdit, QAbstractSpinBox, QComboBox { border-color: #333333;border: 1px solid #333333;}/* === QHeaderView === */QHeaderView::section {background: #222222;border: 0;color: #BBBBBB;padding: 3px 0 3px 4px;}/* === QListView === */QListView::item:hover {background: #333333;}QListView::item:selected {background: #111111;color: yellow;}/* === QTableView === */QTableView::item:hover {background: #333333;}QTableView::item:hover {background: #111111;color: yellow;}/* === QTreeView === */QTreeView::item {background: black;}QTreeView::item:hover {background: #333333;}QTreeView::item:selected {background: #111111;color: yellow;}QTreeView::branch {}QTreeView::branch:has-siblings:adjoins-item {}QTreeView::branch:has-siblings:!adjoins-item {}QTreeView::branch:closed:has-children:has-siblings { }QTreeView::branch:has-children:!has-siblings:closed {}QTreeView::branch:!has-children:!has-siblings:adjoins-item { }QTreeView::branch:open:has-children:has-siblings {}QTreeView::branch:open:has-children:!has-siblings {}/* === Customizations === */QFrame#infoLabel {border: 1px inset #333333;}2..QWidget {background-color: beige;}QToolBar {background-color: beige;}QDialog, QFileDialog {background-color: beige;}QTabWidget::pane { /* The tab widget frame */border-top: 2px solid #C2C7CB;}QTabWidget::tab-bar {left: 5px; /* move to the right by 5px */}QTabBar, QTabWidget {}QTabBar::tab {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); border: 1px solid darkkhaki;border-bottom-color: #C2C7CB; /* same as the pane color */ border-top-left-radius: 4px;border-top-right-radius: 4px;min-width: 8ex;padding: 2px;}QTabBar::tab:selected, QTabBar::tab:hover {background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #fafafa, stop: 0.4 #f4f4f4,stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);}QTabBar::tab:selected {border-color: #9B9B9B;border-bottom-color: #C2C7CB; /* same as pane color */}QTabBar::tab:!selected {margin-top: 2px; /* make non-selected tabs look smaller */}/* Nice Windows-XP-style password character. */QLineEdit[echoMode="2"] {lineedit-password-character: 9679;}QHeaderView::section {background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,stop:0 #616161, stop: 0.5 #505050,stop: 0.6 #434343, stop:1 #656565); color: white;padding-left: 4px;border: 1px solid #6c6c6c;}QHeaderView::section:checked{}/* We provide a min-width and min-height for push buttonsso that they look elegant regardless of the width of the text. */ QPushButton {background-color: palegoldenrod;border-width: 2px;border-color: darkkhaki;border-style: solid;border-radius: 5;padding: 3px;min-width: 9ex;min-height: 2.5ex;}QPushButton:hover {background-color: khaki;}/* Increase the padding, so the text is shifted when the button is pressed. */QPushButton:pressed {padding-left: 5px;padding-top: 5px;background-color: #d0d67c;}QLabel, QAbstractButton {font: bold;}/* Mark mandatory fields with a brownish color. */.mandatory {color: brown;}/* Bold text on status bar looks awful. */QStatusBar QLabel {font: normal;}QStatusBar::item {border-width: 1;border-color: darkkhaki;border-style: solid;border-radius: 2;}QStackedWidget, QComboBox, QLineEdit, QSpinBox, QTextEdit, QListView, QWebView , QTreeView, QHeaderView {background-color: cornsilk;selection-color: #0a214c;selection-background-color: #C19A6B;}QListView {show-decoration-selected: 1;}QListView::item:hover {background-color: wheat;}/* We reserve 1 pixel space in padding. When we get the focus,we kill the padding and enlarge the border. This makes the itemsglow. */QLineEdit, QFrame {border-width: 1px;padding: 1px;border-style: solid;border-color: darkkhaki;border-radius: 5px;}/* As mentioned above, eliminate the padding and increase the border. */QLineEdit:focus, QFrame:focus {border-width: 3px;padding: 0px;}/* A QLabel is a QFrame */QLabel {border: none;padding: 0;background: none;}/* A QToolTip is a QLabel */QToolTip {border: 2px solid darkkhaki;padding: 5px;border-radius: 3px;opacity: 200;}/* Nice to have the background color change when hovered. */ QRadioButton:hover, QCheckBox:hover {background-color: wheat;}/* Force the dialog's buttons to follow the Windows guidelines. */ QDialogButtonBox {button-layout: 0;}3./*Stylebyevilworks,****************************This file is Public Domain.*//* === Shared === */QStackedWidget, QLabel, QPushButton, QRadioButton, QCheckBox, QGroupBox, QStatusBar, QToolButton, QComboBox, QDialog, QTabBar { font-family: "Segoe UI";background-color: #888;color: #000;}/* === QWidget === */QWidget:window {font-family: 'Segoe UI';background-color: #888;}/* === QPushButton === */QPushButton {border: 1px solid #555;padding: 4px;min-width: 65px;min-height: 12px;}QPushButton:hover {background-color: #999;}QPushButton:pressed {background-color: #333;border-color: #555;color: #AAA;}QPushButton:disabled {color: #333333;}/* === QComboBox === */ QComboBox {background-color: #AAA;border: 1px solid #555;color: black;}QComboBox::drop-down {subcontrol-origin: padding;subcontrol-position: top right; border-left: 1px solid #333333; }/* === QGroupBox === */ QGroupBox {border: 1px solid #555;margin-top: 2ex;}QGroupBox::title {color: black;subcontrol-origin: margin;subcontrol-position: top left; border: 1px solid #555;}/* === QTabBar === */ QTabBar::tab {border-bottom: none;color: #000;padding: 4px;background-color: #888; border: 1px solid #555; }QTabBar::tab:hover {background-color: #AAA; }QTabBar::tab:selected {background-color: #000; color: white;}/* === QTabWidget === */ QTabWidget::pane {background: #888;border: 1px solid #555; }/* === QToolBar === */ QToolBar {background: #949494; border: none;padding-left: 0px;padding-right: 0px;margin: 2px;}QToolBar::separator {width: 1px;margin-left: 3px;margin-right: 3px;background-color: #555; }/* === QToolButton === */ QToolButton {border: 1px solid #666;margin: 1px;}QToolButton:hover {background-color: #AAA;}QToolButton[popupMode="1"] { /* only for MenuButtonPopup */ padding-right: 20px; /* make way for the popup button */}QToolButton::menu-button {border-left: 1px solid #666;background: transparent;width: 16px;}QToolButton::menu-button:hover {border-left: 1px solid #666;background: transparent;width: 16px;}QToolButton:checked, QToolButton:pressed {background-color: #000;border: 1px solid #555;color: white;}/* === QScrollBar:vertical === */QScrollBar:vertical {width: 16px;margin: 16px 0 16px 0;background: #333;}QScrollBar::handle:vertical {background: #888;min-height: 16px;border-top: 1px solid #666;border-bottom: 1px solid #666;}QScrollBar::add-line:vertical {background: #888;height: 16px;subcontrol-position: bottom;subcontrol-origin: margin;}QScrollBar::sub-line:vertical {background: #888;height: 16px;subcontrol-position: top;subcontrol-origin: margin;}QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: none;}/* === QScrollBar:horizontal === */QScrollBar:horizontal {height: 16px;margin: 0 16px 0 16px;background: #333;}QScrollBar::handle:horizontal {background: #888;min-width: 16px;border-left: 1px solid #666;border-right: 1px solid #666;}QScrollBar::add-line:horizontal {background: #888;width: 16px;subcontrol-position: right;subcontrol-origin: margin;}QScrollBar::sub-line:horizontal {background: #888;width: 16px;subcontrol-position: left;subcontrol-origin: margin;}QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal { background: none;}/* === QMenu === */QMenu {background-color: black;border: 1px solid gray;color: white;padding: 1px;}QMenu::item {padding: 2px 25px 2px 20px;border: 1px solid transparent;}QMenu::item:disabled {color: #666666;}QMenu::item:selected {border-color: gray;background: #222222;}QMenu::icon:checked {}QMenu::separator {height: 1px;background: #222222;margin-left: 10px;margin-right: 10px;margin-top: 1px;margin-bottom: 1px;}QMenu::indicator {width: 13px;height: 13px;}/* === QMenuBar === */QMenuBar {background-color: black;color: white;}QMenuBar::item {background: transparent;}QMenuBar::item:disabled {color: gray;}QMenuBar::item:selected {background: #222222;}QMenuBar::item:pressed {background: #444444;}/* =================== */QLineEdit, QListView, QTreeView, QTableView, QAbstractSpinBox {background-color: #AAA;color: #000;border: 1px solid #555;}QAbstractScrollArea, QLineEdit, QTextEdit, QAbstractSpinBox, QComboBox { border: 1px solid #555;}/* === QHeaderView === */QHeaderView::section {height: 20px;}QHeaderView::section {background: #666;border: 0;color: #000;padding-left: 4px;}/* === QListView === */QListView::item:hover {background: #AAA;}QListView::item:selected {background: #333;color: #AAA;}/* === QTableView === */QTableView::item:hover {background: #333333;}QTableView::item:hover {background: #111111;color: yellow;}/* === QTreeView === */QTreeView::item {background: #AAA;}QTreeView::item:hover {background: #CCC;}QTreeView::item:selected {background: #333;color: #AAA;}QTreeView::branch {}QTreeView::branch:has-siblings:adjoins-item { }QTreeView::branch:has-siblings:!adjoins-item {}QTreeView::branch:closed:has-children:has-siblings {}QTreeView::branch:has-children:!has-siblings:closed {}QTreeView::branch:!has-children:!has-siblings:adjoins-item { }QTreeView::branch:open:has-children:has-siblings {}QTreeView::branch:open:has-children:!has-siblings {}作者:江南。
关于Qt中的qss样式表需要注意的坑

关于Qt中的qss样式表需要注意的坑关于QSS要注意的坑。
qss源⾃css,相当于css的⼀个⼦集,主要⽀持的是css2标准,很多⽹上的css3的标准的写法在qss这⾥是不⽣效的,所以不要⼤惊⼩怪。
qss也不是完全⽀持所有的css2,⽐如text-align官⽅⽂档就有说明,只⽀持 QPushButton and QProgressBar,务必看清楚。
有时候偷懒直接来⼀句 *{xxx},你会发现⼤部分是应⽤了,也有⼩部分或者极个别没有应⽤,你可能需要在对应的窗体中 this->setStyleSheet() 来设置。
qss的执⾏是有优先级的,如果没有指定⽗对象,则对所有的应⽤,⽐如在窗体widget中 {color:#ff0000;} 这样会对widget以及widget的所有⼦对象应⽤该样式,这种问题各⼤群每周都有⼈问,你会发现各种奇奇怪怪的异样不正常,怎么办呢,你需要指定类名或者对象名,⽐如 #widget{color:#ff0000;} 这样就只会对widget对象应⽤该样式,另⼀种写法 QWidget#widget{color:#ff0000;},只想对窗体本⾝⽽不是⼦控件按钮标签等 .QWidget{color:#ff0000;} ,具体详细规则参见官⽅说明。
qss整体来说还是可以的,解析速度性能在Qt5⾼版本后期⽐Qt4好很多,尤其是修复了不少qss中的解析绘制BUG。
尽管有这样那样的BUG,怀着包容的⼼对待它。
qss官⽅学习地址1:qss官⽅学习地址2:Qt样式表有多种运⾏机制,主要是考虑到各种需求场景,继承⾃QWidget的类和qApp类都⽀持setStyleSheet⽅法,还可以统⼀将样式表放在⽂件,或者将样式⽂件加⼊到资源⽂件。
⽃⽓:qss内容写得到处都是,哪⾥需要就写在哪⾥,各种控件调⽤ setStyleSheet⽅法传⼊样式表内容,或者直接对应控件⿏标右键弹出菜单选择改变样式表填⼊内容;⽃者:qss内容放在⽂件,读取⽂件内容设置样式表,程序发布的时候带上qss⽂件;⽃师:qss⽂件作为资源⽂件放到qrc⽂件,直接编译到可执⾏⽂件中,防⽌篡改;⽃灵:在qss⽂件中⾃定义⼀些标志充当变量使⽤,读取以后替换对应的变量为颜⾊值,类似动态换肤;⽃王:放在⽂件容易被篡改,集成到可执⾏⽂件不够灵活,⼀旦样式表更新需要重新编译⽂件,如何做到既能只更新样式表⽂件,⼜不需要重新编译可执⾏⽂件,⼜能防⽌被篡改:采⽤rcc命令将资源⽂件编译⽣成⼆进制,只需要替换该⼆进制⽂件即可;⽃皇:继承qstyle类⾃⼰实现完成所有样式接⼝,统⼀整体风格,⼤名⿍⿍的UOS系统默认规则就是如此,不允许⽤样式表,全部painter绘制;。
Qt Creator快速入门 界面外观

第10页/共28页
在设计模式使用样式表
进入设计模式。在界面上点击鼠标右键,在弹出的菜单中选择“改变样式 表”,这时会出现编辑样式表对话框,在其中输入如下代码: QPushButton{ } 注意光标留在第一个大括号后。然后点击上面“添加颜色”选项后面的下拉 箭头,在弹出的列表中选择“background-color”一项。 这时会弹出选择颜色对话框,大家可以随便选择一个颜色,然后点击“确定 按钮”,这时会自动添加代码: QPushButton{ background-color: rgb(0, 85, 255); } 根据选择的颜色不同,rgb()中的参数的数值也会不同。可以看到,在这里设 置样式表不仅很便捷而且很直观,不仅可以设置颜色,还可以使用图片,使 用渐变颜色,或者更改字体。相似的,可以再设置QSlider的背景色。
相似的,有伪状态比没有伪状态优先。如果两个选择符的特殊性相同,则后 面出现的比前面的优先。Qt样式表使用CSS2规范来确定规则的特殊性。
第16页/共28页
层叠
样式表可以被设置在QApplication上,或者父部件上, 或者子部件上。部件有效的样式表是通过部件祖先的样式 表和QApplication上的样式表合并得到的。
第18页/共28页
/****************主界面背景*******************/ QMainWindow{ /*背景图片*/ background-image: url(:/image/beijing01.png); } /****************按钮部件*******************/ QPushButton{ /*背景色*/ background-color: rgba(100, 225, 100, 30); /*边框样式*/ border-style: outset; /*边框宽度为4像素*/ border-width: 4px; /*边框圆角半径*/ border-radius: 10px; /*边框颜色*/ border-color: rgba(255, 225, 255, 30); /*字体*/ font: bold 14px; /*字体颜色*/ color:rgba(0, 0, 0, 100); /*填衬*/ padding: 6px; }
qt toolbutton 用法

qt toolbutton 用法
“Qt ToolButton用法”这句话的意思是关于如何使用Qt框架中的QToolButton类。
QToolButton是Qt框架中用于创建工具栏按钮的类,它提供了创建和管理工具栏按钮的功能。
以下是Qt ToolButton的一些常见用法:
1.创建工具栏按钮:使用QToolButton类创建一个工具栏按钮,可以使用构
造函数或从QToolBar的addAction()方法创建。
2.设置按钮样式:使用QToolButton类的setToolButtonStyle()方法来设置
按钮的样式,可以定制按钮的外观。
3.设置按钮图标:使用QToolButton类的setIcon()方法来设置按钮的图标,
可以显示不同的图标来标识按钮的功能。
4.设置按钮文本:使用QToolButton类的setText()方法来设置按钮的文本,
可以在按钮上显示文本以提供额外的信息。
5.设置按钮状态:使用QToolButton类的setCheckable()方法来设置按钮是
否可被选中,可以实现单选按钮或复选框的功能。
6.连接信号和槽:将QToolButton的信号(如clicked())连接到相应的槽函
数,以便在按钮被点击时执行特定的操作。
这些用法可以帮助开发者在Qt应用程序中创建和管理工具栏按钮,提供用户交互和自定义按钮的功能。
总结来说,“Qt ToolButton用法”指的是如何使用Qt框架中的QToolButton类来创建和管理工具栏按钮的方法和技巧。
通过合理地使用这些方法,可以定制按钮的外观、设置图标和文本、处理用户交互等,以实现所需的用户界面功能。
PyQt5(8)之控件QPushButton

PyQt5控件QPushButton一、QPushButton 简介QPushButton 控件是一个最常用的按钮控件,它继承自QAbstracrtButton,因此QAbstracrtButton的所有函数和方法对QPushButton都适用。
二、构造函数QPushButton可以设置父控件,图标以及显示的文本。
如果创建控件时就能确定这些参数,则可以通过构造函数快捷创建。
QPushButton的构造函数如下:QPushButton(const QIcon &icon, const QString &text, QWidget *parent = nullptr) 创建对象时,请注意对应参数的位置。
Demo1:构造函数的使用程序运行效果如下:程序知识点:➢QPushButton有3个参数,分别为图标对象,文本以及父控件;它们的顺序是根据构造函数来确定的三、控件APIDemo1中的参数和方法对QAbstracrtButton的子类都适用;接下来介绍一下QPushButton控件特有的功能。
1. 菜单(menu)功能通常菜单栏的按钮,都会是一个菜单按钮,点击它们以后会弹出相关的菜单,提供更多的功能供用户选择,比如IDLE的菜单如下:File菜单列表中有不同的项,如:Save、Close、Exit等按钮,当它被按下的时候,直接执行动作,这种项被称为Action;而有的则是Bookmarks按钮,它不直接执行动作,而是弹出另一个菜单,它被称为子菜单。
Demo2:添加一个Action下面的程序演示了:如何添加一个 Aciton 到菜单中。
程序运行效果如下:程序知识点:➢QPushButton 控件添加菜单后,右侧多了一个三角形;当‘菜单’按钮被点击时候,会弹出一个菜单,里面有一个‘关机’的Action➢Action的信号连接到匿名槽函数。
(信号和槽,都理解了吧。
不多说了)➢按钮中添加一个菜单只需要通过setMenu()函数设置,参数为Menu对象➢Menu对象可以使用addAction添加QAction、子菜单和分割线Demo3:添加子菜单添加子菜单就是在一个菜单(Menu)中,再添加一个菜单(Menu):程序运行效果如下:程序知识点:➢程序按创建菜单的方式创建了一个新的菜单对象:sub_menu,接着将其添加到原本的菜单即可完成子菜单的添加Demo4:添加分割线菜单中一般有多个项目,它们可能有明显的分类,我们可以通过添加分割线来让菜单更有条理。
Qt中设置按钮透明的方法

Qt中设置按钮透明的⽅法在Qt中让按钮实现透明的效果,查了好多资料,关于这个其实不是很多,总结下来可以⽤下⾯两种⽅法实现:第⼀种⽅法:使⽤setFlat函数实现按钮透明的效果。
QPushButton *mode =new QPushButton(this);mode->setText("mode");mode->move(200,100);mode->setFlat(true);//就是这句实现按钮透明的效果。
第⼆种⽅法,在design视图下,右键点击按钮-》Change StyleSheet,add color -》选择background-color,之后把最右下⾓的Alpha通道设置为0(其余RGB没有关系),就能实现透明的效果,效果和第⼀种⽅法⼀样。
也可以通过setStyleSheet函数来实现;setStyleSheet("background-color: rgba(0, 0, 0, 0)"); 同样前三个0就是RGB值,可以是别的。
我在实现的过程中,虽然⽤这样的⽅法设置了透明的效果,但是按钮的背景⾊是⿊的,并不是透明的,不能显⽰出背景图⽚,后来就⽤ps做了⼀个和按钮⼤⼩⼀样的png格式的透明图⽚,然后⽤同样的右键,Change styleSheet的⽅式,add background-image,选择了我做的那个透明图⽚,最终透明效果就完全实现了。
我猜测之所以还要这么做,应该是⽗窗⼝使⽤了setStyleSheet的⽅式设置了背景⾊的原因。
如果不⽤这种⽅式设置背景⾊,应该就不需要经过这⼀步了。
(注:已经确认就是这个问题,⽗窗⼝最好不要采⽤setStyleSheet⽅式设置背景⾊。
这种情况下就不需要加那个透明图⽚了。
不过在我实验过程中,这两种⽅法还是有区别的,第⼀种⽅法实现透明后,当按下按钮的时候,能显⽰⼀下按钮的轮廓以及按钮的颜⾊;第⼆种⽅法实现透明后,当按下按钮后,显⽰⼀下按钮的轮廓并且按钮是⿊⾊的,就是点击⼀下屏幕,出现⼀闪⽽过的⿊⾊,想要不出现⿊⾊,就要⽤第⼀种⽅法或者第⼆种⽅法加⼀个透明按钮图(和按钮⼤⼩⼀致的透明图⽚,png格式)。
QT入门-在窗体中添加按钮以及对窗体进行设置

QT⼊门-在窗体中添加按钮以及对窗体进⾏设置1) QT中,按钮类叫做QPushButton。
在mywidget.h的MyWidget类的私有变量中:QPushButton b1; 或者QPushButton* b2;按住F4可以直接跳到.cpp⽂件。
2)按钮初始化:在窗⼝类的构造函数⾥初始化即可。
b2=new QPushButton(…)选中按F1可以查看构造函数以及其参数。
(const QIcon &icon, const QString &text, QWidget *parent = nullptr)//图标以及……(const QString &text, QWidget *parent = nullptr)//按钮上显⽰的内容以及…(QWidget *parent = nullptr)//⽗窗⼝⾄于修改内容的函数,需要从⽗类(QAbstractButton)⾥找。
即void setText(const QString& text);因此找不到相应的成员函数实现其功能的时候,可以从⽗类⾥找⼀下。
b2->setText(“Hello QT”);b2=new QPushButton(“Hello QT”);b2->show();想要的效果是把按钮显⽰到窗⼝⾥(按钮实际上也是窗⼝?),但这时候按钮是独⽴显⽰出来的。
如果窗⼝需要依附于另⼀个窗⼝,就应该给该窗⼝指定⽗类,这可以在构造函数内实现。
因为此处实例化按钮是在MyWidget构造函数内实现的,所以⽗类指针是this,即b2=new QPushButton(“Hello QT”,this);或者:QPushButton b1; b1.setParent(this);注意,如果指定了⽗亲的话,就不⽤调⽤show函数了,因为直接在⽗类窗⼝显⽰了。
3)两个按钮默认在同⼀个位置的话会发⽣覆盖,需要改变按钮的位置,相当于移动。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
在 Qt Creator 中,要修改 `QPushButton` 的样式,你可以使用 Qt 的样式表(StyleSheet)。
Qt 的样式表类似于网页的 CSS,允许你定制 Qt 控件的外观。
以下是如何为 `QPushButton` 设置样式表的步骤:
1. 打开 Qt Creator 并加载你的项目。
2. 在项目的 UI 编辑器中,找到你的 `QPushButton`。
3. 选择该按钮。
4. 在属性编辑器中,找到 `styleSheet` 属性。
5. 在 `styleSheet` 属性中,输入你想要的样式。
例如,要设置按钮的颜色、字体和边框,你可以使用以下样式表:
```css
QPushButton {
background-color: #FF0000; /* 红色背景 */
border: 2px solid #00FF00; /* 绿色边框 */
font-size: 16px; /* 字体大小 */
}
```
6. 保存并重新运行你的项目。
你应该能够看到修改后的按钮样式。
注意:Qt 的样式表功能非常强大,你可以定制几乎所有的外观属性,包括颜色、字体、边框、圆角等。
你可以在 Qt 的官方文档
中查找更多关于样式表的详细信息。