窗体部件之自定义PushButton

本文介绍了一种使用C++和Qt自定义QPushButton样式的方法,通过不同状态加载对应的图片实现按钮的视觉效果变化,并提供了另一种利用样式表实现类似效果的方案。

pushbutton.cpp

#include "pushbutton.h"

#include <QPainter>
#include <QMouseEvent>
#include <QFontMetrics>
#include <QLabel>

PushButton::PushButton(QString normal, QString hover, QString pressed, QWidget *parent) :
    QPushButton(parent)
{
    buttonState = Normal;

    normalPixmap.load(normal);
    hoverPixmap.load(hover);
    pressPixmap.load(pressed);

    this->setFixedSize(normalPixmap.size());

    this->setContentsMargins(0, 0, 0, 0);
}

PushButton::PushButton(QString background, QWidget *parent) :
    QPushButton(parent)
{
    buttonState = Normal;

    normalPixmap.load(background);
    hoverPixmap.load(background);
    pressPixmap.load(background);

    this->setFixedSize(normalPixmap.size());

    this->setContentsMargins(0, 0, 0, 0);
}

void PushButton::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    switch(buttonState)
    {
    case Normal:
        painter.drawPixmap(this->rect(), normalPixmap);
        break;
    case Hover:
        painter.drawPixmap(this->rect(), hoverPixmap);
        break;
    case Pressed:
        painter.drawPixmap(this->rect(), pressPixmap);
    }

    painter.drawText(this->rect(), Qt::AlignCenter, this->text());
}

void PushButton::enterEvent(QEvent *)
{
    buttonState = Hover;
    update();
}

void PushButton::leaveEvent(QEvent *)
{
    buttonState = Normal;
    update();
}

void PushButton::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        buttonState = Pressed;
        update();
    }
}

bool isOnPushButton(const QPoint &point, const PushButton *pushButton)
{
    if(point.x() < 0 || point.x() > pushButton->width() ||
            point.y() < 0 || point.y() > pushButton->height())
    {
        return false;
    }
    return true;
}

void PushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        //判断鼠标抬起时是否在PushButton之上
        if(isOnPushButton(e->pos(), this))
        {
            emit clicked();
        }

        buttonState = Hover;
        update();
    }
}

另外一种用图片组分割的方法设置样式表
这里写图片描述

QPushButton
{
border-image: url(:/images/9d.png) 0 354 0 0;
}
QPushButton:hover
{
border-image:url(:/images/9d.png) 0 236 0 118;
}
QPushButton:pressed
{
border-image:url(:/images/9d.png) 0 118 0 236;
}

当然可以,PySide6 窗体可以更换样式。你可以通过多种方式来实现窗体样式的更换,以下是几种常见的方法: 1. **使用 Qt Style Sheets**: Qt Style Sheets 类似于 CSS,可以用来设置窗口部件的样式。你可以定义样式表并将其应用到整个应用程序或特定的窗口部件上。 ```python from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton app = QApplication([]) window = QWidget() layout = QVBoxLayout(window) button = QPushButton("Click Me") button.setStyleSheet("background-color: blue; color: white;") layout.addWidget(button) window.show() app.exec() ``` 2. **使用 QStyle**: PySide6 提供了多种内置样式,你可以使用 `QStyle` 类来设置应用程序的样式。 ```python from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton from PySide6.QtWidgets import QStyleFactory app = QApplication([]) app.setStyle(QStyleFactory.create("Fusion")) window = QWidget() layout = QVBoxLayout(window) button = QPushButton("Click Me") layout.addWidget(button) window.show() app.exec() ``` 3. **自定义样式**: 你可以创建自定义的样式类,继承自 `QProxyStyle` 或其他样式类,并重写其中的方法来实现自定义样式。 ```python from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton from PySide6.QtWidgets import QProxyStyle class MyStyle(QProxyStyle): def drawControl(self, element, option, painter, widget=None): if element == self.CE_PushButton: painter.save() painter.setBrush(Qt.blue) painter.setPen(Qt.NoPen) painter.drawRect(option.rect) painter.setPen(Qt.white) painter.drawText(option.rect, Qt.AlignCenter, option.text) painter.restore() else: super().drawControl(element, option, painter, widget) app = QApplication([]) app.setStyle(MyStyle()) window = QWidget() layout = QVBoxLayout(window) button = QPushButton("Click Me") layout.addWidget(button) window.show() app.exec() ``` 通过这些方法,你可以根据需要更换 PySide6 窗体的样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值