QPushButton 样式设置 渐变圆角光泽按钮

QPushButton
1.事件设置
2.快捷键设置
3.样式设置

QPushButton事件信号

1.click()
2.click(bool) 是否选中
3.pressed()
4.released()


#include 
#include 
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
public slots:
    void Click(bool ch)
    {
        qDebug()<<ch;
    }

    void Pre()
    {
        qDebug()<<"pressed";
    }

    void Rel()
    {
        qDebug()<<"release";
    }
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H


#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include 
#include 
#include 
#include 

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
    QPushButton *clickButton;

    void setupUi(QWidget *Widget)
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QString::fromUtf8("Widget"));
        Widget->resize(800, 600);
        clickButton = new QPushButton(Widget);
        clickButton->setObjectName(QString::fromUtf8("clickButton"));
        clickButton->setGeometry(QRect(100, 140, 281, 101));
        clickButton->setCheckable(true);

        retranslateUi(Widget);
        QObject::connect(clickButton, SIGNAL(clicked(bool)), Widget, SLOT(Click(bool)));
        QObject::connect(clickButton, SIGNAL(pressed()), Widget, SLOT(Pre()));
        QObject::connect(clickButton, SIGNAL(released()), Widget, SLOT(Rel()));

        QMetaObject::connectSlotsByName(Widget);
    } // setupUi

    void retranslateUi(QWidget *Widget)
    {
        Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
        clickButton->setText(QCoreApplication::translate("Widget", "click", nullptr));
    } // retranslateUi

};

namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

QPushButton 快捷键

1.setShortcut
2.setShortcut(tr(“Alt+F7”))
3.setShortcut(tr(“Alt+X,Alt+Z”))


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->shortButton->setShortcut(tr("Ctrl+X"));
}

QPushButton样式设置
1.背景色,字体及颜色
2.背景图片 FLAT
3.圆角边框
4.渐变圆角光泽按钮


#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include 
#include 
#include 
#include 

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
    QPushButton *shortButton;
    QPushButton *pushButton;

    void setupUi(QWidget *Widget)
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QString::fromUtf8("Widget"));
        Widget->resize(800, 600);
        shortButton = new QPushButton(Widget);
        shortButton->setObjectName(QString::fromUtf8("shortButton"));
        shortButton->setGeometry(QRect(120, 40, 421, 221));
        shortButton->setStyleSheet(QString::fromUtf8("background-color: rgb(52, 101, 164);\n"
"color: qlineargradient(spread:pad, x1:1, y1:0.533, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
"font: 700 31pt \"Ubuntu Condensed\";\n"
"color: rgb(164, 0, 0);\n"
"background-image: url(:/test.gif);\n"
"border-radius:15"));
        shortButton->setCheckable(true);
        pushButton = new QPushButton(Widget);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        pushButton->setGeometry(QRect(180, 320, 321, 161));
        pushButton->setStyleSheet(QString::fromUtf8("QPushButton{\n"
"border-radius:15;\n"
"font: 40pt \"Ubuntu\";\n"
"color: rgb(164, 0, 0);\n"
"}\n"
"QPushButton::!hover{\n"
"background-color: qlineargradient(spread:reflect, x1:1, y1:0.545455, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"QPushButton::hover{\n"
"background-color: qlineargradient(spread:reflect, x1:0.989691, y1:0.483, x2:1, y2:1, stop:0.546392 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
""));

        retranslateUi(Widget);
        QObject::connect(shortButton, SIGNAL(clicked()), Widget, SLOT(Pre()));

        QMetaObject::connectSlotsByName(Widget);
    } // setupUi

    void retranslateUi(QWidget *Widget)
    {
        Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
        shortButton->setText(QCoreApplication::translate("Widget", "&Click", nullptr));
        pushButton->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
    } // retranslateUi

};

namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

你可能感兴趣的:(Qt,学习,Qt,qt,ui,开发语言)