当前位置:   article > 正文

QT之鼠标滑过按钮,按钮改变颜色,离开之后,按钮恢复原先颜色_qt button修改颜色后如何恢复

qt button修改颜色后如何恢复

1、重写按钮类MyButton

//myButton.h
#include<QPushButton>
#include<QEvent>
class MyButton :public QPushButton
{
    Q_OBJECT;
 
public:
    MyButton(QWidget *parent = 0);
    ~MyButton();
 
public:
    void enterEvent(QEvent*);
    void leaveEvent(QEvent*);
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
//myButton.cpp
#include"myButton.h"
MyButton::MyButton(QWidget* parent) :QPushButton(parent)
{
}
 
MyButton::~MyButton()
{
}
 
void MyButton::enterEvent(QEvent *)
{
    setStyleSheet("QPushButton{background-color:rgb(255,255,0)}");
}
 
void MyButton::leaveEvent(QEvent *)
{
    setStyleSheet("QPushButton{background-color:rgb(17,17,17)}");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2、在另一个类中添加#include"myButton.h",声明MyButton的指针对象MyButton *button1, *button2;

//analysis.h
#ifndef ANALYSIS_H
#define ANALYSIS_H
 
#include <QtWidgets/QWidget>
#include "ui_analysis.h"
#include"myButton.h"
#include<QPushButton>
#include<QToolButton>
#pragma execution_charactor_set("utf-8")
 
class Analysis : public QWidget
{
    Q_OBJECT
 
public:
    Analysis(QWidget *parent = 0);
    ~Analysis();
    MyButton *button1, *button2;
    QToolButton *toolButton;
    bool flag = false;
    QEvent *e;
 
private:
    Ui::AnalysisClass ui;
 
public slots:
    void chooseMenu();
    void chooseBtn();
};
 
#endif // ANALYSIS_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
//analysis.cpp
#include "analysis.h"
#include<QEvent>
 
Analysis::Analysis(QWidget *parent)
: QWidget(parent)
{
    ui.setupUi(this);
    button1 = new MyButton(this);
    button2 = new MyButton(this);
    toolButton = new QToolButton(this);
    QPixmap pixmap;
    pixmap.load("images/arrow_view-analysis.png");
    toolButton->setIcon(pixmap);
    toolButton->setIconSize(pixmap.size());
    toolButton->setText(QStringLiteral("请选择分类"));
    toolButton->setGeometry(200, 100, 100, 40);
    toolButton->setStyleSheet("border:1px groove gray;border-radius:3px;background-color:rgb(17,17,17);");
    QPalette pal;
    pal.setColor(QPalette::ButtonText, QColor(255, 255, 255));
    toolButton->setPalette(pal);
 
    button1->setText(QStringLiteral("支架"));
    button1->setGeometry(200, 140, 100, 40);
    button1->setStyleSheet("background-color:rgb(17,17,17);");
    button1->setObjectName("btn1");
    button1->setPalette(pal);
    
    
    button2->setText(QStringLiteral("轮廓"));
    button2->setGeometry(200, 180, 100, 40);
    button2->setObjectName("btn2");
    button2->setStyleSheet("background-color:rgb(17,17,17);");
    button2->setPalette(pal); 
    
    button1->hide();
    button2->hide();
    connect(toolButton, SIGNAL(clicked()), this, SLOT(chooseMenu()));
    connect(button1, SIGNAL(clicked()), this, SLOT(chooseBtn()));
    connect(button2, SIGNAL(clicked()), this, SLOT(chooseBtn()));
}
 
Analysis::~Analysis()
{
}
 
void Analysis::chooseMenu()
{
    if (!flag)
    {
        button1->show();
        button2->show();
    }
}
 
 
void Analysis::chooseBtn()
{
    QPushButton *btn = qobject_cast<QPushButton*>(sender());
    if ("btn1" == btn->objectName())
    {
        button1->hide();
        button2->hide();
        toolButton->setText(QStringLiteral("支架"));
    }
    if ("btn2" == btn->objectName())
    {
        button1->hide();
        button2->hide();
        toolButton->setText(QStringLiteral("轮廓"));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/292740
推荐阅读
相关标签
  

闽ICP备14008679号