Qt设置背景色,按钮添加图片,实现三态,label添加图片,设置字体大小,颜色等基本操作

本文介绍了在Qt中为widget设置背景色的两种方法,包括使用调色板,并展示了如何在QPushButton中实现按钮的图片显示及三态功能。此外,还详细讲解了QLabel的基本操作,如添加图片以及设置字体大小和颜色。

一,如何给widget设置背景色

 

方法1:使用调色板

<span style="font-family:Arial;background-color: rgb(255, 255, 255);"></span> 
<span style="font-family:Arial;background-color: rgb(255, 255, 255);">1,设置widget属性:</span>
<span style="font-family:Arial;background-color: rgb(255, 255, 255);"> <span style="color:#000000;">setAutoFillBackground</span><span style="color:#000000;">(</span><span style="color:#80800;">true</span><span style="color:#000000;">);</span></span>
<span style="font-family:Arial;background-color: rgb(255, 255, 255);"></span> 
<span style="font-family:Arial;background-color: rgb(255, 255, 255);">2:,调色板</span>
<span style="color:#80080;">QPalette</span><span style="color:#c0c0c0;"> </span><span style="color:#000000;">palette</span><span style="color:#000000;">;</span><pre style="margin: 0px; text-indent: 0px; -qt-block-indent: 0;"><span style="color:#000000;">palette</span><span style="color:#000000;">.</span><span style="color:#000000;">setColor</span><span style="color:#000000;">(</span><span style="color:#80080;">QPalette</span><span style="color:#000000;">::</span><span style="color:#80080;">Background</span><span style="color:#000000;">,</span><span style="color:#c0c0c0;"> </span><span style="color:#80080;">QColor</span><span style="color:#000000;">(</span><span style="color:#0080;">231</span><span style="color:#000000;">,</span><span style="color:#c0c0c0;"> </span><span style="color:#0080;">231</span><span style="color:#000000;">,</span><span style="color:#c0c0c0;"> </span><span style="color:#0080;">231</span><span style="color:#000000;">));</span>
 
3,给widget设置调色板
setPalette(palette);
 
方法2:重写paintEvent事件函数
 
1,.h文件增加定义
protected:
    void paintEvent(QPaintEvent *event);
 
2,.cpp文件
void testWidget::paintEvent(QPaintEvent *event)
{
    //设置底图
    this->setFixedSize(209, 147);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.drawPixmap(this->rect(), QPixmap(":/addBackGround"));
}
 
二,如何给QPushButton添加图片
 
1,给QPushButton添加图片

QPushButton *pushButton = new QPushButton();

pushButton->setFixedSize(47, 26);
pushButton->setIconSize(QSize(47, 26));
pushButton->setIcon(QIcon(":/pushButton"));
 
2,使用QSS给按钮增加三态
QString styleQSS ="QPushButton:enabled {\
            color: white;\
    font-size: 13px;\
    font-weight: bold;\
    border-image: url(:/pushButton);\
    }\
    QPushButton:!enabled {\
        border-image: url(:/pushButton);\
    }\
    QPushButton:hover:enabled {\
                          border-image: url(:/pushButtonVIHover);\
    }\
    QPushButton:pressed:enabled {\
                            border-image: url(:/pushButtonVIPressed);\
    }\
    QPushButton:checked:enabled {\
                            border-image: url(:/pushButtonVIPressed);\
    }\
    QPushButton::menu-indicator {\
    image: url(:/Common/menuIndicator);\
        subcontrol-position: right center;\
        subcontrol-origin: padding;\
    width: 25px;\
    height: 25px;\
    }";
    pushButton->setStyleSheet(styleQSS);
 

三,QLabel基本设置

 

1,给QLabel添加图片

QLabel *pLabel = new QLabel();

pLabel->setPixmap(QPixmap(":/pushButtonVI"));
 
2,QLabel设置字体大小
 
方法1:
pLabel->setText("Hello");
pLabel->setStyleSheet("font-size : 53px");
 
方法2:
QFont ft;
ft.setPointSize(12);
pLabel->setFont(ft);

 

3,QLabel 设置字体颜色

QPalette pe;
pe.setColor(QPalette::WindowText, Qt::blue);
pLabel->setPalette(pe);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值