1 新增可移动按钮控件 2 解决可移动控件无法在子控件上拖动的情况
This commit is contained in:
parent
45ad0e67aa
commit
3e020719b0
418
xyylMCWEACSystem/MoveButton.cpp
Normal file
418
xyylMCWEACSystem/MoveButton.cpp
Normal file
@ -0,0 +1,418 @@
|
||||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "MoveButton.h"
|
||||
#include "qpainter.h"
|
||||
#include "qevent.h"
|
||||
#include "qtimer.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
MoveButton::MoveButton(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
text = "";
|
||||
textColor = QColor(255, 255, 255);
|
||||
|
||||
borderOutColorStart = QColor(255, 255, 255);
|
||||
borderOutColorEnd = QColor(166, 166, 166);
|
||||
|
||||
borderInColorStart = QColor(166, 166, 166);
|
||||
borderInColorEnd = QColor(255, 255, 255);
|
||||
|
||||
bgColor = QColor(100, 184, 255);
|
||||
|
||||
showRect = false;
|
||||
showOverlay = true;
|
||||
overlayColor = QColor(255, 255, 255);
|
||||
|
||||
canMove = false;
|
||||
this->installEventFilter(this);
|
||||
|
||||
timerAlarm = new QTimer(this);
|
||||
connect(timerAlarm, SIGNAL(timeout()), this, SLOT(alarm()));
|
||||
timerAlarm->setInterval(500);
|
||||
|
||||
//setFont(QFont("Arial", 8));
|
||||
}
|
||||
|
||||
bool MoveButton::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
if (canMove) {
|
||||
static QPoint lastPnt;
|
||||
static bool pressed = false;
|
||||
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
||||
|
||||
if (mouseEvent->type() == QEvent::MouseButtonPress) {
|
||||
if (this->rect().contains(mouseEvent->pos()) && (mouseEvent->button() == Qt::LeftButton)) {
|
||||
lastPnt = mouseEvent->pos();
|
||||
pressed = true;
|
||||
}
|
||||
} else if (mouseEvent->type() == QEvent::MouseMove && pressed) {
|
||||
int dx = mouseEvent->pos().x() - lastPnt.x();
|
||||
int dy = mouseEvent->pos().y() - lastPnt.y();
|
||||
this->move(this->x() + dx, this->y() + dy);
|
||||
} else if (mouseEvent->type() == QEvent::MouseButtonRelease && pressed) {
|
||||
pressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void MoveButton::paintEvent(QPaintEvent *)
|
||||
{
|
||||
int width = this->width();
|
||||
int height = this->height();
|
||||
int side = qMin(width, height);
|
||||
|
||||
//绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
|
||||
QPainter painter(this);
|
||||
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
|
||||
|
||||
if (showRect) {
|
||||
//绘制矩形区域
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.setBrush(bgColor);
|
||||
painter.drawRoundedRect(this->rect(), 5, 5);
|
||||
|
||||
//绘制文字
|
||||
if (!text.isEmpty()) {
|
||||
QFont font;
|
||||
font.setPixelSize(side - 20);
|
||||
painter.setFont(font);
|
||||
painter.setPen(textColor);
|
||||
painter.drawText(this->rect(), Qt::AlignCenter, text);
|
||||
}
|
||||
} else {
|
||||
painter.translate(width / 2, height / 2);
|
||||
painter.scale(side / 200.0, side / 200.0);
|
||||
|
||||
//绘制外边框
|
||||
drawBorderOut(&painter);
|
||||
//绘制内边框
|
||||
drawBorderIn(&painter);
|
||||
//绘制内部指示颜色
|
||||
drawBg(&painter);
|
||||
//绘制居中文字
|
||||
drawText(&painter);
|
||||
//绘制遮罩层
|
||||
drawOverlay(&painter);
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::drawBorderOut(QPainter *painter)
|
||||
{
|
||||
int radius = 99;
|
||||
painter->save();
|
||||
painter->setPen(Qt::NoPen);
|
||||
QLinearGradient borderGradient(0, -radius, 0, radius);
|
||||
borderGradient.setColorAt(0, borderOutColorStart);
|
||||
borderGradient.setColorAt(1, borderOutColorEnd);
|
||||
painter->setBrush(borderGradient);
|
||||
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
void MoveButton::drawBorderIn(QPainter *painter)
|
||||
{
|
||||
int radius = 90;
|
||||
painter->save();
|
||||
painter->setPen(Qt::NoPen);
|
||||
QLinearGradient borderGradient(0, -radius, 0, radius);
|
||||
borderGradient.setColorAt(0, borderInColorStart);
|
||||
borderGradient.setColorAt(1, borderInColorEnd);
|
||||
painter->setBrush(borderGradient);
|
||||
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
void MoveButton::drawBg(QPainter *painter)
|
||||
{
|
||||
int radius = 80;
|
||||
painter->save();
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(bgColor);
|
||||
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
void MoveButton::drawText(QPainter *painter)
|
||||
{
|
||||
if (text.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int radius = 100;
|
||||
painter->save();
|
||||
|
||||
QFont font;
|
||||
font.setPixelSize(85);
|
||||
painter->setFont(font);
|
||||
painter->setPen(textColor);
|
||||
QRect rect(-radius, -radius, radius * 2, radius * 2);
|
||||
painter->drawText(rect, Qt::AlignCenter, text);
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
void MoveButton::drawOverlay(QPainter *painter)
|
||||
{
|
||||
if (!showOverlay) {
|
||||
return;
|
||||
}
|
||||
|
||||
int radius = 80;
|
||||
painter->save();
|
||||
painter->setPen(Qt::NoPen);
|
||||
|
||||
QPainterPath smallCircle;
|
||||
QPainterPath bigCircle;
|
||||
radius -= 1;
|
||||
smallCircle.addEllipse(-radius, -radius, radius * 2, radius * 2);
|
||||
radius *= 2;
|
||||
bigCircle.addEllipse(-radius, -radius + 140, radius * 2, radius * 2);
|
||||
|
||||
//高光的形状为小圆扣掉大圆的部分
|
||||
QPainterPath highlight = smallCircle - bigCircle;
|
||||
|
||||
QLinearGradient linearGradient(0, -radius / 2, 0, 0);
|
||||
overlayColor.setAlpha(100);
|
||||
linearGradient.setColorAt(0.0, overlayColor);
|
||||
overlayColor.setAlpha(30);
|
||||
linearGradient.setColorAt(1.0, overlayColor);
|
||||
painter->setBrush(linearGradient);
|
||||
painter->rotate(-20);
|
||||
painter->drawPath(highlight);
|
||||
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
QString MoveButton::getText() const
|
||||
{
|
||||
return this->text;
|
||||
}
|
||||
|
||||
QColor MoveButton::getTextColor() const
|
||||
{
|
||||
return this->textColor;
|
||||
}
|
||||
|
||||
QColor MoveButton::getBorderOutColorStart() const
|
||||
{
|
||||
return this->borderOutColorStart;
|
||||
}
|
||||
|
||||
QColor MoveButton::getBorderOutColorEnd() const
|
||||
{
|
||||
return this->borderOutColorEnd;
|
||||
}
|
||||
|
||||
QColor MoveButton::getBorderInColorStart() const
|
||||
{
|
||||
return this->borderInColorStart;
|
||||
}
|
||||
|
||||
QColor MoveButton::getBorderInColorEnd() const
|
||||
{
|
||||
return this->borderInColorEnd;
|
||||
}
|
||||
|
||||
QColor MoveButton::getBgColor() const
|
||||
{
|
||||
return this->bgColor;
|
||||
}
|
||||
|
||||
bool MoveButton::getCanMove() const
|
||||
{
|
||||
return this->canMove;
|
||||
}
|
||||
|
||||
bool MoveButton::getShowRect() const
|
||||
{
|
||||
return this->showRect;
|
||||
}
|
||||
|
||||
bool MoveButton::getShowOverlay() const
|
||||
{
|
||||
return this->showOverlay;
|
||||
}
|
||||
|
||||
QColor MoveButton::getOverlayColor() const
|
||||
{
|
||||
return this->overlayColor;
|
||||
}
|
||||
|
||||
QSize MoveButton::sizeHint() const
|
||||
{
|
||||
return QSize(100, 100);
|
||||
}
|
||||
|
||||
QSize MoveButton::minimumSizeHint() const
|
||||
{
|
||||
return QSize(10, 10);
|
||||
}
|
||||
|
||||
void MoveButton::setText(const QString &text)
|
||||
{
|
||||
if (this->text != text) {
|
||||
this->text = text;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setTextColor(const QColor &textColor)
|
||||
{
|
||||
if (this->textColor != textColor) {
|
||||
this->textColor = textColor;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setBorderOutColorStart(const QColor &borderOutColorStart)
|
||||
{
|
||||
if (this->borderOutColorStart != borderOutColorStart) {
|
||||
this->borderOutColorStart = borderOutColorStart;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setBorderOutColorEnd(const QColor &borderOutColorEnd)
|
||||
{
|
||||
if (this->borderOutColorEnd != borderOutColorEnd) {
|
||||
this->borderOutColorEnd = borderOutColorEnd;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setBorderInColorStart(const QColor &borderInColorStart)
|
||||
{
|
||||
if (this->borderInColorStart != borderInColorStart) {
|
||||
this->borderInColorStart = borderInColorStart;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setBorderInColorEnd(const QColor &borderInColorEnd)
|
||||
{
|
||||
if (this->borderInColorEnd != borderInColorEnd) {
|
||||
this->borderInColorEnd = borderInColorEnd;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setBgColor(const QColor &bgColor)
|
||||
{
|
||||
if (this->bgColor != bgColor) {
|
||||
this->bgColor = bgColor;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setCanMove(bool canMove)
|
||||
{
|
||||
if (this->canMove != canMove) {
|
||||
this->canMove = canMove;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setShowRect(bool showRect)
|
||||
{
|
||||
if (this->showRect != showRect) {
|
||||
this->showRect = showRect;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setShowOverlay(bool showOverlay)
|
||||
{
|
||||
if (this->showOverlay != showOverlay) {
|
||||
this->showOverlay = showOverlay;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setOverlayColor(const QColor &overlayColor)
|
||||
{
|
||||
if (this->overlayColor != overlayColor) {
|
||||
this->overlayColor = overlayColor;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::setGreen()
|
||||
{
|
||||
textColor = QColor(255, 255, 255);
|
||||
setBgColor(QColor(0, 166, 0));
|
||||
}
|
||||
|
||||
void MoveButton::setRed()
|
||||
{
|
||||
textColor = QColor(255, 255, 255);
|
||||
setBgColor(QColor(255, 0, 0));
|
||||
}
|
||||
|
||||
void MoveButton::setYellow()
|
||||
{
|
||||
textColor = QColor(25, 50, 7);
|
||||
setBgColor(QColor(238, 238, 0));
|
||||
}
|
||||
|
||||
void MoveButton::setBlack()
|
||||
{
|
||||
textColor = QColor(255, 255, 255);
|
||||
setBgColor(QColor(10, 10, 10));
|
||||
}
|
||||
|
||||
void MoveButton::setGray()
|
||||
{
|
||||
textColor = QColor(255, 255, 255);
|
||||
setBgColor(QColor(129, 129, 129));
|
||||
}
|
||||
|
||||
void MoveButton::setBlue()
|
||||
{
|
||||
textColor = QColor(255, 255, 255);
|
||||
setBgColor(QColor(0, 0, 166));
|
||||
}
|
||||
|
||||
void MoveButton::setLightBlue()
|
||||
{
|
||||
textColor = QColor(255, 255, 255);
|
||||
setBgColor(QColor(100, 184, 255));
|
||||
}
|
||||
|
||||
void MoveButton::setLightRed()
|
||||
{
|
||||
textColor = QColor(255, 255, 255);
|
||||
setBgColor(QColor(255, 107, 107));
|
||||
}
|
||||
|
||||
void MoveButton::setLightGreen()
|
||||
{
|
||||
textColor = QColor(255, 255, 255);
|
||||
setBgColor(QColor(24, 189, 155));
|
||||
}
|
||||
|
||||
void MoveButton::startAlarm()
|
||||
{
|
||||
if (!timerAlarm->isActive()) {
|
||||
timerAlarm->start();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::stopAlarm()
|
||||
{
|
||||
if (timerAlarm->isActive()) {
|
||||
timerAlarm->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void MoveButton::alarm()
|
||||
{
|
||||
static bool isRed = false;
|
||||
if (isRed) {
|
||||
this->setBlack();
|
||||
} else {
|
||||
this->setRed();
|
||||
}
|
||||
|
||||
isRed = !isRed;
|
||||
}
|
126
xyylMCWEACSystem/MoveButton.h
Normal file
126
xyylMCWEACSystem/MoveButton.h
Normal file
@ -0,0 +1,126 @@
|
||||
#ifndef MoveButton_H
|
||||
#define MoveButton_H
|
||||
|
||||
/*
|
||||
MoveButton
|
||||
*/
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
|
||||
class MoveButton : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString text READ getText WRITE setText)
|
||||
Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
|
||||
|
||||
Q_PROPERTY(QColor borderOutColorStart READ getBorderOutColorStart WRITE setBorderOutColorStart)
|
||||
Q_PROPERTY(QColor borderOutColorEnd READ getBorderOutColorEnd WRITE setBorderOutColorEnd)
|
||||
Q_PROPERTY(QColor borderInColorStart READ getBorderInColorStart WRITE setBorderInColorStart)
|
||||
Q_PROPERTY(QColor borderInColorEnd READ getBorderInColorEnd WRITE setBorderInColorEnd)
|
||||
Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
|
||||
|
||||
Q_PROPERTY(bool canMove READ getCanMove WRITE setCanMove)
|
||||
Q_PROPERTY(bool showRect READ getShowRect WRITE setShowRect)
|
||||
Q_PROPERTY(bool showOverlay READ getShowOverlay WRITE setShowOverlay)
|
||||
Q_PROPERTY(QColor overlayColor READ getOverlayColor WRITE setOverlayColor)
|
||||
|
||||
public:
|
||||
explicit MoveButton(QWidget *parent = 0);
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
void paintEvent(QPaintEvent *);
|
||||
void drawBorderOut(QPainter *painter);
|
||||
void drawBorderIn(QPainter *painter);
|
||||
void drawBg(QPainter *painter);
|
||||
void drawText(QPainter *painter);
|
||||
void drawOverlay(QPainter *painter);
|
||||
|
||||
private:
|
||||
QString text; //文本
|
||||
QColor textColor; //文字颜色
|
||||
|
||||
QColor borderOutColorStart; //外边框渐变开始颜色
|
||||
QColor borderOutColorEnd; //外边框渐变结束颜色
|
||||
QColor borderInColorStart; //里边框渐变开始颜色
|
||||
QColor borderInColorEnd; //里边框渐变结束颜色
|
||||
QColor bgColor; //背景颜色
|
||||
|
||||
bool showRect; //显示成矩形
|
||||
bool canMove; //是否能够移动
|
||||
bool showOverlay; //是否显示遮罩层
|
||||
QColor overlayColor; //遮罩层颜色
|
||||
|
||||
QTimer *timerAlarm; //定时器切换颜色
|
||||
|
||||
public:
|
||||
QString getText() const;
|
||||
QColor getTextColor() const;
|
||||
|
||||
QColor getBorderOutColorStart() const;
|
||||
QColor getBorderOutColorEnd() const;
|
||||
QColor getBorderInColorStart() const;
|
||||
QColor getBorderInColorEnd() const;
|
||||
QColor getBgColor() const;
|
||||
|
||||
bool getCanMove() const;
|
||||
bool getShowRect() const;
|
||||
bool getShowOverlay() const;
|
||||
QColor getOverlayColor() const;
|
||||
|
||||
QSize sizeHint() const;
|
||||
QSize minimumSizeHint() const;
|
||||
|
||||
public Q_SLOTS:
|
||||
//设置文本
|
||||
void setText(const QString &text);
|
||||
//设置文本颜色
|
||||
void setTextColor(const QColor &textColor);
|
||||
|
||||
//设置外边框渐变颜色
|
||||
void setBorderOutColorStart(const QColor &borderOutColorStart);
|
||||
void setBorderOutColorEnd(const QColor &borderOutColorEnd);
|
||||
|
||||
//设置里边框渐变颜色
|
||||
void setBorderInColorStart(const QColor &borderInColorStart);
|
||||
void setBorderInColorEnd(const QColor &borderInColorEnd);
|
||||
|
||||
//设置背景色
|
||||
void setBgColor(const QColor &bgColor);
|
||||
|
||||
//设置是否可移动
|
||||
void setCanMove(bool canMove);
|
||||
//设置是否显示矩形
|
||||
void setShowRect(bool showRect);
|
||||
//设置是否显示遮罩层
|
||||
void setShowOverlay(bool showOverlay);
|
||||
//设置遮罩层颜色
|
||||
void setOverlayColor(const QColor &overlayColor);
|
||||
|
||||
//设置为绿色
|
||||
void setGreen();
|
||||
//设置为红色
|
||||
void setRed();
|
||||
//设置为黄色
|
||||
void setYellow();
|
||||
//设置为黑色
|
||||
void setBlack();
|
||||
//设置为灰色
|
||||
void setGray();
|
||||
//设置为蓝色
|
||||
void setBlue();
|
||||
//设置为淡蓝色
|
||||
void setLightBlue();
|
||||
//设置为淡红色
|
||||
void setLightRed();
|
||||
//设置为淡绿色
|
||||
void setLightGreen();
|
||||
|
||||
//设置报警闪烁
|
||||
void startAlarm();
|
||||
void stopAlarm();
|
||||
void alarm();
|
||||
};
|
||||
|
||||
#endif //MoveButton_H
|
@ -40,18 +40,17 @@
|
||||
#include "datamanager.h"
|
||||
#include "leadscheme.h"
|
||||
#include <QTextCodec>
|
||||
#include "MoveButton.h"
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
QTextCodec *codec = QTextCodec::codecForName("GBK");
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
FrameWindow *mainw = new FrameWindow;
|
||||
FramelessWindow *window = new FramelessWindow(mainw);
|
||||
window->resize(300, 400);
|
||||
window->showMaximized();
|
||||
|
||||
//a.setStyleSheet("QWidget{background-color:#ffffff;}");
|
||||
// a.setStyleSheet("QWidget{background: rgb(47, 61, 82);}");
|
||||
// FramelessWindow *window = new FramelessWindow(mainw);
|
||||
//window->resize(300, 400);
|
||||
// window->showMaximized();
|
||||
mainw->showMaximized();
|
||||
QFont globalFont;
|
||||
globalFont.setFamily("黑体");
|
||||
QApplication::setFont(globalFont);
|
||||
@ -60,6 +59,33 @@ int main(int argc, char *argv[])
|
||||
|
||||
LeadScheme lead;
|
||||
lead.show();
|
||||
|
||||
|
||||
QWidget * w = new QWidget;
|
||||
|
||||
QList<QColor> colors;
|
||||
colors.append(QColor(100, 184, 255));
|
||||
colors.append(QColor(255, 107, 107));
|
||||
colors.append(QColor(24, 189, 155));
|
||||
colors.append(QColor(1, 174, 103));
|
||||
colors.append(QColor(52, 73, 94));
|
||||
|
||||
int x = 5;
|
||||
int y = 5;
|
||||
int radius = 50;
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
MoveButton *btn = new MoveButton(w);
|
||||
|
||||
btn->setGeometry(x, y, radius, radius);
|
||||
x = x + radius;
|
||||
btn->setText(QString("0%10").arg(i + 1));
|
||||
btn->setCanMove(true);
|
||||
btn->setShowOverlay(false);
|
||||
btn->setBgColor(colors.at(i));
|
||||
//btns.append(btn);
|
||||
}
|
||||
w->show();
|
||||
#if 0
|
||||
MedicalRecordManager me;
|
||||
me.show();
|
||||
|
Loading…
x
Reference in New Issue
Block a user