QPushButton
一、简述
- 按钮,界面中常用的控件,默认矩形。
- QPushButton的子类:QCommandLinkButton
- 父类:QAbstractButton。QAbstractButton的父类是QWidget
- 同样继承自QAbstractButton的还有QCheckBox, QPushButton, QRadioButton, and QToolButton
- 常用信号:左键点击(clicked)
二、常用功能
2.1名称匹配信号槽(vs+qt自动关联)
- 返回值:void
- 命名规则:on+ui文件pushbotton控件名称+clicked()
- 示例:void on_pushbutton_clicked();
2.2外形相关
2.2.1长宽(大小)
设置按钮长宽两种方法,具体方法如下代码示例
1 //方法1 2 ui.btn_search->resize(50, 80); 3 4 //方法2 5 QSize btnSize; 6 btnSize.setWidth(100); 7 btnSize.setHeight(40); 8 ui.btn_insert->resize(btnSize);
2.2.2矩形直角弧度(设置成为圆形/椭圆)
通过setStyleSheet接口,通过qss控制
1 //border-radius:11px 2 //border-radius:角弧度 3 //11px:半径11px 4 5 ui.btn_search->setStyleSheet("QPushButton{background:rgb(225,226,226);border-radius:11px;}");
2.2.3背景颜色
1 //颜色名称设置:blue 2 ui.btn_search->setStyleSheet("QPushButton{background:blue;border-radius:11px;}"); 3 4 //rgb颜色设置 5 //最后一个透明度,透明度0-255值越小透明度越大 6 ui.btn_insert->setStyleSheet("QPushButton{background:rgb(50,20,50,20);border-radius:11px;}");
2.2.4边框线
边框线类型:
dashed
|dot-dash
|dot-dot-dash
|dotted
|double
|groove
|inset
|outset
|ridge
|solid
|none
1 //border:边框线 2 //2px:粗2px 3 //线类型:slid 4 //线颜色:#000000 5 ui.btn_search->setStyleSheet("QPushButton{background:grey;border-radius:11px;border:2px solid #000000;}"); 6 7 ui.btn_insert->setStyleSheet("QPushButton{background:rgb(50,20,50,20);border-radius:11px;border:5px double #000000;}");
2.2.5鼠标悬浮时颜色
1 2 //鼠标移动到button上变化:QPushButton:hover 3 //背景:background-color 4 ui.btn_search->setStyleSheet("QPushButton{background:grey;border-radius:11px;border:2px solid #000000;}""QPushButton:hover{background-color: #FFFF0000;}"); 5 6 ui.btn_insert->setStyleSheet("QPushButton{background:rgb(50,20,50,20);border-radius:11px;border:5px double #000000;}QPushButton:hover{background-color: #19a91c;}");
2.3右键菜单
1 QMenu *pmenu = new QMenu(this); 2 pmenu->setWindowFlags(pmenu->windowFlags() | Qt::FramelessWindowHint); 3 //menu->setAttribute(Qt::WA_TranslucentBackground); 4 //pmenu->setObjectName(QStringLiteral("")); 5 6 QAction *mp_delet = pmenu->addAction(QStringLiteral("删除数据")); 7 QAction *mp_export = pmenu->addAction(QStringLiteral("导出数据")); 8 9 10 connect(mp_delet, SIGNAL(triggered()), this, SLOT(on_action_delet_triggered())); 11 connect(mp_export, SIGNAL(triggered()), this, SLOT(on_action_export_triggered())); 12 //影藏三角形,但依旧会占右侧空间,字体不居中 13 ui.btn_operation->setStyleSheet("QPushButton:menu-indicator{image:none;}"); 14 ui.btn_operation->setCheckable(true); 15 ui.btn_operation->setMenu(pmenu);
2.4左键下拉菜单(类似QComboBox)
1 QMenu* pMenu = ui.btn_search->menu(); 2 if (!pMenu) 3 { 4 pMenu = new QMenu(this); 5 ui.btn_search->setMenu(pMenu); 6 } 7 QAction* action = new QAction(); 8 action->setText(QString::fromLocal8Bit("选项1")); 9 10 //下拉菜单选择以后会有对勾勾选 11 action->setCheckable(true); 12 pMenu->addAction(action);
三、相关参考
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。