赞
踩
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*);
};
//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)}");
}
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
//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("轮廓"));
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。