[fix]修改配置文件会被菜单工具替换的问题,大小和位置改为采用样式文件处理
This commit is contained in:
parent
671688eb3d
commit
9a25ad7fe4
@ -1,41 +0,0 @@
|
||||
#include "CButton.h"
|
||||
#include <QWidget>
|
||||
#include <QPainter>
|
||||
#include <QStyleOption>
|
||||
#include <QHBoxLayout>
|
||||
#include <QPainterPath>
|
||||
|
||||
CButton::CButton(const QIcon& icon, const QString &text, QWidget *parent) : QPushButton(icon, text,parent) {
|
||||
|
||||
}
|
||||
|
||||
void CButton::paintEvent(QPaintEvent *event) {
|
||||
Q_UNUSED(event);
|
||||
|
||||
QPainter painter(this);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
QStyleOptionButton option;
|
||||
initStyleOption(&option);
|
||||
QRectF rect = QRectF(option.rect);
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.setBrush(palette().button());
|
||||
|
||||
// 绘制平行四边形形状
|
||||
QPainterPath path;
|
||||
qreal c=0.4;
|
||||
path.moveTo(rect.left() + rect.width() * c, rect.top());
|
||||
path.lineTo(rect.right(), rect.top());
|
||||
path.lineTo(rect.right() - rect.width() * c, rect.bottom());
|
||||
path.lineTo(rect.left(), rect.bottom());
|
||||
path.lineTo(rect.left() + rect.width() * c, rect.top());
|
||||
painter.drawPath(path);
|
||||
|
||||
// 绘制按钮的文本
|
||||
painter.setPen(Qt::black);
|
||||
painter.drawText(rect, Qt::AlignCenter, text());
|
||||
|
||||
// 绘制按钮的状态
|
||||
if (isDown() || isChecked()) {
|
||||
painter.fillRect(rect, QColor(0, 0, 0, 20)); // 按下或选中状态时添加半透明效果
|
||||
}
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
#ifndef CBUTTON_H
|
||||
#define CBUTTON_H
|
||||
#include <QPushButton>
|
||||
|
||||
class CButton: public QPushButton
|
||||
{
|
||||
public:
|
||||
CButton(const QIcon& icon, const QString &text, QWidget *parent = Q_NULLPTR);
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void enterEvent(QEvent *event) override {
|
||||
setStyleSheet("background-color: yellow;"); // 鼠标悬浮时改变背景颜色为黄色
|
||||
QPushButton::enterEvent(event);
|
||||
}
|
||||
|
||||
void leaveEvent(QEvent *event) override {
|
||||
setStyleSheet(""); // 鼠标离开时恢复原来的样式表
|
||||
QPushButton::leaveEvent(event);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // CBUTTON_H
|
||||
@ -8,15 +8,18 @@
|
||||
#include <QProcess>
|
||||
#include <QDir>
|
||||
#include "pub_utility_api/FileStyle.h"
|
||||
#include "CButton.h"
|
||||
|
||||
CSecondButtonGroupWidget::CSecondButtonGroupWidget(QWidget *parent, bool editMode)
|
||||
: QWidget(parent),
|
||||
m_pJsonReader(NULL),
|
||||
m_isEdit(editMode)
|
||||
m_buttonGroup(NULL),
|
||||
m_isEdit(editMode),
|
||||
m_btnWidth(0),
|
||||
m_btnHeight(0),
|
||||
m_baseX(0)
|
||||
{
|
||||
initView();
|
||||
initQss();
|
||||
initView();
|
||||
}
|
||||
|
||||
CSecondButtonGroupWidget::~CSecondButtonGroupWidget()
|
||||
@ -28,6 +31,39 @@ CSecondButtonGroupWidget::~CSecondButtonGroupWidget()
|
||||
m_pJsonReader = NULL;
|
||||
}
|
||||
|
||||
int CSecondButtonGroupWidget::buttonWidth()
|
||||
{
|
||||
return m_btnWidth;
|
||||
}
|
||||
|
||||
void CSecondButtonGroupWidget::setButtonWidth(const int &width)
|
||||
{
|
||||
m_btnWidth = width;
|
||||
resetBtnStyle();
|
||||
}
|
||||
|
||||
int CSecondButtonGroupWidget::buttonHeight()
|
||||
{
|
||||
return m_btnHeight;
|
||||
}
|
||||
|
||||
void CSecondButtonGroupWidget::setButtonHeight(const int &height)
|
||||
{
|
||||
m_btnHeight = height;
|
||||
resetBtnStyle();
|
||||
}
|
||||
|
||||
int CSecondButtonGroupWidget::baseX()
|
||||
{
|
||||
return m_baseX;
|
||||
}
|
||||
|
||||
void CSecondButtonGroupWidget::setBaseX(const int &baseX)
|
||||
{
|
||||
m_baseX = baseX;
|
||||
resetBtnStyle();
|
||||
}
|
||||
|
||||
void CSecondButtonGroupWidget::onButtonClicked(int index)
|
||||
{
|
||||
QList<QPushButton *> buttonList = this->findChildren<QPushButton *>();
|
||||
@ -116,14 +152,14 @@ void CSecondButtonGroupWidget::initView()
|
||||
dir.cd("data");
|
||||
dir.cd("back_pixmap");
|
||||
|
||||
QButtonGroup *buttonGroup = new QButtonGroup(this);
|
||||
m_buttonGroup = new QButtonGroup(this);
|
||||
int id = 0;
|
||||
QMap<QString, ST_BUTTON>::const_iterator iter
|
||||
= buttonMap.constBegin();
|
||||
|
||||
int btnWidth= m_pJsonReader->getBtnWidth()>0?m_pJsonReader->getBtnWidth():195;
|
||||
int btnHeight= m_pJsonReader->getBtnHeight()>0?m_pJsonReader->getBtnHeight():45;
|
||||
int baseX=m_pJsonReader->getBaseX()>0?m_pJsonReader->getBaseX():160;
|
||||
int btnWidth= m_btnWidth>0?m_btnWidth:195;
|
||||
int btnHeight= m_btnHeight>0?m_btnHeight:45;
|
||||
int baseX=m_baseX>0?m_baseX:160;
|
||||
|
||||
for(; iter != buttonMap.constEnd(); iter++)
|
||||
{
|
||||
@ -135,7 +171,7 @@ void CSecondButtonGroupWidget::initView()
|
||||
button->setObjectName(iter.key());
|
||||
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
button->setCheckable(true);
|
||||
buttonGroup->addButton(button, id++);
|
||||
m_buttonGroup->addButton(button, id++);
|
||||
if(!m_isEdit)
|
||||
{
|
||||
connect(button, &QPushButton::clicked, this, &CSecondButtonGroupWidget::slotButtonClicked);
|
||||
@ -163,3 +199,24 @@ void CSecondButtonGroupWidget::initQss()
|
||||
setStyleSheet(qss);
|
||||
}
|
||||
}
|
||||
|
||||
void CSecondButtonGroupWidget::resetBtnStyle()
|
||||
{
|
||||
if(m_buttonGroup == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int btnWidth= m_btnWidth>0?m_btnWidth:195;
|
||||
int btnHeight= m_btnHeight>0?m_btnHeight:45;
|
||||
int baseX=m_baseX>0?m_baseX:160;
|
||||
|
||||
QList<QAbstractButton*> buttonList = m_buttonGroup->buttons();
|
||||
int id = 0;
|
||||
for (QAbstractButton *button : buttonList)
|
||||
{
|
||||
id++;
|
||||
button->setGeometry((id-1)*baseX, 5, btnWidth, btnHeight);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,12 +2,15 @@
|
||||
#define CBUTTONGROUPWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QButtonGroup>
|
||||
|
||||
class CJsonReader;
|
||||
class CSecondButtonGroupWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(int buttonWidth READ buttonWidth WRITE setButtonWidth)
|
||||
Q_PROPERTY(int buttonHeight READ buttonHeight WRITE setButtonHeight)
|
||||
Q_PROPERTY(int baseX READ baseX WRITE setBaseX)
|
||||
public:
|
||||
enum EN_BUTTON_TYPE
|
||||
{
|
||||
@ -20,6 +23,15 @@ public:
|
||||
CSecondButtonGroupWidget(QWidget *parent = 0, bool editMode = true);
|
||||
~CSecondButtonGroupWidget();
|
||||
|
||||
int buttonWidth();
|
||||
void setButtonWidth(const int &width);
|
||||
|
||||
int buttonHeight();
|
||||
void setButtonHeight(const int &height);
|
||||
|
||||
int baseX();
|
||||
void setBaseX(const int &baseX);
|
||||
|
||||
public slots:
|
||||
void onButtonClicked(int index = 0);
|
||||
int getCheckedButton();
|
||||
@ -53,10 +65,16 @@ private slots:
|
||||
private:
|
||||
void initView();
|
||||
void initQss();
|
||||
void resetBtnStyle();
|
||||
private:
|
||||
CJsonReader *m_pJsonReader;
|
||||
QButtonGroup *m_buttonGroup;
|
||||
bool m_isEdit;
|
||||
|
||||
int m_btnWidth;
|
||||
int m_btnHeight;
|
||||
int m_baseX;
|
||||
|
||||
};
|
||||
|
||||
#endif // CBUTTONGROUPWIDGET_H
|
||||
|
||||
@ -29,14 +29,12 @@ SOURCES += \
|
||||
# main.cpp \
|
||||
CJsonReader.cpp \
|
||||
CSecondButtonGroupPluginWidget.cpp \
|
||||
CSecondButtonGroupWidget.cpp \
|
||||
CButton.cpp
|
||||
CSecondButtonGroupWidget.cpp
|
||||
|
||||
HEADERS += \
|
||||
CJsonReader.h \
|
||||
CSecondButtonGroupPluginWidget.h \
|
||||
CSecondButtonGroupWidget.h \
|
||||
CButton.h
|
||||
CSecondButtonGroupWidget.h
|
||||
|
||||
LIBS += \
|
||||
-llog4cplus \
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
#include "CButtonGroupWidget.h"
|
||||
#include "CSecondButtonGroupWidget.h"
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
CButtonGroupWidget w;
|
||||
CSecondButtonGroupWidget w(0,false);
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user