一、按键包含图片与文字,以文字的方向设定。
使用QToolButton按键创建对象
textAndImage::textAndImage(QWidget *parent) :QMainWindow(parent),
ui(new Ui::textAndImage)
{
ui->setupUi(this);
ui->refreshToolButton->setText(tr("刷新"));
ui->refreshToolButton->setIcon(QPixmap(":/image/10IK012614.png"));
ui->refreshToolButton->setIconSize(QPixmap(":/image/10IK012614.png").size());
ui->refreshToolButton->setAutoRaise(true);
ui->refreshToolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
ui->musictoolbutton->setText(tr("音乐"));
ui->musictoolbutton->setIcon(QPixmap(":/image/10IK05Q25.png"));
ui->musictoolbutton->setIconSize(QPixmap(":/image/10IK05Q25.png").size());
ui->musictoolbutton->setAutoRaise(true);
ui->musictoolbutton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
ui->stoptoolbutton->setText(tr("暂停"));
ui->stoptoolbutton->setIcon(QPixmap(":/image/10IK0M92.png"));
ui->stoptoolbutton->setIconSize(QPixmap(":/image/10IK0M92.png").size());
ui->stoptoolbutton->setAutoRaise(true);
ui->stoptoolbutton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
ui->powertoolbutton->setText(tr("电源"));
ui->powertoolbutton->setIcon(QPixmap(":/image/10IK0M28.png"));
ui->powertoolbutton->setIconSize(QPixmap(":/image/10IK0M28.png").size());
ui->powertoolbutton->setAutoRaise(true);
ui->powertoolbutton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
ui->soundtoolbutton->setText(tr("声音"));
ui->soundtoolbutton->setIcon(QPixmap(":/image/10IK05F9.png"));
ui->soundtoolbutton->setIconSize(QPixmap(":/image/10IK05F9.png").size());
ui->soundtoolbutton->setAutoRaise(true);
ui->soundtoolbutton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
}
二、sheetstyle风格的设置。
QString sheetStyle = "QToolButton{background-color:rgb(80,180,200);color:rgb(56, 49, 255);border-radius:12px;border:2px groove gray;border-style:outset;font: 75 12pt Ubuntu;}"\
"QToolButton:hover{background-color:white;color:black;}"\
"QToolButton:pressed{background-color:rgb(85,170,255);border-style:inset;}";
int main(int argc, char *argv[])
{
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QApplication a(argc, argv);
QStyleFactory style;
a.setStyle(style.create(sheetStyle));
a.setStyleSheet(sheetStyle);
textAndImage w;
w.show();
return a.exec();
}
三、鼠标停留显示备注
在qt creator中选中对象,进入其属性设置对话框,双击“toolTip选项”,就可以进行设置了,如图:
四、最后按键的效果图如下: