标签:icon 边框颜色 去除 action 颜色 mys setfont lan 获取
// 重新设定按钮的位置
pBtnTest->move(100, 50);
// 重新设定按钮的大小
pBtnTest->resize(80, 50);
// 设置按钮的位置和大小
pBtnTest->setGeometry(100, 50, 80, 50);
pBtnTest->setFont(QFont("宋体", 18));
pBtnTest->setText("我是一个很长很长很长的文本");
// adjustSize():自动调整控件的大小,以适应其内容;
pBtnTest->adjustSize();
// 设置控件获取焦点
pBtnTest->setFocus();
// 获取控件是否具有焦点;如果控件有焦点,返回 true;
bool b = pBtnTest->hasFocus();
qDebug() << b;
// 清除控件的焦点
pBtnTest->clearFocus();
pBtnTest->setCursor(QCursor(Qt::BusyCursor));
// 禁用控件
pBtnTest->setDisabled(true);
// 启用控件
pBtnTest->setEnabled(true);
pBtnTest->setFlat(true);
pBtnTest->setDefault(true);
// 设置按钮上显示的图标
pBtnTest->setIcon(QIcon(":/Image/Luffy.png"));
// 设置图标的大小
pBtnTest->setIconSize(QSize(24, 24));
auto earMonitorSwitch = new QPushButton(this);
earMonitorSwitch->setCheckable(true);
earMonitorSwitch->setStyleSheet("QPushButton{border-image:url(./resource/audio/audio_setting/btn_earmonitor_close.png);}"
"QPushButton:checked{border-image:url(./resource/audio/audio_setting/btn_earmonitor_open.png);}");
pBtnTest->setStyleSheet("QPushButton{border-image:url(./resource/audio/audio_setting/close_normal.png);border:none;}"
"QPushButton:hover{border-image:url(./resource/audio/audio_setting/close_hover.png);}"
"QPushButton:pressed{border-image:url(./resource/audio/audio_setting/close_press.png);}");
// 定义初始样式集合
QStringList list;
list.append("color:white"); // 前景色
list.append("background-color:rgb(85,170,255)"); // 背景色
list.append("border-style:outset"); // 边框风格
list.append("border-width:5px"); // 边框宽度
list.append("border-color:rgb(10,45,110)"); // 边框颜色
list.append("border-radius:20px"); // 边框倒角
list.append("font:bold 30px"); // 字体
list.append("padding:4px"); // 内边距
// 设置按钮初始样式
pBtnTest->setStyleSheet(list.join(‘;‘));
// 按钮按下时修改样式
list.replace(6, "font:bold 35px");
connect(pBtnTest, &QPushButton::pressed, [=](){
pBtnTest->setStyleSheet(list.join(‘;‘));
});
// 按钮弹起时恢复样式
list.replace(6, "font:bold 30px");
connect(pBtnTest, &QPushButton::released, [=](){
pBtnTest->setStyleSheet(list.join(‘;‘));
});
auto button = new QPushButton(u8"按钮");
button->setContextMenuPolicy(Qt::ActionsContextMenu);
if (type == AccomSoundType::mySound)
{
auto deleteAction = new QAction(left);
deleteAction->setText(u8"删除");
connect(deleteAction, &QAction::triggered, this, [=]() {
trace("删除");
});
button->addAction(deleteAction);
}
QPushButton* btn_room_setting = new QPushButton();
btn_room_setting->setObjectName("btn_room_setting");
auto settingMenu = new QMenu(this);
auto audioSettingAction = new QAction(u8"音频设置");
auto hostSettingAction = new QAction(u8"设置主持人");
auto adminSettingAction = new QAction(u8"设置管理员");
auto blackListAction = new QAction(u8"黑名单");
settingMenu->addAction(audioSettingAction);
settingMenu->addAction(hostSettingAction);
settingMenu->addAction(adminSettingAction);
settingMenu->addAction(blackListAction);
btn_room_setting->setMenu(settingMenu);
上面的方法会让按钮显示一个下拉的三角形图标,如果想取消这个图片可以用 qss 去除:
QPushButton::menu-indicator#btn_room_setting{
image:none;
}
参考:
Qt学习笔记(十五):QPushButton 按钮的常用方法
标签:icon 边框颜色 去除 action 颜色 mys setfont lan 获取
原文地址:https://www.cnblogs.com/linuxAndMcu/p/13354355.html