QPushbutton设置icon和⽂字(⽂字在图标下⽅)1.使⽤系统⾃带api
QIcon myicon1;
myicon1.addFile(tr(":/image/Resource/new.png"));
ui->new_btn->setIcon(myicon1);
ui->new_btn->setIconSize(ui->new_btn->size());
//ui.btn_calib->setIconSize(QSize(90, 90));
ui->new_btn->setStyleSheet("background-color:rgba(0,0,0,0)");//透明背景
ui->new_btn->setFocusPolicy(Qt::NoFocus);
//设置按钮悬停事件
ui->new_btn->installEventFilter(this);//安装事件过滤器
效果为:
⽆法设置icon和⽂字之间的距离。
2.可使⽤QToolButton实现⽂字在图标下⽅
QIcon myicon9;
myicon9.addFile(tr(":/image/Resource/CAN_open.png"));
ui->openCan_toolButton->setIconSize(QSize(32,32));
ui->openCan_toolButton->setIcon(myicon9);
ui->openCan_toolButton->setText(tr("打开CAN"));
ui->openCan_toolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
ui->openCan_toolButton->setStyleSheet("background-color:rgba(0,0,0,0)");
ui->openCan_toolButton->setFocusPolicy(Qt::NoFocus);
ui->openCan_toolButton->installEventFilter(this);//安装事件过滤器
效果为:
QToolButton与QPushButton并⽆⼤的区别,在常规使⽤⼆者很类似!
3.在pushbutton上⾯使⽤label进⾏布局
QLabel *iconLabel =new QLabel;
QLabel *textLabel =new QLabel;
iconLabel->setFixedSize(32,32);
iconLabel->setStyleSheet("border:1px solid red;");
textLabel->setStyleSheet("border:1px solid red;");
iconLabel->setPixmap(QPixmap(":/images/Setting.png"));
textLabel->setText(QString::fromLocal8Bit("系统设置"));
textLabel->setFixedWidth(60);
QHBoxLayout *myLayout =new QHBoxLayout();
myLayout->addSpacing(10);
myLayout->addWidget(iconLabel);
myLayout->addSpacing(30);
myLayout->addWidget(textLabel);
myLayout->addStretch();
ui->pushButton_2->setLayout(myLayout);
ui->pushButton_2->setStyleSheet("QPushButton{border:1px solid blue;background:white;}"
"QPushButton:hover{border:0px;background:blue;}"
"QPushButton:pressed{border:0px;background:red;}");
ui->test_btn->setStyleSheet("QPushButton{background-color:black;\
color: white;  border-radius: 10px;  border: 2px groove gray;\
border-style: outset;}"
"QPushButton:hover{background-color:white; color: black;}"
icon图标库"QPushButton:pressed{background-color:rgb(85, 170, 255);\
border-style: inset; }");
以上4种⽅法,需要设置,⽐较⿇烦,最好还是UI设计按钮图⽚,这样软件的适配性也⽐较好。

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