Qt QRadioButton使用,添加图片,去掉前面的圈圈
前言
QRadioButton部件提供了一个带有文本标签的单选框(单选按钮),就外观来说,但是一般都满足用户的需求。如果要做的好看,需要自己添加图标,另外有选中和未选中2中状态,就需要添加2个状态图片。
思路
1.首先确定QRadioButton样式
2.选中和未选中信号处理
步骤
1. qt设计器选中QRadioButton
2. 设置QRadioButton styleSheet,
QRadioButton{
image: url(:/signalGeneratorUiPluginLib/META-INF/images/checkEnable.png);
spacing: 2px;
color: white;
background-origin:content;
background-position:content;
border-width: 0px;border-style: solid;border-color: rgb(85, 255, 255);
font: bold 13px; color:rgb(173,210,228);
}
QRadioButton::indicator {
width: 0px;
height: 0px;
}
其中,QRadioButton{ 第一段是单选按钮基本外观
QRadioButton::indicator { 表示,去掉单选按钮的默认前面的小白色圆圈图标 ,很影响外观
注意资源图片一定要加到工程中,否则qt找不到图片!!!!
3. 添加QRadioButton信号槽
QObject::connect(ui->radioButton_5, SIGNAL(clicked(bool)), this, SLOT(on_emitSignal_clicked()));
4. 槽函数实现
void SignalGeneratorUi::on_emitSignal_clicked()
{
if(ui->radioButton_5->isChecked())
{
// QString strStatus = ui->radioButton->isChecked() ? "Checked" : "Unchecked";
if(emitCan)
{
ui->radioButton_5->setStyleSheet(QString("QRadioButton{"
"image: url(:/signalGeneratorUiPluginLib/META-INF/images/uncheckEnable.png);"
"spacing: 2px;"
"color: white;"
"background-origin:content;"
"background-position:right;"
"border-width: 0px;border-style: solid;border-color: rgb(85, 255, 255);"
"font: bold 13px; color:rgb(173,210,228);}"
"QRadioButton::indicator {"
"width: 0px;"
"height: 0px;}"));
emitCan = false;
}
else {
ui->radioButton_5->setStyleSheet(QString("QRadioButton{"
"image: url(:/signalGeneratorUiPluginLib/META-INF/images/checkEnable.png);"
"spacing: 2px;"
"color: white;"
"background-origin:content;"
"background-position:right;"
"border-width: 0px;border-style: solid;border-color: rgb(85, 255, 255);"
"font: bold 13px; color:rgb(173,210,228);}"
"QRadioButton::indicator {"
"width: 0px;"
"height: 0px;}"));
emitCan = true;
}
}
}