├── .gitattributes ├── ArcProgressBar ├── ArcProgressBar.pro ├── ArcProgressBar_screenshot.gif ├── ArcProgressBar_shortcut.gif ├── arcprogressbar.cpp ├── arcprogressbar.h ├── background.png ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h └── src.qrc ├── ColorDashboard ├── ColorDashboard.pro ├── ColorDashboard_Screenshot.gif ├── ColorDashboard_shotcut.gif ├── colordashboard.cpp ├── colordashboard.h ├── main.cpp ├── mainwindow.cpp └── mainwindow.h ├── DynamicLine ├── DynamicLine.pro ├── DynamicLine.pro.user ├── dynamicline.cpp ├── dynamicline.h ├── main.cpp ├── mainwindow.cpp └── mainwindow.h ├── GenericDashboard ├── Dashboard.pro ├── GenericDashBoard_screenshot.gif ├── GenericDashBoard_shotcut.gif ├── genericdashboard.cpp ├── genericdashboard.h ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h └── src.qrc ├── GridWidget ├── GridWidget.pro ├── GridWidget1_shotcut.gif ├── GridWidget2_shotcut.gif ├── GridWidget3_shotcut.gif ├── gridwidget.cpp ├── gridwidget.h ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── src.qrc └── src │ ├── 00.png │ ├── 01.png │ ├── 02.png │ ├── 03.png │ ├── 04.png │ ├── 05.png │ ├── 06.png │ ├── 07.png │ ├── 08.png │ ├── 09.png │ ├── 10.png │ ├── 11.png │ ├── 12.png │ ├── 13.png │ ├── 14.png │ ├── 15.png │ ├── 16.png │ ├── 17.png │ ├── 18.png │ ├── 19.png │ ├── 20.png │ ├── 21.png │ ├── 22.png │ ├── background.gif │ └── icon.gif ├── README.md ├── SliderWidget ├── SliderWidget.pro ├── SliderWidget.pro.user ├── main.cpp ├── res.qrc ├── resource │ ├── icons │ │ ├── indicatorActive.png │ │ └── indicatorInactive.png │ └── imgs │ │ ├── (0).jpg │ │ ├── (1).jpg │ │ ├── (10).jpg │ │ ├── (11).jpg │ │ ├── (12).jpg │ │ ├── (13).jpg │ │ ├── (14).jpg │ │ ├── (15).jpg │ │ ├── (16).jpg │ │ ├── (17).jpg │ │ ├── (18).jpg │ │ ├── (19).jpg │ │ ├── (2).jpg │ │ ├── (20).jpg │ │ ├── (21).jpg │ │ ├── (22).jpg │ │ ├── (23).jpg │ │ ├── (24).jpg │ │ ├── (25).jpg │ │ ├── (26).jpg │ │ ├── (27).jpg │ │ ├── (28).jpg │ │ ├── (29).jpg │ │ ├── (3).jpg │ │ ├── (30).jpg │ │ ├── (31).jpg │ │ ├── (32).jpg │ │ ├── (33).jpg │ │ ├── (34).jpg │ │ ├── (35).jpg │ │ ├── (36).jpg │ │ ├── (37).jpg │ │ ├── (38).jpg │ │ ├── (39).jpg │ │ ├── (4).jpg │ │ ├── (40).jpg │ │ ├── (41).jpg │ │ ├── (42).jpg │ │ ├── (43).jpg │ │ ├── (44).jpg │ │ ├── (45).jpg │ │ ├── (46).jpg │ │ ├── (47).jpg │ │ ├── (48).jpg │ │ ├── (49).jpg │ │ ├── (5).jpg │ │ ├── (50).jpg │ │ ├── (51).jpg │ │ ├── (52).jpg │ │ ├── (53).jpg │ │ ├── (54).jpg │ │ ├── (55).jpg │ │ ├── (56).jpg │ │ ├── (57).jpg │ │ ├── (58).jpg │ │ ├── (59).jpg │ │ ├── (6).jpg │ │ ├── (60).jpg │ │ ├── (7).jpg │ │ ├── (8).jpg │ │ ├── (9).jpg │ │ ├── background.png │ │ ├── background1.png │ │ ├── background2.png │ │ ├── background3.png │ │ ├── icon.gif │ │ └── logo.png ├── slider1.gif ├── slider2.gif ├── sliderWidget.cpp └── sliderWidget.h ├── WaveProgressBar ├── WareProgressBar_Screenshot.gif ├── WareProgressBar_shorcut.gif ├── WaveProgressBar.pro ├── background.png ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── screenshot.cpp ├── src.qrc ├── wareprogressbar.cpp └── wareprogressbar.h ├── bannerLabel ├── banner-image │ ├── 0.jpg │ ├── 1.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── 4.jpg │ ├── 5.jpeg │ └── 5.jpg ├── bannerLabel.pro ├── bannerLabel_shotcut.gif ├── bannerlabel.cpp ├── bannerlabel.h ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── point.png └── resource.qrc ├── commonhelper ├── commonhelper.h ├── commonhelper.pro ├── main.cpp ├── mainwindow.cpp └── mainwindow.h ├── iconhelper ├── Font Awesome Cheatsheet.png ├── fontawesome-webfont.ttf ├── iconhelper.h ├── iconhelper.pro ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h └── resource.qrc ├── popupmessagebox ├── icon.gif ├── icon.jpg ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── popupmessagebox.cpp ├── popupmessagebox.h ├── popupmessagebox.pro ├── popupmessagebox │ ├── black.qss │ ├── close.png │ ├── close_hover.png │ ├── close_pressed.png │ └── home.png └── resource.qrc └── simplemessagebox ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── resource.qrc ├── simplemessagebox.cpp ├── simplemessagebox.h ├── simplemessagebox.pro └── simplemessagebox ├── black.qss ├── close.png ├── close_hover.png ├── close_pressed.png ├── error.png ├── home.png ├── info.png └── question.png /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /ArcProgressBar/ArcProgressBar.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # You can make your code fail to compile if it uses deprecated APIs. 8 | # In order to do so, uncomment the following line. 9 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 10 | 11 | SOURCES += \ 12 | arcprogressbar.cpp \ 13 | main.cpp \ 14 | mainwindow.cpp 15 | 16 | HEADERS += \ 17 | arcprogressbar.h \ 18 | mainwindow.h 19 | 20 | # Default rules for deployment. 21 | qnx: target.path = /tmp/$${TARGET}/bin 22 | else: unix:!android: target.path = /opt/$${TARGET}/bin 23 | !isEmpty(target.path): INSTALLS += target 24 | 25 | RESOURCES += \ 26 | src.qrc 27 | -------------------------------------------------------------------------------- /ArcProgressBar/ArcProgressBar_screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/ArcProgressBar/ArcProgressBar_screenshot.gif -------------------------------------------------------------------------------- /ArcProgressBar/ArcProgressBar_shortcut.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/ArcProgressBar/ArcProgressBar_shortcut.gif -------------------------------------------------------------------------------- /ArcProgressBar/arcprogressbar.cpp: -------------------------------------------------------------------------------- 1 | #include "arcprogressbar.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | ArcProgressBar::ArcProgressBar(QWidget *parent) : QWidget(parent) 8 | { 9 | m_animation.setTargetObject(this); 10 | m_animation.setPropertyName("curValue"); 11 | m_animation.setEasingCurve(m_easingCurveType); 12 | } 13 | 14 | ArcProgressBar::~ArcProgressBar() 15 | { } 16 | 17 | void ArcProgressBar::paintEvent(QPaintEvent *event) 18 | { 19 | QPainter painter(this); 20 | 21 | painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing, true); 22 | painter.translate(width()/2.0, height()/2.0); 23 | m_radius = qMin(width(), height()) / 2.0; 24 | 25 | drawArc(&painter); 26 | drawValue(&painter); 27 | drawTitle(&painter); 28 | 29 | return QWidget::paintEvent(event); 30 | } 31 | 32 | void ArcProgressBar::drawArc(QPainter *painter) 33 | { 34 | double pen_width = m_radius * 0.17; 35 | double radius = m_radius - pen_width; 36 | QRectF rect(-radius, -radius, 2.0*radius, 2.0*radius); 37 | 38 | painter->save(); 39 | 40 | QPen pen; 41 | pen.setWidthF(pen_width); 42 | pen.setCapStyle(Qt::RoundCap); 43 | 44 | painter->setBrush(Qt::NoBrush); 45 | 46 | double allAngle = m_endAngle - m_startAngle; 47 | double curAngle = allAngle * (m_curValue - m_minValue) / (m_maxValue - m_minValue); 48 | double otherAngle = allAngle - curAngle; 49 | 50 | pen.setColor(m_baseColor); 51 | painter->setPen(pen); 52 | painter->drawArc(rect, (270 - m_startAngle-curAngle)*16, -otherAngle * 16); 53 | 54 | pen.setColor(m_arcColor); 55 | painter->setPen(pen); 56 | painter->drawArc(rect, (270 - m_startAngle) * 16, -curAngle*16); 57 | 58 | painter->restore(); 59 | } 60 | 61 | void ArcProgressBar::drawValue(QPainter *painter) 62 | { 63 | double radius = m_radius * 0.7; 64 | 65 | painter->save(); 66 | 67 | painter->setPen(m_textColor); 68 | painter->setFont(QFont("Consolas", m_radius * 0.25, QFont::Bold)); 69 | double temp = 1.0 * m_curValue / (m_maxValue - m_minValue) * 100; 70 | painter->drawText(QRect(-radius, -radius*0.1, radius * 2, radius), Qt::AlignCenter, QString("%1%").arg(temp)); 71 | 72 | painter->restore(); 73 | } 74 | 75 | void ArcProgressBar::drawTitle(QPainter *painter) 76 | { 77 | double radius = m_radius * 0.7; 78 | 79 | painter->save(); 80 | 81 | painter->setPen(m_titleColor); 82 | painter->setFont(QFont("Consolas", m_radius * 0.12, QFont::Bold)); 83 | painter->drawText(QRect(-radius, -radius*0.8, radius * 2, radius), Qt::AlignCenter, m_title); 84 | 85 | painter->restore(); 86 | } 87 | 88 | void ArcProgressBar::updateValue(int value) 89 | { 90 | m_curValue = value; 91 | update(); 92 | } 93 | 94 | int ArcProgressBar::getCurValue() const 95 | { 96 | return m_curValue; 97 | } 98 | 99 | void ArcProgressBar::setValue(int value) 100 | { 101 | if (value < m_minValue) 102 | value = m_minValue; 103 | else if (value > m_maxValue) 104 | value = m_minValue; 105 | 106 | m_value = value; 107 | 108 | if (m_animationStepTime == 0) 109 | { 110 | updateValue(m_value); 111 | } 112 | else if (m_curValue != m_value) 113 | { 114 | m_animation.stop(); 115 | m_animation.setKeyValueAt(0, m_curValue); 116 | m_animation.setKeyValueAt(1, m_value); 117 | 118 | m_animation.setDuration(m_animationStepTime * (m_value - m_minValue)); 119 | m_animation.start(); 120 | } 121 | } 122 | 123 | void ArcProgressBar::setArcColor(const QColor &color) 124 | { 125 | m_arcColor = color; 126 | update(); 127 | } 128 | 129 | void ArcProgressBar::setBaseColor(const QColor &color) 130 | { 131 | m_baseColor = color; 132 | update(); 133 | } 134 | 135 | void ArcProgressBar::setTextColor(const QColor &color) 136 | { 137 | m_textColor = color; 138 | update(); 139 | } 140 | 141 | void ArcProgressBar::setTitleColor(const QColor &color) 142 | { 143 | m_titleColor = color; 144 | update(); 145 | } 146 | 147 | void ArcProgressBar::setTitle(const QString &title) 148 | { 149 | m_title = title; 150 | update(); 151 | } 152 | 153 | void ArcProgressBar::setRange(int minValue, int maxValue) 154 | { 155 | m_minValue = minValue; 156 | m_maxValue = maxValue; 157 | update(); 158 | } 159 | 160 | void ArcProgressBar::setMinValue(int minValue) 161 | { 162 | m_minValue = minValue; 163 | update(); 164 | } 165 | 166 | void ArcProgressBar::setMaxValue(int maxValue) 167 | { 168 | m_maxValue = maxValue; 169 | update(); 170 | } 171 | 172 | void ArcProgressBar::setAngleRange(int startAngle, int endAngle) 173 | { 174 | m_startAngle = startAngle; 175 | m_endAngle = endAngle; 176 | update(); 177 | } 178 | 179 | void ArcProgressBar::setStartAngle(int startAngle) 180 | { 181 | m_startAngle = startAngle; 182 | update(); 183 | } 184 | 185 | void ArcProgressBar::setEndAngle(int endAngle) 186 | { 187 | m_endAngle = endAngle; 188 | update(); 189 | } 190 | 191 | void ArcProgressBar::setAnimationStepTime(int msec) 192 | { 193 | m_animationStepTime = msec; 194 | update(); 195 | } 196 | 197 | QColor ArcProgressBar::getArcColor() const 198 | { 199 | return m_arcColor; 200 | } 201 | 202 | QColor ArcProgressBar::getBaseColor() const 203 | { 204 | return m_baseColor; 205 | } 206 | 207 | QColor ArcProgressBar::getTextColor() const 208 | { 209 | return m_textColor; 210 | } 211 | 212 | QColor ArcProgressBar::getTitleColor() const 213 | { 214 | return m_title; 215 | } 216 | 217 | QString ArcProgressBar::getTitle() const 218 | { 219 | return m_title; 220 | } 221 | 222 | int ArcProgressBar::getMinValue() const 223 | { 224 | return m_minValue; 225 | } 226 | 227 | int ArcProgressBar::getMaxValue() const 228 | { 229 | return m_maxValue; 230 | } 231 | 232 | int ArcProgressBar::getValue() const 233 | { 234 | return m_value; 235 | } 236 | 237 | int ArcProgressBar::getStartAngle() const 238 | { 239 | return m_startAngle; 240 | } 241 | 242 | int ArcProgressBar::getEndAngle() const 243 | { 244 | return m_endAngle; 245 | } 246 | 247 | int ArcProgressBar::getAnimationStepTime() const 248 | { 249 | return m_animationStepTime; 250 | } 251 | 252 | 253 | 254 | -------------------------------------------------------------------------------- /ArcProgressBar/arcprogressbar.h: -------------------------------------------------------------------------------- 1 | #ifndef ARCPROGRESSBAR_H 2 | #define ARCPROGRESSBAR_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /* 圆弧进度条自定义控件 实现的功能 10 | * 1. 可设置范围 11 | * 2. 可设置开始旋转角度、结束旋转角度 12 | * 3. 可设置过度动画时常 13 | * 4. 可设置仪表盘标题 14 | * 5. 可设置背景、进度条、值、标题颜色 15 | * 6. 自适应窗体拉伸,文字自动缩放 16 | */ 17 | 18 | class ArcProgressBar : public QWidget 19 | { 20 | Q_OBJECT 21 | 22 | Q_PROPERTY(QColor arcColor READ getArcColor WRITE setArcColor) 23 | Q_PROPERTY(QColor baseColor READ getBaseColor WRITE setBaseColor) 24 | Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor) 25 | Q_PROPERTY(QColor titleColor READ getTitleColor WRITE setTitleColor) 26 | Q_PROPERTY(QString title READ getTitle WRITE setTitle) 27 | Q_PROPERTY(int minValue READ getMinValue WRITE setMinValue) 28 | Q_PROPERTY(int maxValue READ getMaxValue WRITE setMaxValue) 29 | Q_PROPERTY(int value READ getValue WRITE setValue) 30 | Q_PROPERTY(int animationStepTime READ getAnimationStepTime WRITE setAnimationStepTime) 31 | Q_PROPERTY(int startAngle READ getStartAngle WRITE setStartAngle) 32 | Q_PROPERTY(int endAngle READ getEndAngle WRITE setEndAngle) 33 | Q_PROPERTY(int curValue READ getCurValue WRITE updateValue) 34 | 35 | public: 36 | explicit ArcProgressBar(QWidget *parent = nullptr); 37 | ~ArcProgressBar(); 38 | 39 | QColor getArcColor() const; 40 | QColor getBaseColor() const; 41 | QColor getTextColor() const; 42 | QColor getTitleColor() const; 43 | QString getTitle() const; 44 | int getMinValue() const; 45 | int getMaxValue() const; 46 | int getValue() const; 47 | int getStartAngle() const; 48 | int getEndAngle() const; 49 | int getAnimationStepTime() const; 50 | 51 | public slots: 52 | void setArcColor(const QColor &color); // 设置圆弧颜色 53 | void setBaseColor(const QColor &color); // 设置基础颜色 54 | void setTextColor(const QColor &color); // 设置文本颜色 55 | void setTitleColor(const QColor &color); // 设置标题颜色 56 | 57 | void setTitle(const QString &title); // 设置Title 58 | 59 | void setRange(int minValue, int maxValue); // 设置范围值 60 | void setMinValue(int minValue); // 设置最小值 61 | void setMaxValue(int maxValue); // 设置最大值 62 | void setValue(int value); // 设置目标值 63 | 64 | void setAngleRange(int startAngle, int endAngle); // 设备旋转角度范围 65 | void setStartAngle(int startAngle); // 设置开始旋转角度 66 | void setEndAngle(int endAngle); // 设置结束旋转角度 67 | 68 | void setAnimationStepTime(int msec); // 设置每刻度动画持续时间 69 | 70 | protected: 71 | void paintEvent(QPaintEvent *event) override; 72 | 73 | void drawArc(QPainter *painter); 74 | void drawValue(QPainter *painter); 75 | void drawTitle(QPainter *painter); 76 | 77 | void updateValue(int value); 78 | int getCurValue() const; 79 | 80 | signals: 81 | 82 | private: 83 | double m_radius = 0.0; 84 | 85 | QColor m_arcColor = QColor(34,163,169); 86 | QColor m_baseColor = QColor(180,180,180); 87 | QColor m_textColor = QColor(34,163,169); 88 | QColor m_titleColor = QColor(Qt::black); 89 | 90 | QString m_title = "正确率"; 91 | 92 | int m_startAngle = 45; 93 | int m_endAngle = 315; 94 | 95 | int m_maxValue = 100; 96 | int m_minValue = 0; 97 | int m_curValue = 0; 98 | int m_value = 0; 99 | 100 | QPropertyAnimation m_animation; 101 | int m_animationStepTime = 5; 102 | QEasingCurve::Type m_easingCurveType = QEasingCurve::Linear; 103 | }; 104 | 105 | #endif // ARCPROGRESSBAR_H 106 | -------------------------------------------------------------------------------- /ArcProgressBar/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/ArcProgressBar/background.png -------------------------------------------------------------------------------- /ArcProgressBar/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /ArcProgressBar/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | #include 5 | 6 | MainWindow::MainWindow(QWidget *parent) 7 | : QLabel(parent) 8 | { 9 | QSlider *pSlider = new QSlider(this); 10 | ArcProgressBar *bar1 = createArcProgressBar1(); 11 | ArcProgressBar *bar2 = createArcProgressBar2(); 12 | ArcProgressBar *bar3 = createArcProgressBar3(); 13 | 14 | pSlider->setRange(0, 100); 15 | 16 | connect(pSlider, &QSlider::valueChanged, bar1, &ArcProgressBar::setValue); 17 | connect(pSlider, &QSlider::valueChanged, bar2, &ArcProgressBar::setValue); 18 | connect(pSlider, &QSlider::valueChanged, bar3, &ArcProgressBar::setValue); 19 | 20 | QHBoxLayout *pLayout = new QHBoxLayout; 21 | pLayout->addWidget(bar1); 22 | pLayout->addWidget(bar2); 23 | pLayout->addWidget(bar3); 24 | pLayout->addSpacing(20); 25 | pLayout->addWidget(pSlider); 26 | 27 | setLayout(pLayout); 28 | 29 | setMinimumSize(1000, 500); 30 | setPixmap(QPixmap(":/image/background.png")); 31 | setScaledContents(true); 32 | } 33 | 34 | MainWindow::~MainWindow() 35 | { 36 | } 37 | 38 | ArcProgressBar *MainWindow::createArcProgressBar1() 39 | { 40 | ArcProgressBar *ret = new ArcProgressBar(this); 41 | 42 | ret->setArcColor(QColor(247,74,99)); 43 | ret->setTextColor(QColor(247,74,99)); 44 | ret->setTitleColor(Qt::black); 45 | ret->setAnimationStepTime(10); 46 | ret->setTitle("完成率"); 47 | 48 | return ret; 49 | } 50 | 51 | ArcProgressBar *MainWindow::createArcProgressBar2() 52 | { 53 | ArcProgressBar *ret = new ArcProgressBar(this); 54 | 55 | ret->setStartAngle(145); 56 | ret->setEndAngle(405); 57 | ret->setAnimationStepTime(0); 58 | ret->setTitle("及格率"); 59 | 60 | return ret; 61 | } 62 | 63 | ArcProgressBar *MainWindow::createArcProgressBar3() 64 | { 65 | ArcProgressBar *ret = new ArcProgressBar(this); 66 | 67 | ret->setStartAngle(0); 68 | ret->setEndAngle(360); 69 | 70 | return ret; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /ArcProgressBar/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include "arcprogressbar.h" 6 | 7 | class MainWindow : public QLabel 8 | { 9 | Q_OBJECT 10 | 11 | public: 12 | MainWindow(QWidget *parent = nullptr); 13 | ~MainWindow(); 14 | protected: 15 | ArcProgressBar *createArcProgressBar1(); 16 | ArcProgressBar *createArcProgressBar2(); 17 | ArcProgressBar *createArcProgressBar3(); 18 | }; 19 | #endif // MAINWINDOW_H 20 | -------------------------------------------------------------------------------- /ArcProgressBar/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | background.png 4 | 5 | 6 | -------------------------------------------------------------------------------- /ColorDashboard/ColorDashboard.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # You can make your code fail to compile if it uses deprecated APIs. 8 | # In order to do so, uncomment the following line. 9 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 10 | 11 | SOURCES += \ 12 | colordashboard.cpp \ 13 | main.cpp \ 14 | mainwindow.cpp 15 | 16 | HEADERS += \ 17 | colordashboard.h \ 18 | mainwindow.h 19 | 20 | # Default rules for deployment. 21 | qnx: target.path = /tmp/$${TARGET}/bin 22 | else: unix:!android: target.path = /opt/$${TARGET}/bin 23 | !isEmpty(target.path): INSTALLS += target 24 | -------------------------------------------------------------------------------- /ColorDashboard/ColorDashboard_Screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/ColorDashboard/ColorDashboard_Screenshot.gif -------------------------------------------------------------------------------- /ColorDashboard/ColorDashboard_shotcut.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/ColorDashboard/ColorDashboard_shotcut.gif -------------------------------------------------------------------------------- /ColorDashboard/colordashboard.h: -------------------------------------------------------------------------------- 1 | #ifndef COLORDASHBOARD_H 2 | #define COLORDASHBOARD_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /* 多彩仪表盘自定义控件 实现的功能 9 | * 1. 可设置范围值 10 | * 2. 可设置大刻度数量,小刻度数量 11 | * 3. 可设置开始旋转角度、结束旋转角度 12 | * 4. 可设置是否启用动画、动画特效及每刻度动画持续时间 13 | * 5. 可设置外圆背景、内圆背景、提示指针、刻度尺、刻度值、提示文字颜色 14 | * 6. 可设置三色刻度占用比例 15 | * 7. 可设置圆环样式,三色圆环、当前圆环 16 | * 8. 可设置指示器样式,球形、指针形、圆角指针、三角形指示器 17 | * 8. 自适应窗体拉伸、刻度尺和文字自动缩放 18 | */ 19 | 20 | class ColorDashboard : public QWidget 21 | { 22 | Q_OBJECT 23 | Q_ENUMS(PieStyle) 24 | Q_ENUMS(PointerStyle) 25 | 26 | Q_PROPERTY(int minValue READ getMinValue WRITE setMinValue) 27 | Q_PROPERTY(int maxValue READ getMaxValue WRITE setMaxValue) 28 | Q_PROPERTY(int scaleMajor READ getScaleMajor WRITE setScaleMajor) 29 | Q_PROPERTY(int scaleMinor READ getScaleMinor WRITE setScaleMinor) 30 | Q_PROPERTY(int startAngle READ getStartAngle WRITE setStartAngle) 31 | Q_PROPERTY(int endAngle READ getEndAngle WRITE setEndAngle) 32 | Q_PROPERTY(int animationStepTime READ getAnimationStepTime WRITE setAnimationStepTime) 33 | Q_PROPERTY(QColor outerCircleColor READ getOuterCircleColor WRITE setOuterCircleColor) 34 | Q_PROPERTY(QColor innerCircleColor READ getInnerCircleColor WRITE setInnerCircleColor) 35 | Q_PROPERTY(QColor m_pieColorStart READ getPieStartColor WRITE setPieStartColor) 36 | Q_PROPERTY(QColor m_pieColorMid READ getPieMidColor WRITE setPieMidColor) 37 | Q_PROPERTY(QColor m_pieColorEnd READ getPieEndColor WRITE setPieEndColor) 38 | Q_PROPERTY(QColor scaleColor READ getScaleColor WRITE setScaleColor) 39 | Q_PROPERTY(QColor scaleNumColor READ getScaleNumColor WRITE setScaleNumColor) 40 | Q_PROPERTY(QColor pointerColor READ getPointerColor WRITE setPointerColor) 41 | Q_PROPERTY(QColor centerCirleColor READ getCenterCircleColor WRITE setCenterCircleColor) 42 | Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor) 43 | Q_PROPERTY(bool isOverlayVisible READ isOverlayVisible WRITE setOverlayVisible) 44 | Q_PROPERTY(QColor overlayColor READ getOverlayColor WRITE setOverlayColor) 45 | Q_PROPERTY(PieStyle pieStyle READ getPieStyle WRITE setPieStyle) 46 | Q_PROPERTY(PointerStyle pointerStyle READ getPointerStyle WRITE setPointerStyle) 47 | Q_PROPERTY(QEasingCurve::Type easingCurveType READ getEasingCurve WRITE setEasingCurve) 48 | Q_PROPERTY(int curValue READ getCurValue WRITE updateValue) 49 | 50 | public: 51 | enum PieStyle { 52 | PieStyle_Three = 0u, // 三色圆环 53 | PieStyle_Current // 当前圆环 54 | }; 55 | 56 | enum PointerStyle { 57 | PointerStyle_Ball = 0u, // 圆指示器 58 | PointerStyle_Indicator, // 指针指示器 59 | PointerStyle_IndicatorR, // 圆角指针指示器 60 | PointerStyle_Triangle // 三角指示器 61 | }; 62 | 63 | explicit ColorDashboard(QWidget *parent = nullptr); 64 | ~ColorDashboard(); 65 | 66 | int getMinValue() const; 67 | int getMaxValue() const; 68 | 69 | int getValue() const; 70 | 71 | int getScaleMajor() const; 72 | int getScaleMinor() const; 73 | 74 | int getStartAngle() const; 75 | int getEndAngle() const; 76 | 77 | int getAnimationStepTime() const; 78 | 79 | QColor getOuterCircleColor() const; 80 | QColor getInnerCircleColor() const; 81 | 82 | QColor getPieStartColor() const; 83 | QColor getPieMidColor() const; 84 | QColor getPieEndColor() const; 85 | 86 | QColor getScaleColor() const; 87 | QColor getScalCircleColor() const; 88 | QColor getScaleNumColor() const; 89 | QColor getPointerColor() const; 90 | QColor getCenterCircleColor() const; 91 | QColor getTextColor() const; 92 | 93 | bool isOverlayVisible() const; 94 | QColor getOverlayColor() const; 95 | 96 | PieStyle getPieStyle() const; 97 | PointerStyle getPointerStyle() const; 98 | 99 | QEasingCurve::Type getEasingCurve() const; 100 | 101 | public slots: 102 | void setRange(int minValue, int maxValue); // 设置范围值 103 | void setMinValue(int minValue); // 设置最小值 104 | void setMaxValue(int maxValue); // 设置最大值 105 | 106 | void setValue(int value); // 设置目标值 107 | 108 | void setScaleMajor(int scaleMajor); // 设置主刻度数量 109 | void setScaleMinor(int scaleMinor); // 设置小刻度数量 110 | 111 | void setAngleRange(int startAngle, int endAngle); // 设备旋转角度范围 112 | void setStartAngle(int startAngle); // 设置开始旋转角度 113 | void setEndAngle(int endAngle); // 设置结束旋转角度 114 | 115 | void setAnimationStepTime(int msec); // 设置每刻度动画持续时间 116 | 117 | void setOuterCircleColor(const QColor &outerCircleColor); // 设置外圆颜色 118 | void setInnerCircleColor(const QColor &innerCircleColor); // 设置内圆颜色 119 | 120 | // 设置饼圆三种颜色 121 | void setPieStartColor(const QColor &scaleStartColor); 122 | void setPieMidColor(const QColor &scaleMidColor); 123 | void setPieEndColor(const QColor &scaleEndColor); 124 | 125 | void setScaleColor(const QColor &scaleColor); // 设置刻度颜色 126 | void setScalCircleColor(const QColor &scaleCircleColor); // 设置刻度圆颜色 127 | void setScaleNumColor(const QColor &scaleNumColor); // 设置刻度值颜色 128 | void setPointerColor(const QColor &pointerColor); // 设置指针颜色 129 | void setCenterCircleColor(const QColor ¢erCircleColor); // 设置中心圆颜色 130 | void setTextColor(const QColor &textColor); // 设置提示文本颜色 131 | 132 | void setOverlayVisible(bool overlay); // 设置是否显示遮蔽罩 133 | void setOverlayColor(const QColor &overlayColor); // 设置遮蔽罩颜色 134 | 135 | void setPieStyle(PieStyle pieStyle); // 设置饼图央视 136 | void setPointerStyle(PointerStyle pointerStyle); // 设置指针样式 137 | 138 | void setEasingCurve(QEasingCurve::Type type); // 设置指针动画类型 139 | 140 | protected: 141 | void paintEvent(QPaintEvent *e) override; 142 | 143 | inline void drawGenericCircle(QPainter *painter, double radius, const QColor &cokor); 144 | void drawOuterCircle(QPainter *painter); 145 | void drawScaleCircle(QPainter *painter); 146 | void drawPieCircle(QPainter *painter); 147 | void drawThreePieCircle(QPainter *painter, double radius); 148 | void drawCurrentPieCircle(QPainter *painter, double radius); 149 | void drawInnerCircle(QPainter *painter); 150 | void drawPointerCircle(QPainter *painter); 151 | void drawCenterCircel(QPainter *painter); 152 | void drawScaleNum(QPainter *painter); 153 | void drawScale(QPainter *painter); 154 | void drawOverlay(QPainter *painter); 155 | void drawPointer(QPainter *painter); 156 | void drawPointerBall(QPainter *painter); 157 | void drawPointerIndicator(QPainter *painter); 158 | void drawPointerIndicatorR(QPainter *painter); 159 | void drawPointerTriangle(QPainter *painter); 160 | void drawText(QPainter *painter); 161 | 162 | void updateValue(int value); 163 | int getCurValue() const; 164 | 165 | signals: 166 | void valueChanged(int value); 167 | void finished(); 168 | 169 | private: 170 | double m_radius = 0; // 仪表盘半径 171 | 172 | QColor m_outerCircleColor = QColor(80, 80, 80); // 外圆颜色 173 | QColor m_scaleCircleColor = QColor(60, 60, 60); // 刻度圆颜色 174 | QColor m_innerCircleColor = QColor(100, 100, 100); // 内圆颜色 175 | QColor m_centerCirleColor = QColor(250, 250, 250); // 中心圆颜色 176 | QColor m_pointerColor = QColor(223,105,105); // 指针颜色 177 | QColor m_pieColorStart = QColor(24,189,155); // 饼圆开始颜色 178 | QColor m_pieColorMid = QColor(218,218,0); // 饼圆中间颜色 179 | QColor m_pieColorEnd = QColor(255,107,107); // 饼圆结束颜色 180 | QColor m_scaleNumColor = QColor(255,255,255); // 刻度字体颜色 181 | QColor m_scaleColor = QColor(255,255,255); // 刻度颜色 182 | QColor m_overlayColor = QColor(255,255,255); // 遮避罩颜色 183 | QColor m_textColor = QColor(0,0,0); // 提示文本颜色 184 | 185 | PieStyle m_pieStyle = PieStyle_Current; // 饼风格 186 | PointerStyle m_pointerStyle = PointerStyle_Triangle; // 指示器样式 187 | 188 | int m_value = 0; // 表盘设定值 189 | int m_curValue = 0; // 表盘当前值 190 | int m_minValue = 0; // 表盘最小值 191 | int m_maxValue = 240; // 表盘最大值 192 | int m_scaleMajor = 10; // 大刻度数量 193 | int m_scaleMinor = 5; // 小刻度数量 【如:总刻度数量 = scaleMajor * scaleMinor】 194 | int m_startAngle = 45; // 起始旋转角度 195 | int m_endAngle = 315; // 结束旋转角度 196 | bool m_isOverlayVisible = true; // 是否显示遮蔽罩 197 | 198 | QPropertyAnimation m_animation; // 指针属性动画 199 | int m_animationStepTime = 50; // 每刻度动画持续时间 200 | QEasingCurve::Type m_easingCurveType = QEasingCurve::OutQuad; // 指针动画缓和曲线类型 201 | }; 202 | 203 | #endif // COLORDASHBOARD_H 204 | -------------------------------------------------------------------------------- /ColorDashboard/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 8 | 9 | QApplication a(argc, argv); 10 | MainWindow w; 11 | w.show(); 12 | return a.exec(); 13 | } 14 | -------------------------------------------------------------------------------- /ColorDashboard/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | MainWindow::MainWindow(QWidget *parent) 8 | : QWidget(parent) 9 | { 10 | QSlider *slder = new QSlider(this); 11 | 12 | ColorDashboard *db1 = ColorDashboard1(); 13 | ColorDashboard *db2 = ColorDashboard2(); 14 | ColorDashboard *db3 = ColorDashboard3(); 15 | ColorDashboard *db4 = ColorDashboard4(); 16 | 17 | connect(db1, &ColorDashboard::valueChanged, [&](int val){qDebug() << val;}); // 打印指针移动时对应的表盘数值 18 | connect(db1, &ColorDashboard::finished, [&](){qDebug() << "Pointer reached";}); // 打印指针到达目标数值 19 | 20 | connect(slder, &QSlider::valueChanged, db1, &ColorDashboard::setValue); // 链接仪表盘数值更新槽 21 | connect(slder, &QSlider::valueChanged, db2, &ColorDashboard::setValue); // 链接仪表盘数值更新槽 22 | connect(slder, &QSlider::valueChanged, db3, &ColorDashboard::setValue); // 链接仪表盘数值更新槽 23 | connect(slder, &QSlider::valueChanged, db4, &ColorDashboard::setValue); // 链接仪表盘数值更新槽 24 | 25 | slder->setOrientation(Qt::Horizontal); 26 | slder->setRange(db1->getMinValue(), db1->getMaxValue()); 27 | slder->setValue(db1->getValue()); 28 | 29 | QGridLayout *pLayout = new QGridLayout; 30 | pLayout->addWidget(db1, 0, 0); 31 | pLayout->addWidget(db2, 0, 1); 32 | pLayout->addWidget(db3, 1, 0); 33 | pLayout->addWidget(db4, 1, 1); 34 | pLayout->addWidget(slder, 2, 0, 1, 2); 35 | 36 | setLayout(pLayout); 37 | 38 | setMinimumSize(400, 400); 39 | } 40 | 41 | MainWindow::~MainWindow() 42 | { 43 | } 44 | 45 | ColorDashboard *MainWindow::ColorDashboard1() 46 | { 47 | ColorDashboard *ret = new ColorDashboard(this); 48 | 49 | ret->setMinValue(0); // 设置仪表盘最小值 50 | ret->setMaxValue(240); // 设置仪表盘最大值 51 | 52 | ret->setScaleMajor(8); // 设置主刻度数量 53 | ret->setScaleMinor(10); // 设置小刻度数量 54 | 55 | ret->setStartAngle(45); // 设置仪表盘起始角度 56 | ret->setEndAngle(315); // 设置仪表盘结束角度 57 | 58 | ret->setEasingCurve(QEasingCurve::OutQuad); // 设置指针缓和曲线 59 | ret->setAnimationStepTime(80); // 设置每刻度动画持续时间 60 | 61 | ret->setPointerStyle(ColorDashboard::PointerStyle_IndicatorR); // 设置指示器样式 62 | ret->setPieStyle(ColorDashboard::PieStyle_Three); // 设置饼圆样式 63 | 64 | return ret; 65 | } 66 | 67 | ColorDashboard *MainWindow::ColorDashboard2() 68 | { 69 | ColorDashboard *ret = new ColorDashboard(this); 70 | 71 | ret->setStartAngle(0); 72 | ret->setEndAngle(270); 73 | ret->setAnimationStepTime(0); 74 | ret->setPointerStyle(ColorDashboard::PointerStyle_Ball); 75 | 76 | return ret; 77 | } 78 | 79 | ColorDashboard *MainWindow::ColorDashboard3() 80 | { 81 | ColorDashboard *ret = new ColorDashboard(this); 82 | 83 | ret->setPointerStyle(ColorDashboard::PointerStyle_Triangle); 84 | ret->setPieStyle(ColorDashboard::PieStyle_Current); 85 | 86 | return ret; 87 | } 88 | 89 | ColorDashboard *MainWindow::ColorDashboard4() 90 | { 91 | ColorDashboard *ret = new ColorDashboard(this); 92 | 93 | ret->setPointerStyle(ColorDashboard::PointerStyle_Indicator); 94 | ret->setPieStyle(ColorDashboard::PieStyle_Three); 95 | ret->setOverlayVisible(false); 96 | 97 | return ret; 98 | } 99 | 100 | 101 | -------------------------------------------------------------------------------- /ColorDashboard/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | #include "colordashboard.h" 7 | 8 | class MainWindow : public QWidget 9 | { 10 | Q_OBJECT 11 | 12 | public: 13 | MainWindow(QWidget *parent = nullptr); 14 | ~MainWindow(); 15 | protected: 16 | ColorDashboard *ColorDashboard1(); 17 | ColorDashboard *ColorDashboard2(); 18 | ColorDashboard *ColorDashboard3(); 19 | ColorDashboard *ColorDashboard4(); 20 | }; 21 | #endif // MAINWINDOW_H 22 | -------------------------------------------------------------------------------- /DynamicLine/DynamicLine.pro: -------------------------------------------------------------------------------- 1 | QT += core gui charts 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # You can make your code fail to compile if it uses deprecated APIs. 8 | # In order to do so, uncomment the following line. 9 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 10 | 11 | SOURCES += \ 12 | dynamicline.cpp \ 13 | main.cpp \ 14 | mainwindow.cpp 15 | 16 | HEADERS += \ 17 | dynamicline.h \ 18 | mainwindow.h 19 | 20 | # Default rules for deployment. 21 | qnx: target.path = /tmp/$${TARGET}/bin 22 | else: unix:!android: target.path = /opt/$${TARGET}/bin 23 | !isEmpty(target.path): INSTALLS += target 24 | -------------------------------------------------------------------------------- /DynamicLine/dynamicline.cpp: -------------------------------------------------------------------------------- 1 | #include "dynamicline.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | DynamicLine::DynamicLine(QWidget *parent) 11 | : QWidget(parent), m_pChartView(new QChartView(this)), m_pChart(new QChart), m_pTimeAxisX(new QDateTimeAxis(this)), m_pAxisY(new QValueAxis(this)) 12 | { 13 | initUi(); 14 | initCtrl(); 15 | 16 | QTimer *pTimer = new QTimer(this); 17 | connect(pTimer, &QTimer::timeout, this, [&](){ 18 | m_lastValues[0] = QRandomGenerator::global()->bounded(120, 140); 19 | m_lastValues[1] = QRandomGenerator::global()->bounded(90, 120); 20 | m_lastValues[2] = QRandomGenerator::global()->bounded(75, 90); 21 | m_lastValues[3] = QRandomGenerator::global()->bounded(40, 70); 22 | m_lastValues[4] = QRandomGenerator::global()->bounded(10, 30); 23 | }); 24 | pTimer->start(1000); 25 | start(1000); 26 | } 27 | 28 | DynamicLine::~DynamicLine() 29 | { 30 | 31 | } 32 | 33 | void DynamicLine::initUi() 34 | { 35 | m_pTimeAxisX->setLabelsAngle(45); 36 | m_pChart->setParent(m_pChartView); 37 | m_pChart->addAxis(m_pTimeAxisX, Qt::AlignBottom); 38 | m_pChart->addAxis(m_pAxisY, Qt::AlignLeft); 39 | m_pChart->setTheme(QChart::ChartThemeDark); 40 | m_pChart->setAnimationOptions(QChart::SeriesAnimations); 41 | m_pChart->legend()->setAlignment(Qt::AlignBottom); 42 | 43 | m_pChartView->setChart(m_pChart); 44 | m_pChartView->setRenderHint(QPainter::Antialiasing); 45 | 46 | QFont font("微软雅黑", 12); 47 | font.setBold(true); 48 | 49 | m_pChart->setTitleFont(font); 50 | 51 | font.setPointSize(9); 52 | m_pTimeAxisX->setLabelsFont(font); 53 | m_pAxisY->setLabelsFont(font); 54 | 55 | font.setPointSize(10); 56 | m_pChart->legend()->setFont(font); 57 | 58 | QVBoxLayout *pLayout = new QVBoxLayout; 59 | pLayout->addWidget(m_pChartView); 60 | pLayout->setMargin(0); 61 | pLayout->setSpacing(0); 62 | setLayout(pLayout); 63 | } 64 | 65 | QLineSeries *DynamicLine::createSeries(QChart *chart, const QString &name, const QPen &pen, bool pointsVisible) 66 | { 67 | QLineSeries *ret = new QLineSeries(chart); 68 | 69 | chart->addSeries(ret); 70 | 71 | ret->setName(name); 72 | ret->attachAxis(m_pTimeAxisX); 73 | ret->attachAxis(m_pAxisY); 74 | ret->setPointsVisible(pointsVisible); 75 | ret->setPen(pen); 76 | 77 | return ret; 78 | } 79 | 80 | void DynamicLine::initCtrl() 81 | { 82 | connect(&m_timer, &QTimer::timeout, this, &DynamicLine::updateChart); 83 | } 84 | 85 | void DynamicLine::start(int intervalMsec) 86 | { 87 | if (m_timer.isActive()) 88 | m_timer.stop(); 89 | 90 | m_timer.start(intervalMsec); 91 | } 92 | 93 | void DynamicLine::stop() 94 | { 95 | m_timer.stop(); 96 | } 97 | 98 | int DynamicLine::count() const 99 | { 100 | return m_series.count(); 101 | } 102 | 103 | qint64 DynamicLine::timeAxisXSpanSecs() const 104 | { 105 | return m_spanSecs; 106 | } 107 | 108 | void DynamicLine::setSeriesVisible(int index, bool visible) 109 | { 110 | if (index < m_series.count()) 111 | m_series[index]->setVisible(visible); 112 | } 113 | 114 | void DynamicLine::setChartTitle(const QString &title) 115 | { 116 | m_pChart->setTitle(title); 117 | } 118 | 119 | void DynamicLine::setSeriesValues(int index, int value) 120 | { 121 | if (index < m_lastValues.count()) 122 | m_lastValues[index] = value; 123 | } 124 | 125 | void DynamicLine::addSplineSeries(const QString &name, const QPen &pen, bool pointsVisible) 126 | { 127 | QLineSeries *series = new QLineSeries(m_pChart); 128 | 129 | m_pChart->addSeries(series); 130 | 131 | series->setName(name); 132 | series->attachAxis(m_pTimeAxisX); 133 | series->attachAxis(m_pAxisY); 134 | series->setPointsVisible(pointsVisible); 135 | series->setPen(pen); 136 | 137 | m_series.append(series); 138 | m_lastValues.append(0); 139 | } 140 | 141 | void DynamicLine::setTimeAxisXSpanSecs(qint64 secs) 142 | { 143 | m_spanSecs = secs; 144 | } 145 | 146 | void DynamicLine::setTimeAxisXFormat(const QString &format) 147 | { 148 | m_pTimeAxisX->setFormat(format); 149 | } 150 | 151 | void DynamicLine::setTimeAxisXTickCount(int tickCount) 152 | { 153 | m_pTimeAxisX->setTickCount(tickCount); 154 | } 155 | 156 | void DynamicLine::setAxisYRange(qreal min, qreal max) 157 | { 158 | m_pAxisY->setRange(min, max); 159 | } 160 | 161 | void DynamicLine::setpAxisYTickCount(int count) 162 | { 163 | m_pAxisY->setTickCount(count); 164 | } 165 | 166 | void DynamicLine::setAxisYLabelFormat(const QString &format) 167 | { 168 | m_pAxisY->setLabelFormat(format); 169 | } 170 | 171 | void DynamicLine::updateChart() 172 | { 173 | QDateTime dateTime = QDateTime::currentDateTime(); 174 | 175 | if (!m_isFirstTime) 176 | { 177 | m_isFirstTime = true; 178 | 179 | QDateTime beginTime = dateTime; 180 | QDateTime endTime = beginTime.addSecs(m_spanSecs); 181 | m_pTimeAxisX->setMin(beginTime); 182 | m_pTimeAxisX->setMax(endTime); 183 | } 184 | 185 | if (m_pTimeAxisX->max() < dateTime) 186 | { 187 | QDateTime endTime = dateTime; 188 | QDateTime beginTime = endTime.addSecs(-m_spanSecs); 189 | m_pTimeAxisX->setMin(beginTime); 190 | m_pTimeAxisX->setMax(endTime); 191 | 192 | for (auto item : m_series) 193 | item->remove(0, 1); 194 | } 195 | 196 | for (int i=0; iappend(QDateTime::currentMSecsSinceEpoch(), m_lastValues.at(i)); 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /DynamicLine/dynamicline.h: -------------------------------------------------------------------------------- 1 | #ifndef DYNAMICLINE_H 2 | #define DYNAMICLINE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace QtCharts; 15 | 16 | /* 动态曲线 17 | * 1. 可设置 Y 轴范围、标签格式、tick 数量 18 | * 2. 可设置 X 轴时间跨度、时间格式、tick 数量 19 | * 3. 可显示任意多条曲线、设置对应曲线颜色及数值点是否显示 20 | * 4. 可设置曲线更新时间间隔 21 | * 5. 可选择隐藏曲线 22 | * 6. 自适应窗体拉伸,图表自动缩放 23 | */ 24 | 25 | class DynamicLine : public QWidget 26 | { 27 | Q_OBJECT 28 | 29 | public: 30 | explicit DynamicLine(QWidget *parent = nullptr); 31 | ~DynamicLine(); 32 | 33 | void start(int intervalMsec = 1000); // 开始更新图表 34 | void stop(); // 停止更新图表 35 | 36 | int count() const; // 曲线数量 37 | qint64 timeAxisXSpanSecs() const; // X 轴时间跨度 38 | void addSplineSeries(const QString &name = "", const QPen &pen = QPen(), bool pointsVisible = false); // 添加一条新的曲线 39 | 40 | public slots: 41 | void setSeriesValues(int index, int value); // 设置曲线值 42 | void setSeriesVisible(int index, bool visible); // 设置曲线是否可见 43 | void setChartTitle(const QString &title); // 设置图表标题 44 | 45 | void setTimeAxisXSpanSecs(qint64 secs); // 设置 X 轴时间跨度 46 | void setTimeAxisXFormat(const QString &format = "HH:mm"); // 设置 X 轴时间格式 47 | void setTimeAxisXTickCount(int tickCount); // 设置 X 轴 tick 数量 48 | 49 | void setAxisYRange(qreal min, qreal max); // 设置 Y 轴范围 50 | void setpAxisYTickCount(int count); // 设置 Y 轴 tick 数量 51 | void setAxisYLabelFormat(const QString &format); // 设置 Y 轴标签格式 52 | 53 | private: 54 | void initUi(); 55 | void initCtrl(); 56 | 57 | QLineSeries *createSeries(QChart *chart, const QString &name, const QPen &pen, bool pointsVisible = false); 58 | 59 | private slots: 60 | void updateChart(); 61 | 62 | private: 63 | QVector m_series; 64 | QChartView* m_pChartView = nullptr; 65 | QChart* m_pChart = nullptr; 66 | QDateTimeAxis* m_pTimeAxisX = nullptr; 67 | QValueAxis* m_pAxisY = nullptr; 68 | 69 | qint64 m_spanSecs = 60; 70 | QVector m_lastValues; 71 | bool m_isFirstTime = false; 72 | 73 | QTimer m_timer; 74 | }; 75 | 76 | #endif // DYNAMICLINE_H 77 | -------------------------------------------------------------------------------- /DynamicLine/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | 9 | MainWindow w; 10 | w.show(); 11 | 12 | return a.exec(); 13 | } 14 | -------------------------------------------------------------------------------- /DynamicLine/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | MainWindow::MainWindow(QWidget *parent) 9 | : QWidget(parent) 10 | { 11 | m_pLines = new DynamicLine(this); 12 | 13 | m_pLines->setChartTitle(QStringLiteral("历史温度曲线")); 14 | 15 | m_pLines->setTimeAxisXSpanSecs(10); 16 | m_pLines->setTimeAxisXFormat(QStringLiteral("HH:mm")); 17 | m_pLines->setTimeAxisXTickCount(11); 18 | 19 | m_pLines->setAxisYRange(0, 150); 20 | m_pLines->setpAxisYTickCount(6); 21 | m_pLines->setAxisYLabelFormat(QString::fromLatin1("%d℃")); 22 | 23 | QPen pen; 24 | pen.setWidth(2); 25 | pen.setColor(QColor(255,255,255)); 26 | m_pLines->addSplineSeries(QStringLiteral("端点1"), pen, true); 27 | pen.setColor(QColor(112,197,166)); 28 | m_pLines->addSplineSeries(QStringLiteral("端点2"), pen, true); 29 | pen.setColor(QColor(9,171,246)); 30 | m_pLines->addSplineSeries(QStringLiteral("端点3"), pen, true); 31 | pen.setColor(QColor(252,184,199)); 32 | m_pLines->addSplineSeries(QStringLiteral("端点4"), pen, true); 33 | pen.setColor(QColor(254,69,100)); 34 | m_pLines->addSplineSeries(QStringLiteral("端点5"), pen, true); 35 | m_pLines->start(1000); 36 | 37 | QVBoxLayout *pLayout = new QVBoxLayout; 38 | pLayout->addWidget(m_pLines, 9); 39 | pLayout->setSpacing(0); 40 | pLayout->setMargin(0); 41 | setLayout(pLayout); 42 | 43 | setWindowTitle(QStringLiteral("动态曲线 Demo")); 44 | resize(700, 350); 45 | 46 | QTimer *pTimer = new QTimer(this); 47 | connect(pTimer, &QTimer::timeout, this, &MainWindow::update); 48 | pTimer->start(1000); 49 | } 50 | 51 | MainWindow::~MainWindow() 52 | { 53 | } 54 | 55 | void MainWindow::update() 56 | { 57 | m_pLines->setSeriesValues(0, QRandomGenerator::global()->bounded(120, 140)); 58 | m_pLines->setSeriesValues(1, QRandomGenerator::global()->bounded(90, 120)); 59 | m_pLines->setSeriesValues(2, QRandomGenerator::global()->bounded(75, 90)); 60 | m_pLines->setSeriesValues(3, QRandomGenerator::global()->bounded(40, 70)); 61 | m_pLines->setSeriesValues(4, QRandomGenerator::global()->bounded(10, 30)); 62 | } 63 | 64 | -------------------------------------------------------------------------------- /DynamicLine/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | #include "dynamicline.h" 7 | 8 | class MainWindow : public QWidget 9 | { 10 | Q_OBJECT 11 | 12 | public: 13 | MainWindow(QWidget *parent = nullptr); 14 | ~MainWindow(); 15 | 16 | private slots: 17 | void update(); 18 | 19 | private: 20 | DynamicLine *m_pLines = nullptr; 21 | }; 22 | #endif // MAINWINDOW_H 23 | -------------------------------------------------------------------------------- /GenericDashboard/Dashboard.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # You can make your code fail to compile if it uses deprecated APIs. 8 | # In order to do so, uncomment the following line. 9 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 10 | 11 | SOURCES += \ 12 | genericdashboard.cpp \ 13 | main.cpp \ 14 | mainwindow.cpp 15 | 16 | HEADERS += \ 17 | genericdashboard.h \ 18 | mainwindow.h 19 | 20 | # Default rules for deployment. 21 | qnx: target.path = /tmp/$${TARGET}/bin 22 | else: unix:!android: target.path = /opt/$${TARGET}/bin 23 | !isEmpty(target.path): INSTALLS += target 24 | 25 | RESOURCES += \ 26 | src.qrc 27 | -------------------------------------------------------------------------------- /GenericDashboard/GenericDashBoard_screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GenericDashboard/GenericDashBoard_screenshot.gif -------------------------------------------------------------------------------- /GenericDashboard/GenericDashBoard_shotcut.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GenericDashboard/GenericDashBoard_shotcut.gif -------------------------------------------------------------------------------- /GenericDashboard/genericdashboard.h: -------------------------------------------------------------------------------- 1 | #ifndef DASHBOARD_H 2 | #define DASHBOARD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | /* 通用仪表盘自定义控件 实现的功能 11 | * 1. 可设置范围值 12 | * 2. 可设置大刻度数量,小刻度数量 13 | * 3. 可设置开始旋转角度、结束旋转角度 14 | * 4. 可设置是否启用动画、动画特效及每刻度动画持续时间 15 | * 5. 可设置外圆背景、内圆背景、提示指针、刻度尺、刻度值、提示文字颜色 16 | * 6. 可设置三色刻度占用比例 17 | * 7. 自适应窗体拉伸、刻度尺和文字自动缩放 18 | */ 19 | 20 | class GenericDashBoard : public QWidget 21 | { 22 | Q_OBJECT 23 | 24 | Q_PROPERTY(int minValue READ getMinValue WRITE setMinValue) 25 | Q_PROPERTY(int maxValue READ getMaxValue WRITE setMaxValue) 26 | Q_PROPERTY(int value READ getValue WRITE setValue) 27 | Q_PROPERTY(int curValue READ getCurValue WRITE updateValue) 28 | Q_PROPERTY(int animationStepTime READ getAnimationStepTime WRITE setAnimationStepTime) 29 | Q_PROPERTY(int scaleMajor READ getScaleMajor WRITE setScaleMajor) 30 | Q_PROPERTY(int scaleMinor READ getScaleMinor WRITE setScaleMinor) 31 | Q_PROPERTY(int startAngle READ getStartAngle WRITE setStartAngle) 32 | Q_PROPERTY(int endAngle READ getEndAngle WRITE setEndAngle) 33 | Q_PROPERTY(int scalePercentage READ getScalePercentage WRITE setScalePercentage) 34 | Q_PROPERTY(QColor scaleNumColor READ getScaleNumColor WRITE setScaleNumColor) 35 | Q_PROPERTY(QColor outerCircleColor READ getOuterCircleColor WRITE setOuterCircleColor) 36 | Q_PROPERTY(QColor innerCircleColor READ getInnerCircleColor WRITE setInnerCircleColor) 37 | Q_PROPERTY(QColor scaleStartColor READ getScaleStartColor WRITE setScaleStartColor) 38 | Q_PROPERTY(QColor scaleEndColor READ getScaleEndColor WRITE setScaleEndColor) 39 | Q_PROPERTY(QColor pointerColor READ getPointerColor WRITE setPointerColor) 40 | Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor) 41 | Q_PROPERTY(QString suffixText READ getSuffixText WRITE setSuffixText) 42 | 43 | public: 44 | explicit GenericDashBoard(QWidget *parent = nullptr); 45 | ~GenericDashBoard(); 46 | 47 | int getMinValue() const; 48 | int getMaxValue() const; 49 | int getValue() const; 50 | int getAnimationStepTime() const; 51 | int getScaleMajor() const; 52 | int getScaleMinor() const; 53 | int getStartAngle() const; 54 | int getEndAngle() const; 55 | QColor getScaleNumColor() const; 56 | QColor getOuterCircleColor() const; 57 | QColor getInnerCircleColor() const; 58 | QColor getScaleStartColor() const; 59 | QColor getScaleEndColor() const; 60 | QColor getPointerColor() const; 61 | QColor getTextColor() const; 62 | QString getSuffixText() const; 63 | QEasingCurve::Type getEasingCurve() const; 64 | double getScalePercentage() const; 65 | 66 | public slots: 67 | void setRange(int minValue, int maxValue); // 设置范围值 68 | void setMinValue(int minValue); // 设置最小值 69 | void setMaxValue(int maxValue); // 设置最大值 70 | 71 | void setValue(int value); // 设置目标值 72 | 73 | void setAnimationStepTime(int msec); // 设置每刻度动画持续时间 74 | 75 | void setScaleMajor(int scaleMajor); // 设置主刻度数量 76 | void setScaleMinor(int scaleMinor); // 设置小刻度数量 77 | 78 | void setAngleRange(int startAngle, int endAngle); // 设备旋转角度范围 79 | void setStartAngle(int startAngle); // 设置开始旋转角度 80 | void setEndAngle(int endAngle); // 设置结束旋转角度 81 | 82 | void setScaleNumColor(const QColor &scaleNumColor); // 设置刻度值颜色 83 | 84 | void setOuterCircleColor(const QColor &outerCircleColor); // 设置外圆颜色 85 | void setInnerCircleColor(const QColor &innerCircleColor); // 设置内圆颜色 86 | 87 | void setScalePercentage(double percentage); // 设置刻度百分比 【对应 m_scaleStartColor m_scaleEndColor】 88 | 89 | // 设置刻度两种颜色 90 | void setScaleStartColor(const QColor &scaleStartColor); 91 | void setScaleEndColor(const QColor &scaleEndColor); 92 | 93 | void setPointerColor(const QColor &pointerColor); // 设置指针颜色 94 | void setTextColor(const QColor &textColor); // 设置提示文本颜色 95 | void setSuffixText(const QString &string); // 设置提示文字 96 | 97 | void setEasingCurve(QEasingCurve::Type type); // 设置指针动画类型 98 | 99 | protected: 100 | void paintEvent(QPaintEvent *event) override; 101 | void drawBackground(QPainter *painter); // 绘制背景 102 | void drawScale(QPainter *painter); // 绘制刻度 103 | void drawScaleNum(QPainter *painter); // 绘制刻度值 104 | void drawPainterIndicator(QPainter *painter); // 绘制指示器 105 | void drawText(QPainter *painter); // 绘制提示文本 106 | 107 | int getCurValue() const; 108 | void updateValue(int value); // 更新表盘值 【主要在指针动画时使用】 109 | 110 | signals: 111 | void valueChanged(int value); 112 | void finished(); 113 | 114 | private: 115 | int m_radius = 0; // 仪表盘半径 116 | QColor m_outerCircleColor = QColor(172, 172, 172); // 外圆背景色 【类比于手表金属外框】 117 | QColor m_innerCircleColor = QColor(40, 40, 40); // 内圆背景色 【类比于手表玻璃显示区域】 118 | QColor m_scaleStartColor = QColor(84, 84, 84); // 刻度开始部分颜色 119 | QColor m_scaleEndColor = QColor(Qt::red); // 刻度结束部分颜色 【如:没油、转速等告警提示】 120 | QColor m_scaleNumColor = QColor(255,255,255); // 刻度值颜色 121 | QColor m_pointerColor = QColor(250, 50, 50); // 指针颜色 122 | QColor m_textColor = QColor(255,255,255); // 提示文本颜色 123 | 124 | int m_curValue = 0; // 表盘指针当前值 【主要在指针动画时使用】 125 | int m_value = 0; // 表盘指针最终指向的值 126 | int m_minValue = 0; // 表盘最小值 127 | int m_maxValue = 240; // 表盘最大值 128 | int m_startAngle = 45; // 刻度盘起始角度 129 | int m_endAngle = 315; // 刻度盘结束角度 130 | int m_scaleMajor = 10; // 大刻度数量 131 | int m_scaleMinor = 5; // 小刻度数量 【如:总刻度数量 = scaleMajor * scaleMinor】 132 | double m_scalePercentage = 0.8; // 表盘刻度划分百分比 【对应 m_scaleStartColor m_scaleEndColor】 133 | QString m_suffixText = "Km/h"; // 提示文本后缀 【如:Km/h 等】 134 | 135 | QPropertyAnimation m_animation; // 指针属性动画 136 | int m_animationStepTime = 50; // 每刻度动画持续时间 137 | QEasingCurve::Type m_easingCurveType = QEasingCurve::OutQuad; // 指针动画缓和曲线类型 138 | }; 139 | 140 | #endif // DASHBOARD_H 141 | -------------------------------------------------------------------------------- /GenericDashboard/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /GenericDashboard/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | MainWindow::MainWindow(QWidget *parent) 9 | : QWidget(parent) 10 | { 11 | QSlider *slder = new QSlider(this); 12 | 13 | GenericDashBoard *db1 = createGenericDashBoard1(); 14 | GenericDashBoard *db2 = createGenericDashBoard2(); 15 | GenericDashBoard *db3 = createGenericDashBoard3(); 16 | GenericDashBoard *db4 = createGenericDashBoard4(); 17 | 18 | connect(db1, &GenericDashBoard::valueChanged, [&](int val){qDebug() << val;}); // 打印指针移动时对应的表盘数值 19 | connect(db1, &GenericDashBoard::finished, [&](){qDebug() << "Pointer reached";}); // 打印指针到达目标数值 20 | 21 | connect(slder, &QSlider::valueChanged, db1, &GenericDashBoard::setValue); // 链接仪表盘数值更新槽 22 | connect(slder, &QSlider::valueChanged, db2, &GenericDashBoard::setValue); // 链接仪表盘数值更新槽 23 | connect(slder, &QSlider::valueChanged, db3, &GenericDashBoard::setValue); // 链接仪表盘数值更新槽 24 | connect(slder, &QSlider::valueChanged, db4, &GenericDashBoard::setValue); // 链接仪表盘数值更新槽 25 | 26 | slder->setOrientation(Qt::Horizontal); 27 | slder->setRange(db1->getMinValue(), db1->getMaxValue()); 28 | slder->setValue(db1->getValue()); 29 | 30 | QGridLayout *pLayout = new QGridLayout; 31 | pLayout->addWidget(db1, 0, 0); 32 | pLayout->addWidget(db2, 0, 1); 33 | pLayout->addWidget(db3, 1, 0); 34 | pLayout->addWidget(db4, 1, 1); 35 | pLayout->addWidget(slder, 2, 0, 1, 2); 36 | 37 | setLayout(pLayout); 38 | 39 | setMinimumSize(400, 400); 40 | } 41 | 42 | MainWindow::~MainWindow() 43 | { 44 | } 45 | 46 | GenericDashBoard *MainWindow::createGenericDashBoard1() 47 | { 48 | GenericDashBoard *ret = new GenericDashBoard(this); 49 | 50 | ret->setMinValue(0); // 设置仪表盘最小值 51 | ret->setMaxValue(240); // 设置仪表盘最大值 52 | ret->setScaleMajor(8); // 设置主刻度数量 53 | ret->setScaleMinor(10); // 设置小刻度数量 54 | ret->setStartAngle(45); // 设置仪表盘起始角度 55 | ret->setEndAngle(315); // 设置仪表盘结束角度 56 | ret->setScalePercentage(0.85); // 设置仪表盘刻度百分比 【非低电量告警特殊颜色等的正常色比例】 57 | ret->setEasingCurve(QEasingCurve::OutQuad); // 设置指针缓和曲线 58 | ret->setAnimationStepTime(80); // 设置每刻度动画持续时间 59 | 60 | return ret; 61 | } 62 | 63 | GenericDashBoard *MainWindow::createGenericDashBoard2() 64 | { 65 | GenericDashBoard *ret = new GenericDashBoard(this); 66 | 67 | ret->setAnimationStepTime(0); // 关闭动画 68 | ret->setStartAngle(0); 69 | ret->setEndAngle(270); 70 | ret->setSuffixText("W"); 71 | 72 | return ret; 73 | } 74 | 75 | GenericDashBoard *MainWindow::createGenericDashBoard3() 76 | { 77 | GenericDashBoard *ret = new GenericDashBoard(this); 78 | 79 | ret->setEasingCurve(QEasingCurve::InOutCubic); 80 | ret->setSuffixText("HZ"); 81 | 82 | return ret; 83 | } 84 | 85 | GenericDashBoard *MainWindow::createGenericDashBoard4() 86 | { 87 | GenericDashBoard *ret = new GenericDashBoard(this); 88 | 89 | ret->setEasingCurve(QEasingCurve::OutQuint); 90 | ret->setOuterCircleColor(Qt::yellow); 91 | ret->setPointerColor(Qt::blue); 92 | ret->setInnerCircleColor(Qt::green); 93 | ret->setScaleStartColor(Qt::red); 94 | ret->setScaleEndColor(Qt::white); 95 | ret->setTextColor(Qt::black); 96 | ret->setScaleNumColor(Qt::darkCyan); 97 | 98 | return ret; 99 | } 100 | 101 | -------------------------------------------------------------------------------- /GenericDashboard/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | #include "genericdashboard.h" 7 | 8 | class MainWindow : public QWidget 9 | { 10 | Q_OBJECT 11 | 12 | public: 13 | MainWindow(QWidget *parent = nullptr); 14 | ~MainWindow(); 15 | protected: 16 | GenericDashBoard *createGenericDashBoard1(); 17 | GenericDashBoard *createGenericDashBoard2(); 18 | GenericDashBoard *createGenericDashBoard3(); 19 | GenericDashBoard *createGenericDashBoard4(); 20 | }; 21 | #endif // MAINWINDOW_H 22 | -------------------------------------------------------------------------------- /GenericDashboard/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /GridWidget/GridWidget.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # You can make your code fail to compile if it uses deprecated APIs. 8 | # In order to do so, uncomment the following line. 9 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 10 | 11 | SOURCES += \ 12 | gridwidget.cpp \ 13 | main.cpp \ 14 | mainwindow.cpp 15 | 16 | HEADERS += \ 17 | gridwidget.h \ 18 | mainwindow.h 19 | 20 | # Default rules for deployment. 21 | qnx: target.path = /tmp/$${TARGET}/bin 22 | else: unix:!android: target.path = /opt/$${TARGET}/bin 23 | !isEmpty(target.path): INSTALLS += target 24 | 25 | RESOURCES += \ 26 | src.qrc 27 | -------------------------------------------------------------------------------- /GridWidget/GridWidget1_shotcut.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/GridWidget1_shotcut.gif -------------------------------------------------------------------------------- /GridWidget/GridWidget2_shotcut.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/GridWidget2_shotcut.gif -------------------------------------------------------------------------------- /GridWidget/GridWidget3_shotcut.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/GridWidget3_shotcut.gif -------------------------------------------------------------------------------- /GridWidget/gridwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "gridwidget.h" 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | GridWidget::GridWidget(QWidget *parent) : QLabel(parent) 9 | { } 10 | 11 | GridWidget::~GridWidget() 12 | { } 13 | 14 | bool GridWidget::eventFilter(QObject *object, QEvent *event) 15 | { 16 | for (int i=0; i(event); 21 | if (me != nullptr) 22 | { 23 | if (me->type() == QEvent::MouseButtonPress && m_moveIndex == -1) 24 | { 25 | m_moveIndex = i; 26 | m_offsetPos = me->pos(); 27 | m_movePos = m_vector[i]->pos(); 28 | 29 | if (m_isAnimation) 30 | m_vector[i]->setFixedSize(m_size.width()*1.3, m_size.height()*1.3); 31 | } 32 | else if (me->type() == QEvent::MouseButtonRelease && m_moveIndex != -1) 33 | { 34 | if (m_isAnimation) 35 | m_vector[i]->setFixedSize(m_size.width(), m_size.height()); 36 | m_vector[m_moveIndex]->move(m_movePos); 37 | m_moveIndex = -1; 38 | } 39 | break; 40 | } 41 | } 42 | } 43 | 44 | return QWidget::eventFilter(object, event); 45 | } 46 | 47 | void GridWidget::mouseMoveEvent(QMouseEvent *event) 48 | { 49 | if (m_moveIndex != -1) 50 | { 51 | m_vector[m_moveIndex]->move(event->pos() - m_offsetPos); 52 | 53 | for (int i=0; igeometry().contains(event->pos())) 56 | { 57 | QPoint ptmp = m_vector[i]->pos(); 58 | m_vector[i]->move(m_movePos); 59 | m_movePos = ptmp; 60 | 61 | QWidget *tmp = m_vector[m_moveIndex]; 62 | m_vector[m_moveIndex] = m_vector[i]; 63 | m_vector[i] = tmp; 64 | 65 | m_moveIndex = i; 66 | 67 | break; 68 | } 69 | } 70 | } 71 | 72 | return QWidget::mouseMoveEvent(event); 73 | } 74 | 75 | void GridWidget::resizeEvent(QResizeEvent *event) 76 | { 77 | rearrangeSubWidget(); 78 | 79 | return QWidget::resizeEvent(event); 80 | } 81 | 82 | QMargins GridWidget::margins() const 83 | { 84 | return m_margins; 85 | } 86 | 87 | QSize GridWidget::size() const 88 | { 89 | return m_size; 90 | } 91 | 92 | QSize GridWidget::spacing() const 93 | { 94 | return m_spacing; 95 | } 96 | 97 | int GridWidget::count() const 98 | { 99 | return m_vector.size(); 100 | } 101 | 102 | bool GridWidget::contains(QWidget *widget) const 103 | { 104 | return m_vector.contains(widget); 105 | } 106 | 107 | bool GridWidget::contains(const QString &objectName) const 108 | { 109 | return (indexOf(objectName) == -1 ? false : true); 110 | } 111 | 112 | void GridWidget::append(QWidget* widget) 113 | { 114 | widget->setFixedSize(m_size); 115 | widget->setParent(this); 116 | widget->installEventFilter(this); 117 | m_vector.append(widget); 118 | rearrangeSubWidget(); 119 | } 120 | 121 | void GridWidget::setSize(QSize size) 122 | { 123 | m_size = size; 124 | 125 | rearrangeSubWidget(); 126 | } 127 | 128 | void GridWidget::setMargins(QMargins margins) 129 | { 130 | m_margins = margins; 131 | 132 | rearrangeSubWidget(); 133 | } 134 | 135 | void GridWidget::setSpacing(QSize spaceing) 136 | { 137 | m_spacing = spaceing; 138 | 139 | rearrangeSubWidget(); 140 | } 141 | 142 | void GridWidget::setRowSpacing(int spaceing) 143 | { 144 | m_spacing.setHeight(spaceing); 145 | 146 | rearrangeSubWidget(); 147 | } 148 | 149 | void GridWidget::setColumnSpacing(int spaceing) 150 | { 151 | m_spacing.setWidth(spaceing); 152 | 153 | rearrangeSubWidget(); 154 | } 155 | 156 | int GridWidget::indexOf(QWidget* widget, int from) const 157 | { 158 | return m_vector.indexOf(widget, from); 159 | } 160 | 161 | int GridWidget::indexOf(const QString &objectName, int from) const 162 | { 163 | for (int i=0; i= from && m_vector[i]->objectName() == objectName) 166 | return i; 167 | } 168 | 169 | return -1; 170 | } 171 | 172 | void GridWidget::insert(int i, QWidget* widget) 173 | { 174 | widget->setFixedSize(m_size); 175 | widget->setParent(this); 176 | widget->installEventFilter(this); 177 | m_vector.insert(i, widget); 178 | rearrangeSubWidget(); 179 | } 180 | 181 | const QWidget *GridWidget::at(int index) const 182 | { 183 | return m_vector.at(index); 184 | } 185 | 186 | QWidget *GridWidget::takeAt(int index) 187 | { 188 | QWidget *ret = nullptr; 189 | 190 | ret = m_vector.takeAt(index); 191 | 192 | rearrangeSubWidget(); 193 | 194 | return ret; 195 | } 196 | 197 | void GridWidget::removeAt(int index) 198 | { 199 | delete m_vector[index]; 200 | 201 | m_vector.removeAt(index); 202 | 203 | rearrangeSubWidget(); 204 | } 205 | 206 | bool GridWidget::removeOne(QWidget *widget) 207 | { 208 | bool ret = false; 209 | 210 | ret = m_vector.removeOne(widget); 211 | 212 | if (ret) 213 | delete widget; 214 | 215 | rearrangeSubWidget(); 216 | 217 | return ret; 218 | } 219 | 220 | bool GridWidget::removeOne(const QString &objectName) 221 | { 222 | bool ret = false; 223 | 224 | for (auto item : m_vector) 225 | { 226 | if (item->objectName() == objectName) 227 | { 228 | ret = m_vector.removeOne(item); 229 | 230 | if (ret) 231 | delete item; 232 | 233 | rearrangeSubWidget(); 234 | 235 | break; 236 | } 237 | } 238 | 239 | return ret; 240 | } 241 | 242 | void GridWidget::clear() 243 | { 244 | qDeleteAll(m_vector); 245 | 246 | m_vector.clear(); 247 | 248 | rearrangeSubWidget(); 249 | } 250 | 251 | void GridWidget::setAnimationEnabled(bool enable) 252 | { 253 | m_isAnimation = enable; 254 | } 255 | 256 | void GridWidget::rearrangeSubWidget() 257 | { 258 | int w = this->width(); 259 | 260 | int x_start = m_margins.left(); 261 | int x_end = w - m_margins.right(); 262 | 263 | int y_start = m_margins.top(); 264 | 265 | int x = x_start; 266 | int y = y_start; 267 | 268 | int rows = 0; 269 | for (auto item : m_vector) 270 | { 271 | item->move(x, y); 272 | 273 | x += m_size.width() + m_spacing.width(); 274 | 275 | if ((x + m_size.width()/2) >= x_end && item != m_vector.last()) 276 | { 277 | ++ rows; 278 | x = x_start; 279 | y += m_size.height() + m_spacing.height(); 280 | } 281 | } 282 | 283 | setMinimumHeight(m_margins.top() + m_margins.bottom() + (rows+1) * (m_size.height()+m_spacing.height()) - m_spacing.height()); 284 | } 285 | -------------------------------------------------------------------------------- /GridWidget/gridwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef GRIDWIDGET_H 2 | #define GRIDWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /* GridWidget 实现的功能 10 | * 1. 可任意插入、追加 QWidget 类组控件 [推荐QLabel] 11 | * 2. 可自由拖动子组件,并保持移动后所有子相对组件位置不变 12 | * 3. 可设置子组件大小、子组件行列间距和子组件与父组件边框的间距 13 | * 4. 可自动根据父组件宽度调整子组件布局 14 | * 5. 可对父组件管理的子组件进行查找、移除等操作 15 | */ 16 | 17 | /* 18 | * 说明:当父组件开始管理子组件时,子组件的生命周期便交由父组件管理,外部不可进行随意 delete 操作 19 | * 可使用 removeAt removeOne clear 进行处理 20 | * 说明:子组件原功能不受任何影响,因此可单独配置后结合父组件实现更丰富的功能 21 | */ 22 | 23 | class GridWidget : public QLabel 24 | { 25 | Q_OBJECT 26 | 27 | Q_PROPERTY(QMargins margins READ margins WRITE setMargins) 28 | Q_PROPERTY(QSize size READ size WRITE setSize) 29 | Q_PROPERTY(QSize spacing READ spacing WRITE setSpacing) 30 | 31 | public: 32 | explicit GridWidget(QWidget *parent = nullptr); 33 | ~GridWidget(); 34 | 35 | QMargins margins() const; // 返回子组件与父组件间距 36 | QSize size() const; // 返回子组件大小 37 | QSize spacing() const; // 返回子组件间行、列间距 38 | 39 | int count() const; // 返回当前父组件中子组件的数量 40 | bool contains(QWidget *widget) const; // 返回当前父组件中是否包含此组件 (参数:widget 地址) 41 | bool contains(const QString &objectName) const; // 返回当前父组件中是否包含此组件 (参数:widget objectName) 42 | int indexOf(QWidget* widget, int from = 0) const; // 返回值在父组件中第一次出现的索引位置,从索引位置向前搜索 (参数:widget 地址) 43 | int indexOf(const QString &objectName, int from = 0) const; // 返回值在父组件中第一次出现的索引位置,从索引位置向前搜索 (参数:widget objectName) 44 | const QWidget *at(int index) const; // 返回父组件中索引位置i处的项 45 | 46 | public slots: 47 | void append(QWidget* widget); // 将参数组件附加到父组件。 48 | void setSize(QSize size); // 设置子组件大小 49 | void setMargins(QMargins margins); // 设置子组件与父组件的边距 50 | void setSpacing(QSize spaceing); // 设置子组件间的行、列距离 51 | void setRowSpacing(int spaceing); // 设置子组件的行间距 52 | void setColumnSpacing(int spaceing); // 设置子组件的列间距 53 | void insert(int i, QWidget* widget); // 在指定索引处插入参数组件 54 | QWidget *takeAt(int index); // 删除索引位置i处的子组件并返回它 55 | void removeAt(int index); // 移除指定索引处的子组件 56 | bool removeOne(QWidget *widget); // 移除指定索引处的子组件 (参数:widget 地址) 57 | bool removeOne(const QString &objectName); // 移除指定索引处的子组件 (参数:widget objectName) 58 | void clear(); // 清空父组件中的所有子组件 59 | void setAnimationEnabled(bool enable); // 设置鼠标点击动画使能 60 | 61 | protected: 62 | bool eventFilter(QObject *object, QEvent *event) override; 63 | void mouseMoveEvent(QMouseEvent *event) override; 64 | void resizeEvent(QResizeEvent *event) override; 65 | 66 | void rearrangeSubWidget(); 67 | 68 | private: 69 | QMargins m_margins = {10, 10, 10, 10}; 70 | QSize m_size = {60, 60}; 71 | QSize m_spacing = {10, 10}; 72 | QVector m_vector; 73 | 74 | bool m_isAnimation = false; 75 | 76 | QPoint m_movePos; 77 | QPoint m_offsetPos; 78 | 79 | int m_moveIndex = -1; 80 | }; 81 | 82 | #endif // GRIDWIDGET_H 83 | -------------------------------------------------------------------------------- /GridWidget/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /GridWidget/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "gridwidget.h" 10 | 11 | MainWindow::MainWindow(QWidget *parent) 12 | : QWidget(parent) 13 | { 14 | //============================================================ 15 | GridWidget *pGridWidget = new GridWidget(this); 16 | 17 | pGridWidget->resize(800, 600); // 设置父组件大小 18 | pGridWidget->setSpacing(QSize(40, 40)); // 设置子组件行、列间距 19 | pGridWidget->setMargins(QMargins(15, 20, 15, 15)); // 设置子组件与父组件的边距 20 | pGridWidget->setSize(QSize(60, 60)); // 设置子组件大小 21 | pGridWidget->setAnimationEnabled(true); // 开启鼠标点击动画 22 | 23 | QMovie *movie = new QMovie(":/image/src/background.gif"); // 设置父组件背景(支持静态图和动态图) 24 | movie->start(); 25 | pGridWidget->setMovie(movie); 26 | 27 | for (int i=0; i<10; ++i) 28 | { 29 | QLabel *pLabel = new QLabel(pGridWidget); 30 | pLabel->setPixmap(QPixmap(QString(":/image/src/%1.png").arg(i, 2, 10, QLatin1Char('0')))); 31 | pLabel->setScaledContents(true); 32 | pGridWidget->append(pLabel); 33 | } 34 | 35 | QLabel *pLabel = new QLabel(pGridWidget); 36 | pLabel->setFixedSize(60, 60); 37 | pLabel->setScaledContents(true); 38 | QMovie *movie1 = new QMovie(":/image/src/icon.gif"); 39 | movie1->start(); 40 | pLabel->setMovie(movie1); 41 | pGridWidget->insert(0, pLabel); 42 | 43 | //============================================================ 44 | 45 | QVBoxLayout *pLayout = new QVBoxLayout; 46 | pLayout->addWidget(pGridWidget); 47 | setLayout(pLayout); 48 | 49 | // setStyleSheet("QLabel:hover {border: 2px solid white;border-radius:5px;}"); 50 | } 51 | 52 | MainWindow::~MainWindow() 53 | { } 54 | 55 | -------------------------------------------------------------------------------- /GridWidget/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | class MainWindow : public QWidget 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | MainWindow(QWidget *parent = nullptr); 12 | ~MainWindow(); 13 | }; 14 | #endif // MAINWINDOW_H 15 | -------------------------------------------------------------------------------- /GridWidget/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | src/icon.gif 4 | src/02.png 5 | src/03.png 6 | src/04.png 7 | src/08.png 8 | src/09.png 9 | src/10.png 10 | src/11.png 11 | src/12.png 12 | src/13.png 13 | src/14.png 14 | src/18.png 15 | src/20.png 16 | src/22.png 17 | src/background.gif 18 | src/01.png 19 | src/05.png 20 | src/06.png 21 | src/07.png 22 | src/15.png 23 | src/16.png 24 | src/17.png 25 | src/19.png 26 | src/21.png 27 | src/00.png 28 | 29 | 30 | -------------------------------------------------------------------------------- /GridWidget/src/00.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/00.png -------------------------------------------------------------------------------- /GridWidget/src/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/01.png -------------------------------------------------------------------------------- /GridWidget/src/02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/02.png -------------------------------------------------------------------------------- /GridWidget/src/03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/03.png -------------------------------------------------------------------------------- /GridWidget/src/04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/04.png -------------------------------------------------------------------------------- /GridWidget/src/05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/05.png -------------------------------------------------------------------------------- /GridWidget/src/06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/06.png -------------------------------------------------------------------------------- /GridWidget/src/07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/07.png -------------------------------------------------------------------------------- /GridWidget/src/08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/08.png -------------------------------------------------------------------------------- /GridWidget/src/09.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/09.png -------------------------------------------------------------------------------- /GridWidget/src/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/10.png -------------------------------------------------------------------------------- /GridWidget/src/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/11.png -------------------------------------------------------------------------------- /GridWidget/src/12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/12.png -------------------------------------------------------------------------------- /GridWidget/src/13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/13.png -------------------------------------------------------------------------------- /GridWidget/src/14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/14.png -------------------------------------------------------------------------------- /GridWidget/src/15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/15.png -------------------------------------------------------------------------------- /GridWidget/src/16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/16.png -------------------------------------------------------------------------------- /GridWidget/src/17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/17.png -------------------------------------------------------------------------------- /GridWidget/src/18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/18.png -------------------------------------------------------------------------------- /GridWidget/src/19.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/19.png -------------------------------------------------------------------------------- /GridWidget/src/20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/20.png -------------------------------------------------------------------------------- /GridWidget/src/21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/21.png -------------------------------------------------------------------------------- /GridWidget/src/22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/22.png -------------------------------------------------------------------------------- /GridWidget/src/background.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/background.gif -------------------------------------------------------------------------------- /GridWidget/src/icon.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/GridWidget/src/icon.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QtCustomcomponent 2 | 3 | > 详细说明 4 | 5 | https://segmentfault.com/a/1190000040566416 6 | 7 | -------------------------------------------------------------------------------- /SliderWidget/SliderWidget.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # You can make your code fail to compile if it uses deprecated APIs. 8 | # In order to do so, uncomment the following line. 9 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 10 | 11 | SOURCES += \ 12 | main.cpp \ 13 | sliderWidget.cpp 14 | 15 | HEADERS += \ 16 | sliderWidget.h 17 | 18 | # Default rules for deployment. 19 | qnx: target.path = /tmp/$${TARGET}/bin 20 | else: unix:!android: target.path = /opt/$${TARGET}/bin 21 | !isEmpty(target.path): INSTALLS += target 22 | 23 | RESOURCES += \ 24 | res.qrc 25 | -------------------------------------------------------------------------------- /SliderWidget/main.cpp: -------------------------------------------------------------------------------- 1 | #include "sliderWidget.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | // 简单随意的测试布局 11 | QWidget *createPage1() 12 | { 13 | QWidget *ret = new QWidget; 14 | QLabel *apps[11]; 15 | 16 | ret->setStyleSheet("background-image:url(:/resource/imgs/background3.png);"); 17 | 18 | for (int i=0; i<6; ++i) 19 | { 20 | apps[i] = new QLabel(ret); 21 | apps[i]->setScaledContents(true); 22 | apps[i]->setFixedSize(100, 100); 23 | apps[i]->setScaledContents(true); 24 | apps[i]->setPixmap(QString(":/resource/imgs/(%1).jpg").arg(i)); 25 | apps[i]->move(i * 130 + 25, 25); 26 | apps[i]->setToolTip("点我"); 27 | } 28 | 29 | for (int i=6; i<11; ++i) 30 | { 31 | apps[i] = new QLabel(ret); 32 | apps[i]->setScaledContents(true); 33 | apps[i]->setFixedSize(100, 100); 34 | apps[i]->setScaledContents(true); 35 | apps[i]->setPixmap(QString(":/resource/imgs/(%1).jpg").arg(i)); 36 | apps[i]->move((i-6) * 130 + 25, 150); 37 | apps[i]->setToolTip("点我"); 38 | } 39 | 40 | return ret; 41 | } 42 | 43 | // 简单随意的测试布局 44 | QWidget *createPage2() 45 | { 46 | QWidget *ret = new QWidget; 47 | QLabel *apps[11]; 48 | 49 | ret->setStyleSheet("background-image:url(:/resource/imgs/background1.png);"); 50 | 51 | for (int i=11; i<17; ++i) 52 | { 53 | apps[i-11] = new QLabel(ret); 54 | apps[i-11]->setScaledContents(true); 55 | apps[i-11]->setFixedSize(100, 100); 56 | apps[i-11]->setScaledContents(true); 57 | apps[i-11]->setPixmap(QString(":/resource/imgs/(%1).jpg").arg(i)); 58 | apps[i-11]->move((i-11) * 130 + 25, 25); 59 | apps[i-11]->setToolTip("点我"); 60 | } 61 | 62 | for (int i=17; i<21; ++i) 63 | { 64 | apps[i-17] = new QLabel(ret); 65 | apps[i-17]->setScaledContents(true); 66 | apps[i-17]->setFixedSize(100, 100); 67 | apps[i-17]->setScaledContents(true); 68 | apps[i-17]->setPixmap(QString(":/resource/imgs/(%1).jpg").arg(i)); 69 | apps[i-17]->move((i-17) * 130 + 25, 150); 70 | apps[i-17]->setToolTip("点我"); 71 | } 72 | 73 | return ret; 74 | } 75 | 76 | // 简单随意的测试布局 77 | QWidget *createPage3() 78 | { 79 | QWidget *ret = new QWidget; 80 | QLabel *apps[11]; 81 | 82 | ret->setStyleSheet("background-image:url(:/resource/imgs/background2.png);"); 83 | 84 | for (int i=21; i<27; ++i) 85 | { 86 | apps[i-21] = new QLabel(ret); 87 | apps[i-21]->setScaledContents(true); 88 | apps[i-21]->setFixedSize(100, 100); 89 | apps[i-21]->setScaledContents(true); 90 | apps[i-21]->setPixmap(QString(":/resource/imgs/(%1).jpg").arg(i)); 91 | apps[i-21]->move((i-21) * 130 + 25, 25); 92 | apps[i-21]->setToolTip("点我"); 93 | } 94 | 95 | for (int i=27; i<30; ++i) 96 | { 97 | apps[i-27] = new QLabel(ret); 98 | apps[i-27]->setScaledContents(true); 99 | apps[i-27]->setFixedSize(100, 100); 100 | apps[i-27]->setScaledContents(true); 101 | apps[i-27]->setPixmap(QString(":/resource/imgs/(%1).jpg").arg(i)); 102 | apps[i-27]->move((i-27) * 130 + 25, 150); 103 | apps[i-27]->setToolTip("点我"); 104 | } 105 | 106 | QMovie *movie = new QMovie(":/resource/imgs/icon.gif"); 107 | QLabel *p = new QLabel(ret); 108 | p->setScaledContents(true); 109 | p->setFixedSize(100, 100); 110 | p->setScaledContents(true); 111 | p->setMovie(movie); 112 | p->move((30-27) * 130 + 25, 150); 113 | movie->start(); 114 | 115 | return ret; 116 | } 117 | 118 | int main(int argc, char *argv[]) 119 | { 120 | QApplication a(argc, argv); 121 | 122 | SliderWidget w(nullptr); 123 | //SliderWidget w(nullptr, ":/resource/imgs/background2.png"); 124 | 125 | w.addWidget(createPage1()); 126 | w.addWidget(createPage2()); 127 | w.addWidget(createPage3()); 128 | 129 | w.setWindowIcon(QIcon(":/resource/imgs/logo.png")); 130 | w.setWindowTitle(" 自定义滑动组件"); 131 | w.setFixedSize(800, 480); 132 | w.show(); 133 | 134 | return a.exec(); 135 | } 136 | -------------------------------------------------------------------------------- /SliderWidget/res.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | resource/icons/indicatorActive.png 4 | resource/icons/indicatorInactive.png 5 | resource/imgs/(1).jpg 6 | resource/imgs/(2).jpg 7 | resource/imgs/(3).jpg 8 | resource/imgs/(4).jpg 9 | resource/imgs/(5).jpg 10 | resource/imgs/(6).jpg 11 | resource/imgs/(7).jpg 12 | resource/imgs/(8).jpg 13 | resource/imgs/(9).jpg 14 | resource/imgs/(10).jpg 15 | resource/imgs/(11).jpg 16 | resource/imgs/(12).jpg 17 | resource/imgs/(13).jpg 18 | resource/imgs/(14).jpg 19 | resource/imgs/(15).jpg 20 | resource/imgs/(16).jpg 21 | resource/imgs/(17).jpg 22 | resource/imgs/(18).jpg 23 | resource/imgs/(19).jpg 24 | resource/imgs/(20).jpg 25 | resource/imgs/(21).jpg 26 | resource/imgs/(22).jpg 27 | resource/imgs/(23).jpg 28 | resource/imgs/(24).jpg 29 | resource/imgs/(25).jpg 30 | resource/imgs/(26).jpg 31 | resource/imgs/(27).jpg 32 | resource/imgs/(28).jpg 33 | resource/imgs/(29).jpg 34 | resource/imgs/(30).jpg 35 | resource/imgs/(31).jpg 36 | resource/imgs/(32).jpg 37 | resource/imgs/(33).jpg 38 | resource/imgs/(34).jpg 39 | resource/imgs/(35).jpg 40 | resource/imgs/(36).jpg 41 | resource/imgs/(37).jpg 42 | resource/imgs/(38).jpg 43 | resource/imgs/(39).jpg 44 | resource/imgs/(40).jpg 45 | resource/imgs/(41).jpg 46 | resource/imgs/(42).jpg 47 | resource/imgs/(43).jpg 48 | resource/imgs/(44).jpg 49 | resource/imgs/(45).jpg 50 | resource/imgs/(46).jpg 51 | resource/imgs/(47).jpg 52 | resource/imgs/(48).jpg 53 | resource/imgs/(49).jpg 54 | resource/imgs/(50).jpg 55 | resource/imgs/(51).jpg 56 | resource/imgs/(52).jpg 57 | resource/imgs/(53).jpg 58 | resource/imgs/(54).jpg 59 | resource/imgs/(55).jpg 60 | resource/imgs/(56).jpg 61 | resource/imgs/(57).jpg 62 | resource/imgs/(58).jpg 63 | resource/imgs/(59).jpg 64 | resource/imgs/(60).jpg 65 | resource/imgs/(0).jpg 66 | resource/imgs/background1.png 67 | resource/imgs/background2.png 68 | resource/imgs/background3.png 69 | resource/imgs/icon.gif 70 | resource/imgs/logo.png 71 | resource/imgs/background.png 72 | 73 | 74 | -------------------------------------------------------------------------------- /SliderWidget/resource/icons/indicatorActive.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/icons/indicatorActive.png -------------------------------------------------------------------------------- /SliderWidget/resource/icons/indicatorInactive.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/icons/indicatorInactive.png -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(0).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(0).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(1).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(1).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(10).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(10).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(11).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(11).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(12).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(12).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(13).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(13).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(14).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(14).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(15).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(15).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(16).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(16).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(17).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(17).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(18).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(18).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(19).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(19).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(2).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(2).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(20).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(20).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(21).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(21).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(22).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(22).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(23).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(23).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(24).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(24).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(25).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(25).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(26).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(26).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(27).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(27).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(28).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(28).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(29).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(29).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(3).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(3).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(30).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(30).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(31).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(31).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(32).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(32).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(33).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(33).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(34).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(34).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(35).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(35).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(36).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(36).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(37).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(37).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(38).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(38).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(39).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(39).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(4).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(4).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(40).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(40).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(41).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(41).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(42).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(42).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(43).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(43).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(44).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(44).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(45).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(45).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(46).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(46).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(47).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(47).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(48).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(48).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(49).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(49).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(5).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(5).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(50).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(50).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(51).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(51).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(52).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(52).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(53).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(53).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(54).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(54).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(55).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(55).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(56).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(56).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(57).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(57).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(58).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(58).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(59).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(59).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(6).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(6).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(60).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(60).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(7).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(7).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(8).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(8).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/(9).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/(9).jpg -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/background.png -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/background1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/background1.png -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/background2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/background2.png -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/background3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/background3.png -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/icon.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/icon.gif -------------------------------------------------------------------------------- /SliderWidget/resource/imgs/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/resource/imgs/logo.png -------------------------------------------------------------------------------- /SliderWidget/slider1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/slider1.gif -------------------------------------------------------------------------------- /SliderWidget/slider2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/SliderWidget/slider2.gif -------------------------------------------------------------------------------- /SliderWidget/sliderWidget.cpp: -------------------------------------------------------------------------------- 1 | #include "sliderWidget.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | SliderWidget::SliderWidget(QWidget *parent, const QString &picture) 11 | : QWidget(parent) 12 | { 13 | m_background = picture; 14 | 15 | initUi(); 16 | initCtrl(); 17 | } 18 | 19 | SliderWidget::~SliderWidget() 20 | { 21 | } 22 | 23 | void SliderWidget::initUi() 24 | { 25 | this->setAttribute(Qt::WA_TranslucentBackground); 26 | 27 | m_pMainWidget = new QWidget(this); 28 | m_pMainWidget->setStyleSheet("background: transparent"); 29 | 30 | m_pScrollArea = new QScrollArea(this); 31 | m_pScrollArea->setAlignment(Qt::AlignCenter); 32 | m_pScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 33 | m_pScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 34 | m_pScrollArea->setWidget(m_pMainWidget); 35 | if (m_background.isEmpty()) 36 | m_pScrollArea->setStyleSheet("background: transparent"); 37 | else 38 | m_pScrollArea->setStyleSheet(QString("background-image:url(%1);").arg(m_background)); 39 | 40 | auto scroller = QScroller::scroller(m_pScrollArea); 41 | scroller->grabGesture(m_pScrollArea, QScroller::LeftMouseButtonGesture); 42 | 43 | auto properties = scroller->scrollerProperties(); 44 | properties.setScrollMetric(QScrollerProperties::SnapTime, 0.5); 45 | properties.setScrollMetric(QScrollerProperties::MinimumVelocity, 10); 46 | scroller->setScrollerProperties(properties); 47 | 48 | m_pMainLayout = new QHBoxLayout; 49 | m_pMainLayout->setMargin(0); 50 | m_pMainLayout->setSpacing(0); 51 | m_pMainWidget->setLayout(m_pMainLayout); 52 | 53 | m_pIndicatorWidget = new QWidget(this); 54 | m_pIndicatorLayout = new QHBoxLayout; 55 | m_pIndicatorLayout->setAlignment(Qt::AlignCenter); 56 | m_pIndicatorLayout->setMargin(0); 57 | m_pIndicatorWidget->setLayout(m_pIndicatorLayout); 58 | } 59 | 60 | void SliderWidget::initCtrl() 61 | { 62 | m_pAnimation = new QPropertyAnimation(m_pScrollArea->horizontalScrollBar(), "value", this); 63 | m_pAnimation->setDuration(200); 64 | m_pAnimation->setEasingCurve(QEasingCurve::OutCurve); 65 | 66 | m_pSlidingTimer = new QTimer(this); 67 | m_pSlidingTimer->setInterval(300); 68 | 69 | connect(QScroller::scroller(m_pScrollArea), &QScroller::stateChanged, this, &SliderWidget::onScrollerStateChanged); 70 | connect(m_pSlidingTimer, &QTimer::timeout, this, &SliderWidget::onSliderTimerTimeout); 71 | } 72 | 73 | void SliderWidget::updateIndicator(int index) 74 | { 75 | const QObjectList &childs = m_pIndicatorWidget->children(); 76 | for (int i=1; i(childs[i]); 79 | 80 | if (i-1 == index) 81 | pIndicatorLbl->setPixmap(QPixmap(":/resource/icons/indicatorActive.png")); 82 | else 83 | pIndicatorLbl->setPixmap(QPixmap(":/resource/icons/indicatorInactive.png")); 84 | } 85 | } 86 | 87 | int SliderWidget::addWidget(QWidget *page) 88 | { 89 | if (!m_background.isEmpty()) 90 | page->setStyleSheet("background: transparent"); 91 | 92 | m_pMainLayout->addWidget(page); 93 | 94 | auto pIndicatorLbl = new QLabel; 95 | m_pIndicatorLayout->addWidget(pIndicatorLbl); 96 | 97 | if (count() == 1) 98 | pIndicatorLbl->setPixmap(QPixmap(":/resource/icons/indicatorActive.png")); 99 | else 100 | pIndicatorLbl->setPixmap(QPixmap(":/resource/icons/indicatorInactive.png")); 101 | 102 | return count() - 1; 103 | } 104 | 105 | int SliderWidget::count() const 106 | { 107 | return m_pMainLayout->count(); 108 | } 109 | 110 | int SliderWidget::currentIndex() const 111 | { 112 | return m_curPageIndex; 113 | } 114 | 115 | QWidget *SliderWidget::currentWidget() const 116 | { 117 | if (count() == 0) 118 | return nullptr; 119 | 120 | return qobject_cast(m_pMainWidget->children()[m_curPageIndex+1]); 121 | } 122 | 123 | int SliderWidget::indexOf(QWidget *widget) const 124 | { 125 | auto index = m_pMainWidget->children().indexOf(widget); 126 | 127 | return index == -1 ? -1 : index -1; 128 | } 129 | 130 | void SliderWidget::setCurrentIndex(int index) 131 | { 132 | if (index < 0 || index >= count()) 133 | return; 134 | 135 | m_pAnimation->setStartValue(m_pScrollArea->horizontalScrollBar()->value()); 136 | m_pAnimation->setEndValue(index * this->width()); 137 | m_pAnimation->start(); 138 | 139 | m_curPageIndex = index; 140 | updateIndicator(m_curPageIndex); 141 | emit currentChanged(m_curPageIndex); 142 | } 143 | 144 | void SliderWidget::setCurrentWidget(QWidget *widget) 145 | { 146 | auto index = m_pMainWidget->children().indexOf(widget); 147 | 148 | if (index == -1) 149 | return; 150 | 151 | setCurrentIndex(index - 1); 152 | } 153 | 154 | void SliderWidget::removeWidget(int index) 155 | { 156 | if (index < 0 || index >= count() || count() <= 1) 157 | return; 158 | 159 | auto widget = qobject_cast(m_pMainWidget->children()[index+1]); 160 | auto indicator = qobject_cast(m_pIndicatorWidget->children()[index+1]); 161 | 162 | m_pMainLayout->removeWidget(widget); 163 | m_pIndicatorLayout->removeWidget(indicator); 164 | 165 | delete widget; 166 | delete indicator; 167 | 168 | setCurrentIndex(0); 169 | } 170 | 171 | void SliderWidget::removeWidget(QWidget *widget) 172 | { 173 | removeWidget(indexOf(widget)); 174 | } 175 | 176 | void SliderWidget::resizeEvent(QResizeEvent *ev) 177 | { 178 | Q_UNUSED(ev) 179 | 180 | m_pScrollArea->resize(this->size()); 181 | m_pMainWidget->resize(this->width() * m_pMainLayout->count(), this->height() - 2); 182 | m_pIndicatorWidget->setGeometry(0, this->height() - 20, this->width(), 20); 183 | 184 | QWidget::resizeEvent(ev); 185 | } 186 | 187 | void SliderWidget::onScrollerStateChanged(QScroller::State state) 188 | { 189 | if (count() == 0) 190 | return; 191 | 192 | if (state == QScroller::State::Inactive) 193 | { 194 | m_pSlidingTimer->stop(); 195 | m_sliderReleaseValue = QCursor::pos().x(); 196 | if (m_sliderReleaseValue == m_sliderPressedValue) 197 | return; 198 | 199 | if (!m_sliderFlag) 200 | { 201 | if (m_sliderPressedValue - m_sliderReleaseValue > 20) 202 | ++m_nextPageIndex; 203 | else if (m_sliderReleaseValue - m_sliderPressedValue > 20) 204 | --m_nextPageIndex; 205 | 206 | if (m_nextPageIndex > count()) 207 | m_nextPageIndex = count() - 1; 208 | else if (m_nextPageIndex < 0) 209 | m_nextPageIndex = 0; 210 | } 211 | else 212 | { 213 | int value = m_pScrollArea->horizontalScrollBar()->value(); 214 | m_nextPageIndex = (value / this->width()) + (value % this->width() / (this->width() / 2)); 215 | } 216 | 217 | if (m_nextPageIndex != m_curPageIndex) 218 | { 219 | m_curPageIndex = m_nextPageIndex; 220 | updateIndicator(m_curPageIndex); 221 | emit currentChanged(m_curPageIndex); 222 | } 223 | 224 | m_pAnimation->setStartValue(m_pScrollArea->horizontalScrollBar()->value()); 225 | m_pAnimation->setEndValue(m_nextPageIndex * this->width()); 226 | m_pAnimation->start(); 227 | 228 | m_sliderReleaseValue = 0; 229 | m_sliderPressedValue = 0; 230 | m_sliderFlag = 0; 231 | } 232 | 233 | if (state == QScroller::State::Pressed) 234 | { 235 | m_nextPageIndex = m_curPageIndex = m_pScrollArea->horizontalScrollBar()->value() / this->width(); 236 | m_sliderPressedValue = QCursor::pos().x(); 237 | m_pSlidingTimer->start(); 238 | } 239 | } 240 | 241 | void SliderWidget::onSliderTimerTimeout() 242 | { 243 | m_pSlidingTimer->stop(); 244 | m_sliderFlag = true; 245 | } 246 | -------------------------------------------------------------------------------- /SliderWidget/sliderWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | /* 滑动窗口 12 | * 1. 支持小部件追加 13 | * 2. 支持获取小部件的数量 14 | * 3. 支持获取当前小部件的索引和小部分地址 15 | * 4. 支持查询给定的小部件索引 16 | * 5. 支持设置首页小部件 17 | * 6. 支持小部件移除 18 | * 7. 支持背景图片是否可滑动切换 !!! 19 | * SliderWidget(QWidget *parent = nullptr, const QString &picture = ""); 20 | * 当 picture 传入图片地址时, 背景图片将一直保持不可切换 21 | */ 22 | 23 | class SliderWidget : public QWidget 24 | { 25 | Q_OBJECT 26 | 27 | public: 28 | SliderWidget(QWidget *parent = nullptr, const QString &picture = ""); 29 | ~SliderWidget(); 30 | 31 | int addWidget(QWidget *page); 32 | int count() const; 33 | int currentIndex() const; 34 | QWidget *currentWidget() const; 35 | int indexOf(QWidget *widget) const; 36 | 37 | public slots: 38 | void setCurrentIndex(int index); 39 | void setCurrentWidget(QWidget *widget); 40 | 41 | void removeWidget(int index); 42 | void removeWidget(QWidget *widget); 43 | 44 | protected: 45 | void resizeEvent(QResizeEvent *ev) override; 46 | 47 | protected slots: 48 | void onScrollerStateChanged(QScroller::State state); 49 | void onSliderTimerTimeout(); 50 | 51 | signals: 52 | void currentChanged(int index); 53 | 54 | private: 55 | void initUi(); 56 | void initCtrl(); 57 | void updateIndicator(int index); 58 | 59 | private: 60 | QWidget *m_pMainWidget; 61 | QScrollArea *m_pScrollArea; 62 | QHBoxLayout *m_pMainLayout; 63 | QWidget *m_pIndicatorWidget; 64 | QHBoxLayout *m_pIndicatorLayout; 65 | 66 | QPropertyAnimation *m_pAnimation; 67 | QTimer *m_pSlidingTimer; 68 | 69 | bool m_sliderFlag = false; 70 | int m_sliderPressedValue = 0; 71 | int m_sliderReleaseValue = 0; 72 | int m_curPageIndex = -1; 73 | int m_nextPageIndex = -1; 74 | 75 | QString m_background; 76 | }; 77 | #endif // MAINWINDOW_H 78 | -------------------------------------------------------------------------------- /WaveProgressBar/WareProgressBar_Screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/WaveProgressBar/WareProgressBar_Screenshot.gif -------------------------------------------------------------------------------- /WaveProgressBar/WareProgressBar_shorcut.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/WaveProgressBar/WareProgressBar_shorcut.gif -------------------------------------------------------------------------------- /WaveProgressBar/WaveProgressBar.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # You can make your code fail to compile if it uses deprecated APIs. 8 | # In order to do so, uncomment the following line. 9 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 10 | 11 | SOURCES += \ 12 | main.cpp \ 13 | mainwindow.cpp \ 14 | wareprogressbar.cpp 15 | 16 | HEADERS += \ 17 | mainwindow.h \ 18 | wareprogressbar.h 19 | 20 | # Default rules for deployment. 21 | qnx: target.path = /tmp/$${TARGET}/bin 22 | else: unix:!android: target.path = /opt/$${TARGET}/bin 23 | !isEmpty(target.path): INSTALLS += target 24 | 25 | RESOURCES += \ 26 | src.qrc 27 | -------------------------------------------------------------------------------- /WaveProgressBar/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/WaveProgressBar/background.png -------------------------------------------------------------------------------- /WaveProgressBar/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | 9 | MainWindow w; 10 | w.show(); 11 | 12 | return a.exec(); 13 | } 14 | -------------------------------------------------------------------------------- /WaveProgressBar/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | MainWindow::MainWindow(QWidget *parent) 8 | : QLabel(parent) 9 | { 10 | QGridLayout *pLayout = new QGridLayout; 11 | QSlider *slider = new QSlider(this); 12 | 13 | WareProgressBar *pWareProgressBar1 = createWareProgressBar1(); 14 | WareProgressBar *pWareProgressBar2 = createWareProgressBar2(); 15 | WareProgressBar *pWareProgressBar3 = createWareProgressBar3(); 16 | WareProgressBar *pWareProgressBar4 = createWareProgressBar4(); 17 | 18 | slider->setOrientation(Qt::Horizontal); 19 | slider->setRange(0, 100); 20 | 21 | connect(slider, &QSlider::valueChanged, pWareProgressBar1, &WareProgressBar::setValue); 22 | connect(slider, &QSlider::valueChanged, pWareProgressBar2, &WareProgressBar::setValue); 23 | connect(slider, &QSlider::valueChanged, pWareProgressBar3, &WareProgressBar::setValue); 24 | connect(slider, &QSlider::valueChanged, pWareProgressBar4, &WareProgressBar::setValue); 25 | 26 | pLayout->addWidget(pWareProgressBar1, 0, 0); 27 | pLayout->addWidget(pWareProgressBar2, 0, 1); 28 | pLayout->addWidget(pWareProgressBar3, 1, 0); 29 | pLayout->addWidget(pWareProgressBar4, 1, 1); 30 | pLayout->addWidget(slider, 2, 0, 1, 2); 31 | 32 | setLayout(pLayout); 33 | 34 | setMinimumSize(500, 500); 35 | setPixmap(QPixmap(":/image/background.png")); 36 | setScaledContents(true); 37 | } 38 | 39 | WareProgressBar *MainWindow::createWareProgressBar1() 40 | { 41 | WareProgressBar *ret = new WareProgressBar(this); 42 | 43 | ret->setMinValue(0); // 设置最小值 44 | ret->setMaxValue(100); // 设置最大值 45 | 46 | return ret; 47 | } 48 | 49 | WareProgressBar *MainWindow::createWareProgressBar2() 50 | { 51 | WareProgressBar *ret = new WareProgressBar(this); 52 | 53 | ret->setWaveHeight(0); // 设置水波高度 54 | ret->setPieCircleColor(QColor(255,107,107)); // 设置饼行进度条圆颜色 55 | ret->setwaterColor(QColor(255,107,107)); // 设置水颜色 56 | ret->setPeiIsForwardOrientation(false); // 设置饼型进度条圆前进方向 57 | 58 | return ret; 59 | } 60 | 61 | WareProgressBar *MainWindow::createWareProgressBar3() 62 | { 63 | WareProgressBar *ret = new WareProgressBar(this); 64 | 65 | ret->setTextColor(Qt::yellow); // 设置文本颜色 66 | ret->setSuffixText("HZ"); // 设置后缀文本 67 | ret->setWaveDensity(2); // 设置水波密度 68 | ret->setPeiCircleIsvisible(false); // 设置饼型进度圆是否可见 69 | ret->setwaterColor(QColor(24,189,155)); // 设置水颜色 70 | 71 | return ret; 72 | } 73 | 74 | WareProgressBar *MainWindow::createWareProgressBar4() 75 | { 76 | WareProgressBar *ret = new WareProgressBar(this); 77 | 78 | ret->setPieStartAngle(90); // 设置饼型进度条圆起始角度 79 | ret->setWaterIsvisible(false); // 设置水是否可见 80 | ret->setSuffixText("V"); // 设置后缀文本 81 | 82 | return ret; 83 | } 84 | 85 | MainWindow::~MainWindow() 86 | { 87 | } 88 | 89 | -------------------------------------------------------------------------------- /WaveProgressBar/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include "wareprogressbar.h" 6 | 7 | class MainWindow : public QLabel 8 | { 9 | Q_OBJECT 10 | 11 | public: 12 | MainWindow(QWidget *parent = nullptr); 13 | ~MainWindow(); 14 | 15 | protected: 16 | WareProgressBar *createWareProgressBar1(); 17 | WareProgressBar *createWareProgressBar2(); 18 | WareProgressBar *createWareProgressBar3(); 19 | WareProgressBar *createWareProgressBar4(); 20 | private: 21 | 22 | }; 23 | #endif // MAINWINDOW_H 24 | -------------------------------------------------------------------------------- /WaveProgressBar/screenshot.cpp: -------------------------------------------------------------------------------- 1 | #include "wareprogressbar.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | WareProgressBar::WareProgressBar(QWidget *parent) : QWidget(parent) 11 | { 12 | setAttribute(Qt::WA_TranslucentBackground, true); 13 | 14 | connect(&m_timer, &QTimer::timeout, this, [&]() 15 | { 16 | if (m_waveForwardOrientation) 17 | { 18 | m_offset += 0.6; 19 | if (m_offset > m_radius) 20 | m_offset = -m_radius; 21 | } 22 | else 23 | { 24 | m_offset -= 0.6; 25 | if (m_offset < -m_radius) 26 | m_offset = m_radius; 27 | } 28 | update(); 29 | }); 30 | 31 | m_timer.start(80); 32 | } 33 | 34 | WareProgressBar::~WareProgressBar() 35 | { } 36 | 37 | void WareProgressBar::paintEvent(QPaintEvent *event) 38 | { 39 | QPainter painter(this); 40 | 41 | painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing, true); 42 | painter.translate(width()/2.0, height()/2.0); 43 | m_radius = qMin(width(), height()) / 2.0; 44 | 45 | drawBackground(&painter); 46 | if (m_peiCircleIsvisible) drawPieCircel(&painter); 47 | if (m_waterIsvisible) drawWater(&painter); 48 | drawText(&painter); 49 | 50 | return QWidget::paintEvent(event); 51 | } 52 | 53 | void WareProgressBar::drawPieCircel(QPainter *painter) 54 | { 55 | double pen_width = m_radius * 0.1; 56 | double radius = m_radius - pen_width; 57 | double angle = 360.0 * (m_value - m_minValue) / (m_maxValue - m_minValue); 58 | 59 | painter->save(); 60 | 61 | QPen pen; 62 | pen.setWidthF(pen_width); 63 | pen.setColor(m_peiCircleColor); 64 | pen.setCapStyle(Qt::SquareCap); 65 | painter->setPen(pen); 66 | painter->drawArc(-radius, -radius, radius*2, radius*2, m_pieStartAngle * 16, m_peiForwardOrientation ? -angle *16 : angle *16); 67 | 68 | painter->restore(); 69 | } 70 | 71 | void WareProgressBar::drawBackground(QPainter *painter) 72 | { 73 | double radius = m_radius * 0.81; 74 | 75 | painter->save(); 76 | 77 | painter->setPen(Qt::NoPen); 78 | painter->setBrush(QColor(50, 50, 50, 100)); 79 | painter->drawEllipse(QPointF(0, 0), radius, radius); 80 | 81 | painter->restore(); 82 | } 83 | 84 | void WareProgressBar::drawWater(QPainter *painter) 85 | { 86 | double radius = m_radius * 0.8; 87 | double percent = 1.0 * (m_value - m_minValue) / (m_maxValue - m_minValue); 88 | 89 | double w = m_waveDensity * M_PI / radius; 90 | double A = radius * m_waveHeight / 40; 91 | double k = (0.5 - percent) * radius * 2; 92 | 93 | painter->save(); 94 | 95 | QPainterPath wavePath1; 96 | QPainterPath wavePath2; 97 | wavePath1.moveTo(-radius, radius); 98 | wavePath2.moveTo(-radius, radius); 99 | 100 | for (double x=-radius; x<=radius; ++x) 101 | { 102 | double waterY1 = 0.0; 103 | double waterY2 = 0.0; 104 | 105 | if (m_value == m_minValue) 106 | { 107 | waterY2 = waterY1 = radius; 108 | } 109 | else if (m_value == m_maxValue) 110 | { 111 | waterY2 = waterY1 = -radius; 112 | } 113 | else 114 | { 115 | waterY1 = (double)(A * qSin(w * x + m_offset)) + k; 116 | waterY2 = (double)(A * qSin(w * x + m_offset + (radius / 2 * w))) + k; 117 | } 118 | 119 | wavePath1.lineTo(QPointF(x, waterY1)); 120 | wavePath2.lineTo(QPointF(x, waterY2)); 121 | } 122 | 123 | wavePath1.lineTo(radius, radius); 124 | wavePath2.lineTo(radius, radius); 125 | 126 | QPainterPath bigPath; 127 | bigPath.addEllipse(QPointF(0, 0), radius, radius); 128 | 129 | QColor watercolor1 = m_waterColor; 130 | QColor watercolor2 = m_waterColor; 131 | 132 | painter->setPen(Qt::NoPen); 133 | 134 | QPainterPath intersectedPath = bigPath.intersected(wavePath1); 135 | watercolor1.setAlpha(100); 136 | painter->setBrush(watercolor1); 137 | painter->drawPath(intersectedPath); 138 | 139 | intersectedPath = bigPath.intersected(wavePath2); 140 | watercolor2.setAlpha(180); 141 | painter->setBrush(watercolor2); 142 | painter->drawPath(intersectedPath); 143 | 144 | painter->restore(); 145 | } 146 | 147 | void WareProgressBar::drawText(QPainter *painter) 148 | { 149 | painter->save(); 150 | 151 | painter->setFont(QFont("Consolas", m_radius * 0.25, QFont::Bold)); 152 | painter->setPen(m_textColor); 153 | int temp = 1.0 * m_value / (m_maxValue - m_minValue) * 100; 154 | painter->drawText(QRect(-m_radius, -m_radius, m_radius*2, m_radius*2), Qt::AlignCenter, QString("%1%2").arg(temp).arg(m_suffixText)); 155 | 156 | painter->restore(); 157 | } 158 | 159 | void WareProgressBar::setPieCircleColor(const QColor &color) 160 | { 161 | m_peiCircleColor = color; 162 | update(); 163 | } 164 | 165 | void WareProgressBar::setwaterColor(const QColor &color) 166 | { 167 | m_waterColor = color; 168 | update(); 169 | } 170 | 171 | void WareProgressBar::setTextColor(const QColor &color) 172 | { 173 | m_textColor = color; 174 | update(); 175 | } 176 | 177 | void WareProgressBar::setPieStartAngle(int angle) 178 | { 179 | m_pieStartAngle = angle; 180 | update(); 181 | } 182 | 183 | void WareProgressBar::setValue(int value) 184 | { 185 | if (value < m_minValue) 186 | value = m_minValue; 187 | else if (value > m_maxValue) 188 | value = m_maxValue; 189 | 190 | m_value = value; 191 | update(); 192 | } 193 | 194 | void WareProgressBar::setRange(int minValue, int maxValue) 195 | { 196 | m_minValue = minValue; 197 | m_maxValue = maxValue; 198 | update(); 199 | } 200 | 201 | void WareProgressBar::setMaxValue(int value) 202 | { 203 | m_maxValue = value; 204 | update(); 205 | } 206 | 207 | void WareProgressBar::setMinValue(int value) 208 | { 209 | m_minValue = value; 210 | update(); 211 | } 212 | 213 | void WareProgressBar::setWaveDensity(int density) 214 | { 215 | m_waveDensity = density; 216 | update(); 217 | } 218 | 219 | void WareProgressBar::setWaveHeight(int height) 220 | { 221 | m_waveHeight = height; 222 | update(); 223 | } 224 | 225 | void WareProgressBar::setSuffixText(const QString &text) 226 | { 227 | m_suffixText = text; 228 | update(); 229 | } 230 | 231 | void WareProgressBar::setWaveIsForwardOrientation(bool Orientation) 232 | { 233 | m_waveForwardOrientation = Orientation; 234 | update(); 235 | } 236 | 237 | void WareProgressBar::setWaterIsvisible(bool visible) 238 | { 239 | m_waterIsvisible = visible; 240 | update(); 241 | } 242 | 243 | void WareProgressBar::setPeiCircleIsvisible(bool visible) 244 | { 245 | m_peiCircleIsvisible = visible; 246 | update(); 247 | } 248 | 249 | void WareProgressBar::setPeiIsForwardOrientation(bool Orientation) 250 | { 251 | m_peiForwardOrientation = Orientation; 252 | update(); 253 | } 254 | 255 | QColor WareProgressBar::getPieCircleColor() const 256 | { 257 | return m_peiCircleColor; 258 | } 259 | 260 | QColor WareProgressBar::getwaterColor() const 261 | { 262 | return m_waterColor; 263 | } 264 | 265 | QColor WareProgressBar::getTextColor() const 266 | { 267 | return m_textColor; 268 | } 269 | 270 | int WareProgressBar::getPieStartAngle() const 271 | { 272 | return m_pieStartAngle; 273 | } 274 | 275 | int WareProgressBar::getValue() const 276 | { 277 | return m_value; 278 | } 279 | 280 | int WareProgressBar::getMaxValue() const 281 | { 282 | return m_maxValue; 283 | } 284 | 285 | int WareProgressBar::getMinValue() const 286 | { 287 | return m_minValue; 288 | } 289 | 290 | int WareProgressBar::getWaveDensity() const 291 | { 292 | return m_waveDensity; 293 | } 294 | 295 | int WareProgressBar::getWaveHeight() const 296 | { 297 | return m_waveHeight; 298 | } 299 | 300 | QString WareProgressBar::getSuffixText() const 301 | { 302 | return m_suffixText; 303 | } 304 | 305 | bool WareProgressBar::getWaveIsForwardOrientation() const 306 | { 307 | return m_waveForwardOrientation; 308 | } 309 | 310 | bool WareProgressBar::getWaterIsvisible() const 311 | { 312 | return m_waterIsvisible; 313 | } 314 | 315 | bool WareProgressBar::getPeiCircleIsvisible() const 316 | { 317 | return m_peiCircleIsvisible; 318 | } 319 | 320 | bool WareProgressBar::getPeiIsForwardOrientation() const 321 | { 322 | return m_peiForwardOrientation; 323 | } 324 | -------------------------------------------------------------------------------- /WaveProgressBar/src.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | background.png 4 | 5 | 6 | -------------------------------------------------------------------------------- /WaveProgressBar/wareprogressbar.cpp: -------------------------------------------------------------------------------- 1 | #include "wareprogressbar.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | WareProgressBar::WareProgressBar(QWidget *parent) : QWidget(parent) 11 | { 12 | setAttribute(Qt::WA_TranslucentBackground, true); 13 | 14 | connect(&m_timer, &QTimer::timeout, this, [&]() 15 | { 16 | if (m_waveForwardOrientation) 17 | { 18 | m_offset += 0.6; 19 | if (m_offset > m_radius) 20 | m_offset = -m_radius; 21 | } 22 | else 23 | { 24 | m_offset -= 0.6; 25 | if (m_offset < -m_radius) 26 | m_offset = m_radius; 27 | } 28 | update(); 29 | }); 30 | 31 | m_timer.start(80); 32 | } 33 | 34 | WareProgressBar::~WareProgressBar() 35 | { } 36 | 37 | void WareProgressBar::paintEvent(QPaintEvent *event) 38 | { 39 | QPainter painter(this); 40 | 41 | painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing, true); 42 | painter.translate(width()/2.0, height()/2.0); 43 | m_radius = qMin(width(), height()) / 2.0; 44 | 45 | drawBackground(&painter); 46 | if (m_peiCircleIsvisible) drawPieCircel(&painter); 47 | if (m_waterIsvisible) drawWater(&painter); 48 | drawText(&painter); 49 | 50 | return QWidget::paintEvent(event); 51 | } 52 | 53 | void WareProgressBar::drawPieCircel(QPainter *painter) 54 | { 55 | double pen_width = m_radius * 0.1; 56 | double radius = m_radius - pen_width; 57 | double angle = 360.0 * (m_value - m_minValue) / (m_maxValue - m_minValue); 58 | 59 | painter->save(); 60 | 61 | QPen pen; 62 | pen.setWidthF(pen_width); 63 | pen.setColor(m_peiCircleColor); 64 | pen.setCapStyle(Qt::SquareCap); 65 | painter->setPen(pen); 66 | painter->drawArc(-radius, -radius, radius*2, radius*2, m_pieStartAngle * 16, m_peiForwardOrientation ? -angle *16 : angle *16); 67 | 68 | painter->restore(); 69 | } 70 | 71 | void WareProgressBar::drawBackground(QPainter *painter) 72 | { 73 | double radius = m_radius * 0.81; 74 | 75 | painter->save(); 76 | 77 | painter->setPen(Qt::NoPen); 78 | painter->setBrush(QColor(50, 50, 50, 100)); 79 | painter->drawEllipse(QPointF(0, 0), radius, radius); 80 | 81 | painter->restore(); 82 | } 83 | 84 | void WareProgressBar::drawWater(QPainter *painter) 85 | { 86 | double radius = m_radius * 0.8; 87 | double percent = 1.0 * (m_value - m_minValue) / (m_maxValue - m_minValue); 88 | 89 | double w = m_waveDensity * M_PI / radius; 90 | double A = radius * m_waveHeight / 40; 91 | double k = (0.5 - percent) * radius * 2; 92 | 93 | painter->save(); 94 | 95 | QPainterPath wavePath1; 96 | QPainterPath wavePath2; 97 | wavePath1.moveTo(-radius, radius); 98 | wavePath2.moveTo(-radius, radius); 99 | 100 | for (double x=-radius; x<=radius; ++x) 101 | { 102 | double waterY1 = 0.0; 103 | double waterY2 = 0.0; 104 | 105 | if (m_value == m_minValue) 106 | { 107 | waterY2 = waterY1 = radius; 108 | } 109 | else if (m_value == m_maxValue) 110 | { 111 | waterY2 = waterY1 = -radius; 112 | } 113 | else 114 | { 115 | waterY1 = (double)(A * qSin(w * x + m_offset)) + k; 116 | waterY2 = (double)(A * qSin(w * x + m_offset + (radius / 2 * w))) + k; 117 | } 118 | 119 | wavePath1.lineTo(QPointF(x, waterY1)); 120 | wavePath2.lineTo(QPointF(x, waterY2)); 121 | } 122 | 123 | wavePath1.lineTo(radius, radius); 124 | wavePath2.lineTo(radius, radius); 125 | 126 | QPainterPath bigPath; 127 | bigPath.addEllipse(QPointF(0, 0), radius, radius); 128 | 129 | QColor watercolor1 = m_waterColor; 130 | QColor watercolor2 = m_waterColor; 131 | 132 | painter->setPen(Qt::NoPen); 133 | 134 | QPainterPath intersectedPath = bigPath.intersected(wavePath1); 135 | watercolor1.setAlpha(100); 136 | painter->setBrush(watercolor1); 137 | painter->drawPath(intersectedPath); 138 | 139 | intersectedPath = bigPath.intersected(wavePath2); 140 | watercolor2.setAlpha(180); 141 | painter->setBrush(watercolor2); 142 | painter->drawPath(intersectedPath); 143 | 144 | painter->restore(); 145 | } 146 | 147 | void WareProgressBar::drawText(QPainter *painter) 148 | { 149 | painter->save(); 150 | 151 | painter->setFont(QFont("Consolas", m_radius * 0.25, QFont::Bold)); 152 | painter->setPen(m_textColor); 153 | int temp = 1.0 * m_value / (m_maxValue - m_minValue) * 100; 154 | painter->drawText(QRect(-m_radius, -m_radius, m_radius*2, m_radius*2), Qt::AlignCenter, QString("%1%2").arg(temp).arg(m_suffixText)); 155 | 156 | painter->restore(); 157 | } 158 | 159 | void WareProgressBar::setPieCircleColor(const QColor &color) 160 | { 161 | m_peiCircleColor = color; 162 | update(); 163 | } 164 | 165 | void WareProgressBar::setwaterColor(const QColor &color) 166 | { 167 | m_waterColor = color; 168 | update(); 169 | } 170 | 171 | void WareProgressBar::setTextColor(const QColor &color) 172 | { 173 | m_textColor = color; 174 | update(); 175 | } 176 | 177 | void WareProgressBar::setPieStartAngle(int angle) 178 | { 179 | m_pieStartAngle = angle; 180 | update(); 181 | } 182 | 183 | void WareProgressBar::setValue(int value) 184 | { 185 | if (value < m_minValue) 186 | value = m_minValue; 187 | else if (value > m_maxValue) 188 | value = m_maxValue; 189 | 190 | m_value = value; 191 | update(); 192 | } 193 | 194 | void WareProgressBar::setRange(int minValue, int maxValue) 195 | { 196 | m_minValue = minValue; 197 | m_maxValue = maxValue; 198 | update(); 199 | } 200 | 201 | void WareProgressBar::setMaxValue(int value) 202 | { 203 | m_maxValue = value; 204 | update(); 205 | } 206 | 207 | void WareProgressBar::setMinValue(int value) 208 | { 209 | m_minValue = value; 210 | update(); 211 | } 212 | 213 | void WareProgressBar::setWaveDensity(int density) 214 | { 215 | m_waveDensity = density; 216 | update(); 217 | } 218 | 219 | void WareProgressBar::setWaveHeight(int height) 220 | { 221 | m_waveHeight = height; 222 | update(); 223 | } 224 | 225 | void WareProgressBar::setSuffixText(const QString &text) 226 | { 227 | m_suffixText = text; 228 | update(); 229 | } 230 | 231 | void WareProgressBar::setWaveIsForwardOrientation(bool Orientation) 232 | { 233 | m_waveForwardOrientation = Orientation; 234 | update(); 235 | } 236 | 237 | void WareProgressBar::setWaterIsvisible(bool visible) 238 | { 239 | m_waterIsvisible = visible; 240 | update(); 241 | } 242 | 243 | void WareProgressBar::setPeiCircleIsvisible(bool visible) 244 | { 245 | m_peiCircleIsvisible = visible; 246 | update(); 247 | } 248 | 249 | void WareProgressBar::setPeiIsForwardOrientation(bool Orientation) 250 | { 251 | m_peiForwardOrientation = Orientation; 252 | update(); 253 | } 254 | 255 | QColor WareProgressBar::getPieCircleColor() const 256 | { 257 | return m_peiCircleColor; 258 | } 259 | 260 | QColor WareProgressBar::getwaterColor() const 261 | { 262 | return m_waterColor; 263 | } 264 | 265 | QColor WareProgressBar::getTextColor() const 266 | { 267 | return m_textColor; 268 | } 269 | 270 | int WareProgressBar::getPieStartAngle() const 271 | { 272 | return m_pieStartAngle; 273 | } 274 | 275 | int WareProgressBar::getValue() const 276 | { 277 | return m_value; 278 | } 279 | 280 | int WareProgressBar::getMaxValue() const 281 | { 282 | return m_maxValue; 283 | } 284 | 285 | int WareProgressBar::getMinValue() const 286 | { 287 | return m_minValue; 288 | } 289 | 290 | int WareProgressBar::getWaveDensity() const 291 | { 292 | return m_waveDensity; 293 | } 294 | 295 | int WareProgressBar::getWaveHeight() const 296 | { 297 | return m_waveHeight; 298 | } 299 | 300 | QString WareProgressBar::getSuffixText() const 301 | { 302 | return m_suffixText; 303 | } 304 | 305 | bool WareProgressBar::getWaveIsForwardOrientation() const 306 | { 307 | return m_waveForwardOrientation; 308 | } 309 | 310 | bool WareProgressBar::getWaterIsvisible() const 311 | { 312 | return m_waterIsvisible; 313 | } 314 | 315 | bool WareProgressBar::getPeiCircleIsvisible() const 316 | { 317 | return m_peiCircleIsvisible; 318 | } 319 | 320 | bool WareProgressBar::getPeiIsForwardOrientation() const 321 | { 322 | return m_peiForwardOrientation; 323 | } 324 | -------------------------------------------------------------------------------- /WaveProgressBar/wareprogressbar.h: -------------------------------------------------------------------------------- 1 | #ifndef WAREPROGRESSBAR_H 2 | #define WAREPROGRESSBAR_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /* 多彩仪表盘自定义控件 实现的功能 10 | * 1. 可设置范围值 11 | * 2. 可设置起始角度 12 | * 3. 可设置水波密度,密度越大浪越大 13 | * 4. 可设置水波高度,高度越大浪越大 14 | * 5. 可设置是否显示进度条 15 | * 6. 可设置是否显示水纹 16 | * 7. 可设置进度色、水纹色、文字色 17 | * 8. 可设置进度条前进方向 18 | */ 19 | 20 | class WareProgressBar : public QWidget 21 | { 22 | Q_OBJECT 23 | 24 | Q_PROPERTY(QColor outerCircleColor READ getPieCircleColor WRITE setPieCircleColor) 25 | Q_PROPERTY(QColor waterColor READ getwaterColor WRITE setwaterColor) 26 | Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor) 27 | Q_PROPERTY(int pieStartAngle READ getPieStartAngle WRITE setPieStartAngle) 28 | Q_PROPERTY(int value READ getValue WRITE setValue) 29 | Q_PROPERTY(int maxValue READ getMaxValue WRITE setMaxValue) 30 | Q_PROPERTY(int minValue READ getMinValue WRITE setMinValue) 31 | Q_PROPERTY(int waveDensity READ getWaveDensity WRITE setWaveDensity) 32 | Q_PROPERTY(int waveHeight READ getWaveHeight WRITE setWaveHeight) 33 | Q_PROPERTY(QString suffixText READ getSuffixText WRITE setSuffixText) 34 | Q_PROPERTY(bool waveForwardOrientation READ getWaveIsForwardOrientation WRITE setWaveIsForwardOrientation) 35 | Q_PROPERTY(bool waterIsvisible READ getWaterIsvisible WRITE setWaterIsvisible) 36 | Q_PROPERTY(bool peiCircleIsvisible READ getPeiCircleIsvisible WRITE setPeiCircleIsvisible) 37 | Q_PROPERTY(bool peiForwardOrientation READ getPeiIsForwardOrientation WRITE setPeiIsForwardOrientation) 38 | 39 | public: 40 | explicit WareProgressBar(QWidget *parent = nullptr); 41 | ~WareProgressBar(); 42 | 43 | QColor getPieCircleColor() const; 44 | QColor getwaterColor() const; 45 | QColor getTextColor() const; 46 | int getPieStartAngle() const; 47 | int getValue() const; 48 | int getMaxValue() const; 49 | int getMinValue() const; 50 | int getWaveDensity() const; 51 | int getWaveHeight() const; 52 | QString getSuffixText() const; 53 | bool getWaveIsForwardOrientation() const; 54 | bool getWaterIsvisible() const; 55 | bool getPeiCircleIsvisible() const; 56 | bool getPeiIsForwardOrientation() const; 57 | 58 | public slots: 59 | 60 | void setPieCircleColor(const QColor &color); // 设置外圆颜色 61 | void setwaterColor(const QColor &color); // 设置水颜色 62 | void setTextColor(const QColor &color); // 设置文本颜色 63 | void setPieStartAngle(int angle); // 设置饼图起始角度 64 | void setValue(int value); // 设置开始值 65 | void setRange(int minValue, int maxValue); // 设置范围值 66 | void setMaxValue(int value); // 设置最大值 67 | void setMinValue(int value); // 设置最小值 68 | void setWaveDensity(int density); // 设置水波密度 [0 - 10] 69 | void setWaveHeight(int height); // 设置水波高度 [0 - 10] 70 | void setSuffixText(const QString &text); // 设置提示文本 71 | void setWaveIsForwardOrientation(bool Orientation); // 设置水波方向 [true 正向; false 反向] 72 | void setWaterIsvisible(bool visible); // 设置水是否可见 73 | void setPeiCircleIsvisible(bool visible); // 设置圆饼否可见 74 | void setPeiIsForwardOrientation(bool Orientation); // 设置圆饼前进方向 75 | 76 | protected: 77 | void paintEvent(QPaintEvent *event) override; 78 | void drawPieCircel(QPainter *painter); 79 | void drawBackground(QPainter *painter); 80 | void drawWater(QPainter *painter); 81 | void drawText(QPainter *painter); 82 | 83 | private: 84 | double m_radius = 0.0; 85 | 86 | QColor m_peiCircleColor = QColor(100,184,255); 87 | QColor m_waterColor = QColor(100,184,255); 88 | QColor m_textColor = QColor(250,250,250); 89 | int m_pieStartAngle = 0; 90 | int m_value = 0; 91 | int m_maxValue = 100; 92 | int m_minValue = 0; 93 | int m_waveDensity = 1; 94 | int m_waveHeight = 5; 95 | bool m_waveForwardOrientation = true; 96 | QString m_suffixText = "%"; 97 | bool m_waterIsvisible = true; 98 | bool m_peiCircleIsvisible = true; 99 | bool m_peiForwardOrientation = true; 100 | 101 | double m_offset = 50; 102 | QTimer m_timer; 103 | }; 104 | 105 | #endif // WAREPROGRESSBAR_H 106 | -------------------------------------------------------------------------------- /bannerLabel/banner-image/0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/bannerLabel/banner-image/0.jpg -------------------------------------------------------------------------------- /bannerLabel/banner-image/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/bannerLabel/banner-image/1.jpg -------------------------------------------------------------------------------- /bannerLabel/banner-image/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/bannerLabel/banner-image/2.jpg -------------------------------------------------------------------------------- /bannerLabel/banner-image/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/bannerLabel/banner-image/3.jpg -------------------------------------------------------------------------------- /bannerLabel/banner-image/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/bannerLabel/banner-image/4.jpg -------------------------------------------------------------------------------- /bannerLabel/banner-image/5.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/bannerLabel/banner-image/5.jpeg -------------------------------------------------------------------------------- /bannerLabel/banner-image/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/bannerLabel/banner-image/5.jpg -------------------------------------------------------------------------------- /bannerLabel/bannerLabel.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # The following define makes your compiler emit warnings if you use 8 | # any Qt feature that has been marked deprecated (the exact warnings 9 | # depend on your compiler). Please consult the documentation of the 10 | # deprecated API in order to know how to port your code away from it. 11 | DEFINES += QT_DEPRECATED_WARNINGS 12 | 13 | # You can also make your code fail to compile if it uses deprecated APIs. 14 | # In order to do so, uncomment the following line. 15 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 | 18 | SOURCES += \ 19 | bannerlabel.cpp \ 20 | main.cpp \ 21 | mainwindow.cpp 22 | 23 | HEADERS += \ 24 | bannerlabel.h \ 25 | mainwindow.h 26 | 27 | # Default rules for deployment. 28 | qnx: target.path = /tmp/$${TARGET}/bin 29 | else: unix:!android: target.path = /opt/$${TARGET}/bin 30 | !isEmpty(target.path): INSTALLS += target 31 | 32 | RESOURCES += \ 33 | resource.qrc 34 | -------------------------------------------------------------------------------- /bannerLabel/bannerLabel_shotcut.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/bannerLabel/bannerLabel_shotcut.gif -------------------------------------------------------------------------------- /bannerLabel/bannerlabel.cpp: -------------------------------------------------------------------------------- 1 | #include "bannerlabel.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | BannerLabel::BannerLabel(QWidget *parent) : QWidget(parent) 12 | { 13 | initUI(); 14 | initControl(); 15 | } 16 | 17 | BannerLabel::~BannerLabel() 18 | { } 19 | 20 | void BannerLabel::initUI() 21 | { 22 | // 放置指示器的窗体载体 23 | m_pIndicatorWidget = new QWidget(this); 24 | 25 | // 给指示器加上左右布局 26 | m_pIndicatorLayout = new QHBoxLayout(m_pIndicatorWidget); 27 | m_pIndicatorLayout->setSpacing(BtnShrinkWidth); 28 | 29 | // 主布局、上下布局 30 | m_pIndicatorLayout->setAlignment(Qt::AlignLeft); 31 | } 32 | 33 | void BannerLabel::initControl() 34 | { 35 | initAnimation(); 36 | 37 | connect(m_pTimer, &QTimer::timeout, this, &BannerLabel::onImageTimerOut); 38 | connect(m_pBtnGroup, static_cast(&QButtonGroup::idClicked), this, &BannerLabel::onButtonClicked); 39 | 40 | m_pTimer->setInterval(interval); 41 | m_pTimer->start(); 42 | } 43 | 44 | void BannerLabel::initAnimation() 45 | { 46 | // 定制主动画组 47 | m_pAnimationGroup = new QParallelAnimationGroup(this); 48 | 49 | // 定义动画切换图片 50 | m_pImageAnimation = new QPropertyAnimation(m_pAnimationGroup, ""); 51 | m_pImageAnimation->setEasingCurve(QEasingCurve::OutCirc); 52 | m_pImageAnimation->setDuration(800); 53 | connect(m_pImageAnimation, &QPropertyAnimation::valueChanged, this, &BannerLabel::onImageValueChange); 54 | 55 | // 按钮串行动画组 56 | QSequentialAnimationGroup *sequentialGroup = new QSequentialAnimationGroup(m_pAnimationGroup); 57 | 58 | m_pExtendBtnAnimation = new QPropertyAnimation(sequentialGroup, ""); 59 | m_pExtendBtnAnimation->setEasingCurve(QEasingCurve::OutCirc); 60 | m_pExtendBtnAnimation->setDuration(400); 61 | connect(m_pExtendBtnAnimation, &QPropertyAnimation::valueChanged, this, &BannerLabel::onExtendBtnValueChange); 62 | 63 | m_pShrinkBtnAnimation = new QPropertyAnimation(sequentialGroup, ""); 64 | m_pShrinkBtnAnimation->setEasingCurve(QEasingCurve::OutCirc); 65 | m_pShrinkBtnAnimation->setDuration(400); 66 | connect(m_pShrinkBtnAnimation, &QPropertyAnimation::valueChanged, this, &BannerLabel::onShrinkBtnValueChange); 67 | 68 | sequentialGroup->addAnimation(m_pExtendBtnAnimation); 69 | sequentialGroup->addAnimation(m_pShrinkBtnAnimation); 70 | 71 | // 图片切换与按钮切换并行运行 72 | m_pAnimationGroup->addAnimation(m_pImageAnimation); 73 | m_pAnimationGroup->addAnimation(sequentialGroup); 74 | } 75 | 76 | void BannerLabel::addImage(const QList> &imagePairList) 77 | { 78 | QString style = QStringLiteral("QToolButton {border-image:url(:/image/point.png); border-width:%1px;}").arg(BtnShrinkWidth/2-1); // 再看看!!! 79 | 80 | for (int index=0; indexsetCursor(Qt::PointingHandCursor); 84 | btn->setFixedSize(BtnShrinkWidth, BtnShrinkWidth); 85 | btn->setStyleSheet(style); 86 | m_pBtnGroup->addButton(btn, index+m_imagePairList.count()); 87 | m_pIndicatorLayout->addWidget(btn); 88 | } 89 | 90 | m_imagePairList.append(imagePairList); 91 | m_pBtnGroup->button(m_currIndex)->setFixedWidth(BtnExtendWidth); 92 | update(); 93 | } 94 | 95 | const QList> &BannerLabel::getImages() const 96 | { 97 | return m_imagePairList; 98 | } 99 | 100 | void BannerLabel::paintEvent(QPaintEvent *event) 101 | { 102 | Q_UNUSED(event) 103 | 104 | if (m_imagePairList.count() == 0) 105 | return; 106 | 107 | QPainter painter(this); 108 | painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing); 109 | QTextOption textOption(Qt::AlignLeft | Qt::AlignVCenter); 110 | painter.setPen(textColor); 111 | 112 | QFont font; 113 | font.setFamily("微软雅黑"); 114 | font.setPixelSize(20); 115 | font.setBold(true); 116 | painter.setFont(font); 117 | 118 | // 取出上张图片 + 当前图片,并平滑缩放 119 | int width = this->width(); 120 | int height = this->height(); 121 | QPixmap previousPix(m_imagePairList.at(m_preIndex).first); 122 | QPixmap currentPix(m_imagePairList.at(m_currIndex).first); 123 | previousPix = previousPix.scaled(width, height, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); 124 | currentPix = currentPix.scaled(width, height, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); 125 | int widthOffset = m_preIndex < m_currIndex ? width + m_offset : m_offset - width; 126 | 127 | painter.drawPixmap(m_offset, 0, previousPix); 128 | painter.drawPixmap(widthOffset, 0, currentPix); 129 | 130 | painter.drawText(QRect(m_offset+BtnShrinkWidth, height-(BtnShrinkWidth*5), width-BtnShrinkWidth, BtnShrinkWidth*2), m_imagePairList.at(m_currIndex).second, textOption); 131 | painter.drawText(QRect(widthOffset+BtnShrinkWidth, height-(BtnShrinkWidth*5), width-BtnShrinkWidth, BtnShrinkWidth*2), m_imagePairList.at(m_currIndex).second, textOption); 132 | } 133 | 134 | void BannerLabel::resizeEvent(QResizeEvent *event) 135 | { 136 | m_pIndicatorWidget->setGeometry(QRect(BtnShrinkWidth, height()-(BtnShrinkWidth*4), width()-BtnShrinkWidth, BtnShrinkWidth*5)); 137 | 138 | QWidget::resizeEvent(event); 139 | } 140 | 141 | void BannerLabel::enterEvent(QEvent *event) 142 | { 143 | if (hoverStop) 144 | m_pTimer->stop(); 145 | 146 | QWidget::enterEvent(event); 147 | } 148 | 149 | void BannerLabel::leaveEvent(QEvent *event) 150 | { 151 | if (hoverStop) 152 | m_pTimer->start(); 153 | 154 | QWidget::leaveEvent(event); 155 | } 156 | 157 | void BannerLabel::mousePressEvent(QMouseEvent *event) 158 | { 159 | if ((event->button() == Qt::LeftButton) && (!m_pIndicatorWidget->geometry().contains(event->pos()))) 160 | { 161 | emit imageclicked(m_currIndex, m_imagePairList.at(m_currIndex).first, m_imagePairList.at(m_currIndex).second); 162 | } 163 | 164 | QWidget::mousePressEvent(event); 165 | } 166 | 167 | void BannerLabel::setFixedSize(const QSize &s) 168 | { 169 | QWidget::setFixedSize(s); 170 | } 171 | 172 | void BannerLabel::setFixedSize(int w, int h) 173 | { 174 | QWidget::setFixedSize(w, h); 175 | } 176 | 177 | int BannerLabel::getInterval() const 178 | { 179 | return interval; 180 | } 181 | 182 | void BannerLabel::setInterval(int _interval) 183 | { 184 | interval = _interval; 185 | 186 | m_pTimer->setInterval(_interval); 187 | } 188 | 189 | void BannerLabel::setHoverStop(bool isHoverStop) 190 | { 191 | hoverStop = isHoverStop; 192 | } 193 | 194 | bool BannerLabel::getHoverStop() const 195 | { 196 | return hoverStop; 197 | } 198 | 199 | void BannerLabel::setTextColor(const QColor &_textColor) 200 | { 201 | textColor = _textColor; 202 | } 203 | 204 | QColor BannerLabel::getTextColor() const 205 | { 206 | return textColor; 207 | } 208 | 209 | void BannerLabel::setIndicatorPosition(IndicatorPosition _indicatorPosition) 210 | { 211 | indicatorPosition = _indicatorPosition; 212 | 213 | m_pIndicatorLayout->setAlignment(static_cast(_indicatorPosition)); 214 | } 215 | 216 | BannerLabel::IndicatorPosition BannerLabel::getIndicatorPosition() const 217 | { 218 | return indicatorPosition; 219 | } 220 | 221 | void BannerLabel::onImageValueChange(const QVariant &variant) 222 | { 223 | m_offset = variant.toInt(); 224 | update(); 225 | } 226 | 227 | void BannerLabel::onExtendBtnValueChange(const QVariant &varient) 228 | { 229 | m_pBtnGroup->button(m_currIndex)->setFixedWidth(varient.toInt()); 230 | } 231 | 232 | void BannerLabel::onShrinkBtnValueChange(const QVariant &varient) 233 | { 234 | for (int index=0; indexbutton(index)->width() > BtnShrinkWidth)) 236 | m_pBtnGroup->button(index)->setFixedWidth(varient.toInt()); 237 | } 238 | 239 | void BannerLabel::onButtonClicked(int index) 240 | { 241 | if (m_imagePairList.count() == 0) 242 | return; 243 | 244 | if (index < 0) 245 | index = m_imagePairList.size() - 1; 246 | 247 | if (index >= m_imagePairList.size()) 248 | index = 0; 249 | 250 | m_preIndex = m_currIndex; 251 | m_currIndex = index; 252 | 253 | m_pAnimationGroup->stop(); 254 | 255 | if (m_preIndex < m_currIndex) 256 | { 257 | m_pImageAnimation->setStartValue(0); 258 | m_pImageAnimation->setEndValue(0 - width()); 259 | } 260 | else 261 | { 262 | m_pImageAnimation->setStartValue(0); 263 | m_pImageAnimation->setEndValue(width()); 264 | } 265 | 266 | m_pShrinkBtnAnimation->setStartValue(BtnExtendWidth); 267 | m_pShrinkBtnAnimation->setEndValue(BtnShrinkWidth); 268 | 269 | m_pExtendBtnAnimation->setStartValue(BtnShrinkWidth); 270 | m_pExtendBtnAnimation->setEndValue(BtnExtendWidth); 271 | 272 | emit imageChange(m_currIndex, m_imagePairList.at(m_currIndex).first, m_imagePairList.at(m_currIndex).second); 273 | 274 | m_pAnimationGroup->start(); 275 | 276 | if (!hoverStop) 277 | m_pTimer->start(); 278 | } 279 | 280 | void BannerLabel::onImageTimerOut() 281 | { 282 | onButtonClicked(m_currIndex+1); 283 | } 284 | 285 | void BannerLabel::imageClear() 286 | { 287 | qDeleteAll(m_pBtnGroup->buttons()); 288 | m_imagePairList.clear(); 289 | m_preIndex = 0; 290 | m_currIndex = 0; 291 | } 292 | -------------------------------------------------------------------------------- /bannerLabel/bannerlabel.h: -------------------------------------------------------------------------------- 1 | #ifndef BANNERLABEL_H 2 | #define BANNERLABEL_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /** 16 | * 横幅轮播空间 17 | * 参考:https://zhuanlan.zhihu.com/p/64257336 18 | * https://www.cnblogs.com/feiyangqingyun/p/10990260.html 19 | * 1. 可随机添加图片路径及提示信息集合 20 | * 2. 可清空图片路径及提示信息集合 21 | * 3. 可设置图片切换时间 22 | * 4. 可设置提示信息文字颜色 23 | * 5. 可设置指示器位置 左边 + 中间 + 右边 24 | * 6. 可设置鼠标悬停停止播放 25 | * 7. 可设置组件固定大小(需要与图片尺寸匹配) 26 | * 8. 图片切换时,可发出切换信号 27 | * 9. 当前图片被点击时,可发出点击信号 28 | * 29 | * 使用时需注意图标资源文件路径 border-image:url(:/image/point.png); 30 | */ 31 | 32 | class BannerLabel : public QWidget 33 | { 34 | Q_OBJECT 35 | 36 | Q_ENUMS(IndicatorPosition) 37 | 38 | Q_PROPERTY(int interval READ getInterval WRITE setInterval); 39 | Q_PROPERTY(bool hoverStop READ getHoverStop WRITE setHoverStop); 40 | Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor); 41 | Q_PROPERTY(IndicatorPosition indicatorPosition READ getIndicatorPosition WRITE setIndicatorPosition); 42 | 43 | public: 44 | enum IndicatorPosition { 45 | IndicatorLeft = Qt::AlignLeft | Qt::AlignVCenter, 46 | IndicatorCenter = Qt::AlignHCenter | Qt::AlignVCenter, 47 | IndicatorRight = Qt::AlignRight | Qt::AlignVCenter 48 | }; 49 | 50 | public slots: 51 | /* 设置图片切换间隔 */ 52 | void setInterval(int _interval); 53 | /* 设置鼠标悬停停止播放 */ 54 | void setHoverStop(bool isHoverStop); 55 | /* 设置提示信息文字颜色 */ 56 | void setTextColor(const QColor &_textColor); 57 | /* 设置指示器位置 左边 + 中间 + 右边 */ 58 | void setIndicatorPosition(IndicatorPosition _indicatorPosition); 59 | /* 清空图片路径及提示信息集合 */ 60 | void imageClear(); 61 | 62 | public: 63 | explicit BannerLabel(QWidget *parent = nullptr); 64 | virtual ~BannerLabel(); 65 | 66 | /* 随机添加图片路径及提示信息集合 */ 67 | void addImage(const QList> &imagePairList); 68 | /* 获取图片路径及提示信息集合 */ 69 | const QList> &getImages() const; 70 | /* 可设置组件固定大小(需要与图片尺寸匹配) */ 71 | void setFixedSize(const QSize &s); 72 | void setFixedSize(int w, int h); 73 | /* 获取图片切换间隔 */ 74 | int getInterval() const; 75 | /* 鼠标悬停是否停止播放 */ 76 | bool getHoverStop() const; 77 | /* 获取提示信息文字颜色 */ 78 | QColor getTextColor() const; 79 | /* 获取指示器位置 左边 + 中间 + 右边 */ 80 | IndicatorPosition getIndicatorPosition() const; 81 | 82 | signals: 83 | /* 图片切换时,发出切换信号 */ 84 | void imageChange(int index, const QString &imagePath, const QString &tipText); 85 | /* 图片被点击时,发出点击信号 */ 86 | void imageclicked(int index, const QString &imagePath, const QString &tipText); 87 | 88 | protected: 89 | void paintEvent(QPaintEvent *event) override; 90 | void resizeEvent(QResizeEvent *event) override; 91 | void enterEvent(QEvent *event) override; 92 | void leaveEvent(QEvent *event) override; 93 | void mousePressEvent(QMouseEvent *event) override; 94 | 95 | private slots: 96 | void onImageValueChange(const QVariant &variant); 97 | void onExtendBtnValueChange(const QVariant &varient); 98 | void onShrinkBtnValueChange(const QVariant &varient); 99 | void onButtonClicked(int index); 100 | void onImageTimerOut(); 101 | 102 | private: 103 | void initUI(); 104 | void initControl(); 105 | void initAnimation(); 106 | 107 | private: 108 | QWidget *m_pIndicatorWidget = nullptr; 109 | QHBoxLayout *m_pIndicatorLayout = nullptr; 110 | 111 | QPropertyAnimation *m_pImageAnimation = nullptr; 112 | QPropertyAnimation *m_pShrinkBtnAnimation = nullptr; 113 | QPropertyAnimation *m_pExtendBtnAnimation = nullptr; 114 | QParallelAnimationGroup *m_pAnimationGroup = nullptr; 115 | 116 | QButtonGroup *m_pBtnGroup = new QButtonGroup(this); 117 | QList> m_imagePairList; 118 | QTimer *m_pTimer = new QTimer(this); 119 | 120 | const static int BtnShrinkWidth = 16; 121 | const static int BtnExtendWidth = 48; 122 | 123 | int m_currIndex = 0; 124 | int m_preIndex = 0; 125 | int m_offset = 0; 126 | 127 | int interval = 2000; 128 | bool hoverStop = false; 129 | QColor textColor = Qt::gray; 130 | IndicatorPosition indicatorPosition = IndicatorLeft; 131 | }; 132 | 133 | #endif // BANNERLABEL_H 134 | -------------------------------------------------------------------------------- /bannerLabel/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /bannerLabel/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include "bannerlabel.h" 4 | 5 | #include 6 | #include 7 | 8 | MainWindow::MainWindow(QWidget *parent) 9 | : QWidget(parent) 10 | { 11 | /* BannerLabel example begin */ 12 | BannerLabel *bannerLabel = new BannerLabel(this); 13 | 14 | bannerLabel->setTextColor(Qt::green); 15 | bannerLabel->setIndicatorPosition(BannerLabel::IndicatorLeft); 16 | bannerLabel->setInterval(1000); 17 | bannerLabel->setHoverStop(true); 18 | bannerLabel->setFixedSize(800, 400); 19 | 20 | const QList>&& imagepairlst = { std::make_pair("C:/Users/DELL/Desktop/bannerLabel/banner-image/0", "离离原上草!"), 21 | std::make_pair("C:/Users/DELL/Desktop/bannerLabel/banner-image/1", "一岁一枯荣!"), 22 | std::make_pair("C:/Users/DELL/Desktop/bannerLabel/banner-image/2", "野火烧不尽!"), 23 | std::make_pair("C:/Users/DELL/Desktop/bannerLabel/banner-image/3", "春风吹又生!"), 24 | std::make_pair("C:/Users/DELL/Desktop/bannerLabel/banner-image/5", "Banner 横幅组件!"), }; 25 | bannerLabel->addImage(imagepairlst); 26 | 27 | connect(bannerLabel, &BannerLabel::imageChange, [=](int index, const QString &imagePath, const QString &tipText){ 28 | qDebug() << "imageChange: " << index << " " << imagePath << " " << tipText; 29 | }); 30 | 31 | connect(bannerLabel, &BannerLabel::imageclicked, [=](int index, const QString &imagePath, const QString &tipText){ 32 | qDebug() << "imageclicked: " << index << " " << imagePath << " " << tipText; 33 | }); 34 | /* BannerLabel example end */ 35 | 36 | QHBoxLayout *pLayout = new QHBoxLayout; 37 | pLayout->addWidget(bannerLabel); 38 | pLayout->setMargin(0); 39 | setLayout(pLayout); 40 | } 41 | 42 | MainWindow::~MainWindow() 43 | { 44 | } 45 | 46 | -------------------------------------------------------------------------------- /bannerLabel/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | class MainWindow : public QWidget 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | MainWindow(QWidget *parent = nullptr); 12 | ~MainWindow(); 13 | }; 14 | #endif // MAINWINDOW_H 15 | -------------------------------------------------------------------------------- /bannerLabel/point.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/bannerLabel/point.png -------------------------------------------------------------------------------- /bannerLabel/resource.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | point.png 4 | 5 | 6 | -------------------------------------------------------------------------------- /commonhelper/commonhelper.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMONHELPER_H 2 | #define COMMONHELPER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /** 16 | * @brief 公共辅助类 17 | * 1. 设置皮肤样式 18 | * 2. 判断是否是IP地址 19 | * 3. 模糊延时 20 | * 4. 窗体剧中显示 21 | * 5. 设置为开机启动 22 | * 6. 设置为应用重启 23 | */ 24 | class CommonHelper : public QObject 25 | { 26 | public: 27 | CommonHelper() = delete; 28 | CommonHelper(const CommonHelper&) = delete; 29 | CommonHelper &operator=(const CommonHelper&) = delete; 30 | 31 | // 设置皮肤样式 32 | static void setStyleSheet(const QString &styleSheet) 33 | { 34 | QFile file(styleSheet); 35 | qApp->setStyleSheet(file.readAll()); 36 | file.close(); 37 | } 38 | 39 | // 判断是否是IP地址 40 | static bool isIP(const QString &ip) 41 | { 42 | QRegExp regExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"); 43 | return regExp.exactMatch(ip); 44 | } 45 | 46 | // 模糊延时 47 | static void sleep(unsigned long msec) 48 | { 49 | QTime deiTime = QTime::currentTime().addMSecs(msec); 50 | while (QTime::currentTime() < deiTime) 51 | { 52 | QCoreApplication::processEvents(QEventLoop::AllEvents, 100); 53 | } 54 | } 55 | 56 | // 窗体剧中显示 57 | static void formInCenter(QWidget &widget) 58 | { 59 | QDesktopWidget w; 60 | int deskWidth = w.width(); 61 | int deskHeight = w.height(); 62 | 63 | QPoint centerPoint((deskWidth - widget.width()) / 2, (deskHeight - widget.height()) / 2); 64 | widget.move(centerPoint); 65 | } 66 | 67 | // 设置为开机启动 68 | static void autoRunWithSystem(bool isAutoRun, const QString &appName, const QString &appPath) 69 | { 70 | QSettings reg(QStringLiteral("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), QSettings::NativeFormat); 71 | 72 | if (isAutoRun) 73 | reg.setValue(appName, appPath); 74 | else 75 | reg.setValue(appName, ""); 76 | } 77 | 78 | // 应用重启 79 | static void reboot() 80 | { 81 | QString program = QApplication::applicationFilePath(); 82 | QStringList argument = QApplication::arguments(); 83 | QString workDirectory = QDir::currentPath(); 84 | QProcess::startDetached(program, argument, workDirectory); 85 | QApplication::exit(); 86 | } 87 | 88 | // 文字转语音 89 | /* 90 | #include 91 | QT += texttospeech 92 | static bool textToSpeech(const QString &text, double volume=1.0, double rate=0.0, double pitch=0.0) 93 | { 94 | if (QTextToSpeech::availableEngines().isEmpty()) 95 | return false; 96 | 97 | static QTextToSpeech speech(qApp); 98 | 99 | if (speech.state() != QTextToSpeech::Ready) 100 | return false; 101 | 102 | speech.setVolume(volume); 103 | speech.setRate(rate); 104 | speech.setPitch(pitch); 105 | speech.say(text); 106 | 107 | return true; 108 | } 109 | */ 110 | }; 111 | 112 | #endif // COMMONHELPER_H 113 | -------------------------------------------------------------------------------- /commonhelper/commonhelper.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # The following define makes your compiler emit warnings if you use 8 | # any Qt feature that has been marked deprecated (the exact warnings 9 | # depend on your compiler). Please consult the documentation of the 10 | # deprecated API in order to know how to port your code away from it. 11 | DEFINES += QT_DEPRECATED_WARNINGS 12 | 13 | # You can also make your code fail to compile if it uses deprecated APIs. 14 | # In order to do so, uncomment the following line. 15 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 | 18 | SOURCES += \ 19 | main.cpp \ 20 | mainwindow.cpp 21 | 22 | HEADERS += \ 23 | commonhelper.h \ 24 | mainwindow.h 25 | 26 | # Default rules for deployment. 27 | qnx: target.path = /tmp/$${TARGET}/bin 28 | else: unix:!android: target.path = /opt/$${TARGET}/bin 29 | !isEmpty(target.path): INSTALLS += target 30 | -------------------------------------------------------------------------------- /commonhelper/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /commonhelper/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | #include 5 | 6 | #include "commonhelper.h" 7 | 8 | MainWindow::MainWindow(QWidget *parent) 9 | : QWidget(parent) 10 | { 11 | QPushButton *pBtn = new QPushButton("重启", this); 12 | 13 | connect(pBtn, &QPushButton::clicked, [=](){ 14 | CommonHelper::reboot(); 15 | }); 16 | 17 | QVBoxLayout *pLayout = new QVBoxLayout; 18 | pLayout->addWidget(pBtn); 19 | setLayout(pLayout); 20 | } 21 | 22 | MainWindow::~MainWindow() 23 | { 24 | } 25 | 26 | -------------------------------------------------------------------------------- /commonhelper/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | class MainWindow : public QWidget 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | MainWindow(QWidget *parent = nullptr); 12 | ~MainWindow(); 13 | }; 14 | #endif // MAINWINDOW_H 15 | -------------------------------------------------------------------------------- /iconhelper/Font Awesome Cheatsheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/iconhelper/Font Awesome Cheatsheet.png -------------------------------------------------------------------------------- /iconhelper/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/iconhelper/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /iconhelper/iconhelper.h: -------------------------------------------------------------------------------- 1 | #ifndef ICONHELPER_H 2 | #define ICONHELPER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | /** 14 | * @brief 字体图标辅助类 15 | * 1. 可设置 Label 字体图标 16 | * 2. 可设置 Button 字体图标 17 | * 3. 可设置 ListWidgetItem 字体图标 18 | * 注意:使用时需注意字体集路径: 19 | * QFontDatabase::addApplicationFont(":/font/fontawesome-webfont.ttf"); 20 | */ 21 | class IconHelper : public QObject 22 | { 23 | public: 24 | /* 设置 Label 字体图标 */ 25 | static void setIcon(QLabel *label, QChar c, int size) 26 | { 27 | static IconHelper *_instance = instance(); 28 | 29 | _instance->m_iconFont.setPointSize(size); 30 | label->setFont(_instance->m_iconFont); 31 | label->setText(c); 32 | } 33 | /* 设置 Button 字体图标 */ 34 | static void setIcon(QAbstractButton *btn, QChar c, int size) 35 | { 36 | static IconHelper *_instance = instance(); 37 | 38 | _instance->m_iconFont.setPointSize(size); 39 | btn->setFont(_instance->m_iconFont); 40 | btn->setText(c); 41 | } 42 | 43 | /* 设置 ListWidgetItem 字体图标 */ 44 | static void setIcon(QListWidgetItem *item, QChar c, int size) 45 | { 46 | static IconHelper *_instance = instance(); 47 | 48 | _instance->m_iconFont.setPointSize(size); 49 | item->setFont(_instance->m_iconFont); 50 | item->setText(c); 51 | } 52 | 53 | private: 54 | IconHelper(const IconHelper&) = delete; 55 | IconHelper &operator=(const IconHelper&) = delete; 56 | 57 | explicit IconHelper(QObject *parent = nullptr) : QObject(parent) 58 | { 59 | int fontId = QFontDatabase::addApplicationFont(":/font/fontawesome-webfont.ttf"); 60 | QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0); 61 | m_iconFont = QFont(fontName); 62 | } 63 | 64 | static IconHelper *instance() 65 | { 66 | static QMutex mutex; 67 | if (m_pInstance==nullptr) 68 | { 69 | QMutexLocker locker(&mutex); 70 | if (m_pInstance==nullptr) 71 | { 72 | m_pInstance = new IconHelper(qApp); 73 | } 74 | } 75 | 76 | return m_pInstance; 77 | } 78 | 79 | private: 80 | QFont m_iconFont; 81 | static IconHelper *m_pInstance; 82 | }; 83 | 84 | IconHelper *IconHelper::m_pInstance = nullptr; 85 | 86 | #endif // ICONHELPER_H 87 | -------------------------------------------------------------------------------- /iconhelper/iconhelper.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # The following define makes your compiler emit warnings if you use 8 | # any Qt feature that has been marked deprecated (the exact warnings 9 | # depend on your compiler). Please consult the documentation of the 10 | # deprecated API in order to know how to port your code away from it. 11 | DEFINES += QT_DEPRECATED_WARNINGS 12 | 13 | # You can also make your code fail to compile if it uses deprecated APIs. 14 | # In order to do so, uncomment the following line. 15 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 | 18 | SOURCES += \ 19 | main.cpp \ 20 | mainwindow.cpp 21 | 22 | HEADERS += \ 23 | iconhelper.h \ 24 | mainwindow.h 25 | 26 | # Default rules for deployment. 27 | qnx: target.path = /tmp/$${TARGET}/bin 28 | else: unix:!android: target.path = /opt/$${TARGET}/bin 29 | !isEmpty(target.path): INSTALLS += target 30 | 31 | RESOURCES += \ 32 | resource.qrc 33 | -------------------------------------------------------------------------------- /iconhelper/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /iconhelper/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include "iconhelper.h" 8 | 9 | MainWindow::MainWindow(QWidget *parent) 10 | : QWidget(parent) 11 | { 12 | QListWidget *pListWidget = new QListWidget(this); 13 | 14 | pListWidget->setMovement(QListView::Snap); 15 | pListWidget->setViewMode(QListView::IconMode); 16 | pListWidget->setResizeMode(QListView::Adjust); 17 | pListWidget->setSpacing(10); 18 | 19 | for (int index=0; index<500; ++index) 20 | { 21 | QListWidgetItem *item = new QListWidgetItem(pListWidget); 22 | IconHelper::setIcon(item, QChar(index + 0xf031), 16); // 注意这里 23 | pListWidget->addItem(item); 24 | } 25 | 26 | QHBoxLayout *pLayout = new QHBoxLayout; 27 | pLayout->addWidget(pListWidget); 28 | setLayout(pLayout); 29 | } 30 | 31 | MainWindow::~MainWindow() 32 | { 33 | } 34 | 35 | -------------------------------------------------------------------------------- /iconhelper/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | class MainWindow : public QWidget 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | MainWindow(QWidget *parent = nullptr); 12 | ~MainWindow(); 13 | }; 14 | #endif // MAINWINDOW_H 15 | -------------------------------------------------------------------------------- /iconhelper/resource.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | fontawesome-webfont.ttf 4 | 5 | 6 | -------------------------------------------------------------------------------- /popupmessagebox/icon.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/popupmessagebox/icon.gif -------------------------------------------------------------------------------- /popupmessagebox/icon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/popupmessagebox/icon.jpg -------------------------------------------------------------------------------- /popupmessagebox/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /popupmessagebox/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | #include "popupmessagebox.h" 6 | 7 | MainWindow::MainWindow(QWidget *parent) 8 | : QWidget(parent) 9 | { 10 | // 抖动 11 | QPointer p = PopupMessageBox::createInstance(); 12 | 13 | p->setTitle("这是一个测试工程提示"); 14 | p->setTitleIcon(QPixmap(":/popumessagebox/popupmessagebox/home.png")); 15 | p->setButtonVisible(false); 16 | p->setText("作业写完了吗!!!"); 17 | p->setTextColor(Qt::red); 18 | p->setButtonVisible(true); 19 | 20 | connect(p, &PopupMessageBox::quit, [=](){ 21 | qDebug() << "quit"; 22 | }); 23 | connect(p, &PopupMessageBox::accepted, [=](){ 24 | qDebug() << "accepted"; 25 | }); 26 | connect(p, &PopupMessageBox::rejected, [=](){ 27 | qDebug() << "rejected"; 28 | }); 29 | 30 | p->start(PopupMessageBox::Animation_Shark, -1, 800); 31 | } 32 | 33 | MainWindow::~MainWindow() 34 | { 35 | } 36 | 37 | -------------------------------------------------------------------------------- /popupmessagebox/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | class MainWindow : public QWidget 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | MainWindow(QWidget *parent = nullptr); 12 | ~MainWindow(); 13 | }; 14 | #endif // MAINWINDOW_H 15 | -------------------------------------------------------------------------------- /popupmessagebox/popupmessagebox.cpp: -------------------------------------------------------------------------------- 1 | #include "popupmessagebox.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | PopupMessageBox::PopupMessageBox(QWidget *parent) : QWidget(parent) 15 | { 16 | initUI(); 17 | initControl(); 18 | 19 | setTitle(QStringLiteral("温馨提示 ^_^ ~")); 20 | setText(QStringLiteral("可加载图片/文字/动画\n 按钮可隐藏")); 21 | setButtonVisible(false); 22 | hide(); 23 | } 24 | 25 | PopupMessageBox::~PopupMessageBox() 26 | { } 27 | 28 | void PopupMessageBox::initUI() 29 | { 30 | m_iconLbl.setObjectName(QStringLiteral("iconLbl")); 31 | m_titleLbl.setObjectName(QStringLiteral("titleLbl")); 32 | m_closeBtn.setObjectName(QStringLiteral("closeBtn")); 33 | m_mainLbl.setObjectName(QStringLiteral("mainLbl")); 34 | m_okBtn.setObjectName(QStringLiteral("okBtn")); 35 | m_cancleBtn.setObjectName(QStringLiteral("cancleBtn")); 36 | 37 | //==================== 组件属性设置 38 | m_iconLbl.setScaledContents(true); 39 | m_iconLbl.setFixedSize(20, 20); 40 | m_titleLbl.setFixedHeight(24); 41 | 42 | m_closeBtn.setFlat(true); 43 | m_closeBtn.setFixedSize(14, 14); 44 | m_closeBtn.setToolTip(QStringLiteral("关闭")); 45 | 46 | m_mainLbl.setAlignment(Qt::AlignCenter); 47 | //m_mainLbl.setScaledContents(true); 48 | m_mainLbl.setWordWrap(true); 49 | 50 | m_okBtn.setFixedSize(150, 35); 51 | m_cancleBtn.setFixedSize(150, 35); 52 | m_okBtn.setText(QStringLiteral("确定(&O)")); 53 | m_cancleBtn.setText(QStringLiteral("取消(&C)")); 54 | m_okBtn.setCursor(Qt::PointingHandCursor); 55 | m_cancleBtn.setCursor(Qt::PointingHandCursor); 56 | 57 | //================ 组件布局 58 | QWidget *titleWidget = new QWidget(this); 59 | QHBoxLayout *pLayout1 = new QHBoxLayout; 60 | titleWidget->setObjectName(QStringLiteral("titleWidget")); 61 | pLayout1->addSpacing(5); 62 | pLayout1->addWidget(&m_iconLbl); 63 | pLayout1->addSpacing(5); 64 | pLayout1->addWidget(&m_titleLbl); 65 | pLayout1->addStretch(); 66 | pLayout1->addWidget(&m_closeBtn); 67 | pLayout1->addSpacing(5); 68 | pLayout1->setContentsMargins(0, 0, 0, 0); 69 | titleWidget->setLayout(pLayout1); 70 | titleWidget->setFixedHeight(30); 71 | 72 | mpBtnLayout = new QHBoxLayout; 73 | mpBtnLayout->addWidget(&m_okBtn); 74 | mpBtnLayout->addWidget(&m_cancleBtn); 75 | mpBtnLayout->setContentsMargins(0, 10, 0, 0); 76 | 77 | QVBoxLayout *pMainLayout = new QVBoxLayout; 78 | pMainLayout->addWidget(titleWidget); 79 | pMainLayout->addWidget(&m_mainLbl); 80 | pMainLayout->addLayout(mpBtnLayout); 81 | pMainLayout->setSpacing(0); 82 | pMainLayout->setContentsMargins(0, 0, 0, 0); 83 | 84 | //================ // 样式表设置 85 | QFile file(QStringLiteral(":/popumessagebox/popupmessagebox/black.qss")); 86 | file.open(QFile::ReadOnly); 87 | this->setStyleSheet(file.readAll()); 88 | file.close(); 89 | 90 | //================ 91 | setLayout(pMainLayout); 92 | setWindowFlag(Qt::FramelessWindowHint); 93 | setFixedSize(400, 300); 94 | } 95 | 96 | void PopupMessageBox::initControl() 97 | { 98 | connect(&m_okBtn, &QPushButton::clicked, [=](){ 99 | emit accepted(); 100 | m_timer.stop(); 101 | animationQuit(); 102 | }); 103 | 104 | connect(&m_cancleBtn, &QPushButton::clicked, [=](){ 105 | emit rejected(); 106 | m_timer.stop(); 107 | animationQuit(); 108 | }); 109 | 110 | connect(&m_closeBtn, &QPushButton::clicked, [=](){ 111 | m_timer.stop(); 112 | animationQuit(); 113 | }); 114 | 115 | connect(&m_timer, &QTimer::timeout, [=](){ 116 | animationQuit(); 117 | }); 118 | 119 | setAttribute(Qt::WA_DeleteOnClose); 120 | } 121 | 122 | void PopupMessageBox::setFixedHeight(int h) 123 | { 124 | QWidget::setFixedHeight(h); 125 | } 126 | 127 | void PopupMessageBox::setFixedSize(const QSize &s) 128 | { 129 | QWidget::setFixedSize(s); 130 | } 131 | 132 | void PopupMessageBox::setFixedSize(int w, int h) 133 | { 134 | QWidget::setFixedSize(w, h); 135 | } 136 | 137 | void PopupMessageBox::setFixedWidth(int w) 138 | { 139 | QWidget::setFixedWidth(w); 140 | } 141 | 142 | QPointer PopupMessageBox::createInstance(QWidget *parent) 143 | { 144 | return (new PopupMessageBox(parent)); 145 | } 146 | 147 | void PopupMessageBox::setTitle(const QString &text) 148 | { 149 | m_titleLbl.setText(text); 150 | 151 | } 152 | 153 | QString PopupMessageBox::title() const 154 | { 155 | return m_titleLbl.text(); 156 | } 157 | 158 | void PopupMessageBox::setTitleIcon(const QPixmap &pixmap) 159 | { 160 | m_iconLbl.setPixmap(pixmap); 161 | } 162 | 163 | QPixmap PopupMessageBox::titleIcon() const 164 | { 165 | return m_iconLbl.pixmap(Qt::ReturnByValue); 166 | } 167 | 168 | void PopupMessageBox::setText(const QString &text) 169 | { 170 | m_mainLbl.setText(text); 171 | } 172 | 173 | QString PopupMessageBox::text() const 174 | { 175 | return m_mainLbl.text(); 176 | } 177 | 178 | void PopupMessageBox::setTextColor(const QColor &color) 179 | { 180 | QString c = QString::number(((color.red() << 16) | (color.green() << 8) | color.blue()), 16); 181 | 182 | m_mainLbl.setStyleSheet(QStringLiteral("QLabel{color:#%1;}").arg(c)); 183 | } 184 | 185 | void PopupMessageBox::setIcon400x270_400x215(const QPixmap & pximap) 186 | { 187 | m_mainLbl.setPixmap(pximap); 188 | } 189 | 190 | QPixmap PopupMessageBox::icon() const 191 | { 192 | return m_mainLbl.pixmap(Qt::ReturnByValue); 193 | } 194 | 195 | void PopupMessageBox::setMovie400x270_400x215(const QString &fileName) 196 | { 197 | QMovie *movie = new QMovie(fileName); 198 | movie->setParent(this); 199 | m_mainLbl.setMovie(movie); 200 | movie->start(); 201 | } 202 | 203 | const QMovie *PopupMessageBox::movie() const 204 | { 205 | return m_mainLbl.movie(); 206 | } 207 | 208 | void PopupMessageBox::setButtonVisible(bool visible) 209 | { 210 | m_okBtn.setVisible(visible); 211 | m_cancleBtn.setVisible(visible); 212 | 213 | if (visible) 214 | mpBtnLayout->setContentsMargins(0, 10, 0, 10); 215 | else 216 | mpBtnLayout->setContentsMargins(0, 0, 0, 0); 217 | } 218 | 219 | bool PopupMessageBox::isButtonVisible() const 220 | { 221 | return m_okBtn.isVisible(); 222 | } 223 | 224 | void PopupMessageBox::animationDrop(int msecs) 225 | { 226 | QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "pos"); 227 | QRect rect = QGuiApplication::screens().at(0)->availableGeometry(); 228 | 229 | pAnimation->setDuration(msecs); 230 | pAnimation->setStartValue(QPoint(rect.width()-width(), 0)); 231 | pAnimation->setEndValue(QPoint(rect.width()-width(), rect.height()-height())); 232 | pAnimation->setEasingCurve(QEasingCurve::OutElastic); 233 | pAnimation->start(QPropertyAnimation::DeleteWhenStopped); 234 | } 235 | 236 | void PopupMessageBox::animationShake(int msecs) 237 | { 238 | QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "pos"); 239 | QRect rect = QGuiApplication::screens().at(0)->availableGeometry(); 240 | QPoint endPoint(rect.width()-width(), rect.height()-height()); 241 | 242 | pAnimation->setDuration(msecs); 243 | pAnimation->setLoopCount(1); 244 | pAnimation->setKeyValueAt(0, QPoint(endPoint.x() - 3, endPoint.y() - 3)); 245 | pAnimation->setKeyValueAt(0.1, QPoint(endPoint.x() + 6, endPoint.y() + 6)); 246 | pAnimation->setKeyValueAt(0.2, QPoint(endPoint.x() - 6, endPoint.y() + 6)); 247 | pAnimation->setKeyValueAt(0.3, QPoint(endPoint.x() + 6, endPoint.y() - 6)); 248 | pAnimation->setKeyValueAt(0.4, QPoint(endPoint.x() - 6, endPoint.y() - 6)); 249 | pAnimation->setKeyValueAt(0.5, QPoint(endPoint.x() + 6, endPoint.y() + 6)); 250 | pAnimation->setKeyValueAt(0.6, QPoint(endPoint.x() - 6, endPoint.y() + 6)); 251 | pAnimation->setKeyValueAt(0.7, QPoint(endPoint.x() + 6, endPoint.y() - 6)); 252 | pAnimation->setKeyValueAt(0.8, QPoint(endPoint.x() - 6, endPoint.y() - 6)); 253 | pAnimation->setKeyValueAt(0.9, QPoint(endPoint.x() + 6, endPoint.y() + 6)); 254 | pAnimation->setKeyValueAt(1, QPoint(endPoint.x(), endPoint.y())); 255 | pAnimation->start(QAbstractAnimation::DeleteWhenStopped); 256 | } 257 | 258 | void PopupMessageBox::animationOpacity(int msecs) 259 | { 260 | QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "windowOpacity"); 261 | QRect rect = QGuiApplication::screens().at(0)->availableGeometry(); 262 | 263 | move(rect.width()-width(), rect.height()-height()); 264 | 265 | pAnimation->setDuration(msecs); 266 | pAnimation->setKeyValueAt(0, 0); 267 | pAnimation->setKeyValueAt(1, 1); 268 | pAnimation->start(QAbstractAnimation::DeleteWhenStopped); 269 | } 270 | 271 | void PopupMessageBox::animationUp(int msecs) 272 | { 273 | QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "pos"); 274 | QRect rect = QGuiApplication::screens().at(0)->availableGeometry(); 275 | 276 | pAnimation->setDuration(msecs); 277 | pAnimation->setStartValue(QPoint(rect.width()-width(), rect.height()+height())); 278 | pAnimation->setEndValue(QPoint(rect.width()-width(), rect.height()-height())); 279 | pAnimation->setEasingCurve(QEasingCurve::Linear); 280 | pAnimation->start(QPropertyAnimation::DeleteWhenStopped); 281 | } 282 | 283 | void PopupMessageBox::animationQuit(int msecs) 284 | { 285 | QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "geometry"); 286 | QRect rect = QGuiApplication::screens().at(0)->availableGeometry(); 287 | 288 | connect(pAnimation, &QPropertyAnimation::finished, [=](){ 289 | emit quit(); 290 | close(); 291 | }); 292 | 293 | pAnimation->setDuration(msecs); 294 | pAnimation->setStartValue(QRect(rect.width()-width(), rect.height()-height(), width(), height())); 295 | pAnimation->setEndValue(QRect(rect.width(), rect.height()-height(), width(), height())); 296 | pAnimation->setEasingCurve(QEasingCurve::Linear); 297 | pAnimation->start(QPropertyAnimation::DeleteWhenStopped); 298 | } 299 | 300 | void PopupMessageBox::animationKeep(int msecs) 301 | { 302 | m_timer.start(msecs); 303 | } 304 | 305 | void PopupMessageBox::start(AnimationType type, int keepTimeMs, int enterTimeMs) 306 | { 307 | show(); 308 | 309 | switch (type) 310 | { 311 | case Animation_Drop: 312 | animationDrop(enterTimeMs); 313 | break; 314 | case Animation_Shark: 315 | animationShake(enterTimeMs); 316 | break; 317 | case Animation_Opacity: 318 | animationOpacity(enterTimeMs); 319 | break; 320 | case Animation_Up: 321 | animationUp(enterTimeMs); 322 | break; 323 | } 324 | 325 | if (keepTimeMs >= 0) 326 | animationKeep(enterTimeMs + keepTimeMs); 327 | } 328 | 329 | void PopupMessageBox::terminate() 330 | { 331 | close(); 332 | } 333 | 334 | 335 | 336 | 337 | 338 | -------------------------------------------------------------------------------- /popupmessagebox/popupmessagebox.h: -------------------------------------------------------------------------------- 1 | #ifndef POPUPMESSAGEBOX_H 2 | #define POPUPMESSAGEBOX_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | /** 15 | * @brief 右下角提示对对话框 16 | * 1. 可设置标题 17 | * 2. 可设置标题图标 18 | * 3. 可设置提示文本 19 | * 4. 可设置提示图片 20 | * 5. 可设置提示动画 21 | * 6. 可设置操作按钮是否可见 22 | * 7. 可设置对话框入场动画时间 23 | * 8. 可设置对话框停留时间 24 | * 9. 可随时终止一个对话框 25 | * 10. 对话框退出时,发送退出信号 26 | * 11. 确定按钮被点击时,发送确认信息 27 | * 12. 取消按钮被点击时,发送取消信号 28 | * 使用时需要注意资源文件路径!! 29 | */ 30 | class PopupMessageBox : public QWidget 31 | { 32 | Q_OBJECT 33 | 34 | public: 35 | enum AnimationType 36 | { 37 | Animation_Drop = 0u, 38 | Animation_Shark, 39 | Animation_Opacity, 40 | Animation_Up, 41 | }; 42 | 43 | Q_PROPERTY(QString title READ title WRITE setTitle); 44 | Q_PROPERTY(QPixmap titleIcon READ titleIcon WRITE setTitleIcon); 45 | Q_PROPERTY(QString text READ text WRITE setText); 46 | Q_PROPERTY(QPixmap icon READ icon WRITE setIcon400x270_400x215); 47 | Q_PROPERTY(bool isButtonVisible READ isButtonVisible WRITE setButtonVisible); 48 | 49 | public: 50 | ~PopupMessageBox(); 51 | 52 | /* 创建一个提示对话框 */ 53 | static QPointer createInstance(QWidget *parent = nullptr); 54 | /* 获取标题 */ 55 | QString title() const; 56 | /* 获取标题图标 */ 57 | QPixmap titleIcon() const; 58 | /* 获取提示文本 */ 59 | QString text() const; 60 | /* 获取提示动画 */ 61 | QPixmap icon() const; 62 | /* 获取提示动画 */ 63 | const QMovie *movie() const; 64 | /* 获取操作按钮是否可见 */ 65 | bool isButtonVisible() const; 66 | 67 | public slots: 68 | /* 设置标题 */ 69 | void setTitle(const QString &text); 70 | /* 设置标题图标 */ 71 | void setTitleIcon(const QPixmap &pixmap); 72 | /* 设置提示文本 */ 73 | void setText(const QString &text); 74 | /* 设置提示文本颜色 */ 75 | void setTextColor(const QColor &color); 76 | /* 设置提示图片 */ 77 | void setIcon400x270_400x215(const QPixmap &pximap); 78 | /* 设置提示动画 */ 79 | void setMovie400x270_400x215(const QString &fileName); 80 | /* 设置操作按钮是否可见 */ 81 | void setButtonVisible(bool visible); 82 | /* 启动提示对话框 (keepTimeMs=-1;对话框不主动退出)*/ 83 | void start(AnimationType type = Animation_Opacity, int keepTimeMs = 2000, int enterTimeMs = 1000); 84 | /* 终止提示对话框 */ 85 | void terminate(); 86 | 87 | signals: 88 | /* 对话框退出 */ 89 | void quit(); 90 | /* 确定按钮被点击 */ 91 | void accepted(); 92 | /* 取消按钮被点击 */ 93 | void rejected(); 94 | 95 | private: 96 | explicit PopupMessageBox(QWidget *parent = nullptr); 97 | void initUI(); 98 | void initControl(); 99 | void setFixedHeight(int h); 100 | void setFixedSize(const QSize &s); 101 | void setFixedSize(int w, int h); 102 | void setFixedWidth(int w); 103 | void animationDrop(int msecs); 104 | void animationShake(int msecs); 105 | void animationOpacity(int msecs); 106 | void animationUp(int msecs); 107 | void animationQuit(int msecs = 500); 108 | void animationKeep(int msecs); 109 | 110 | private: 111 | QLabel m_iconLbl; 112 | QLabel m_titleLbl; 113 | QPushButton m_closeBtn; 114 | QLabel m_mainLbl; 115 | QPushButton m_okBtn; 116 | QPushButton m_cancleBtn; 117 | QHBoxLayout *mpBtnLayout = nullptr; 118 | 119 | QTimer m_timer; 120 | }; 121 | 122 | #endif // POPUPMESSAGEBOX_H 123 | -------------------------------------------------------------------------------- /popupmessagebox/popupmessagebox.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # The following define makes your compiler emit warnings if you use 8 | # any Qt feature that has been marked deprecated (the exact warnings 9 | # depend on your compiler). Please consult the documentation of the 10 | # deprecated API in order to know how to port your code away from it. 11 | DEFINES += QT_DEPRECATED_WARNINGS 12 | 13 | # You can also make your code fail to compile if it uses deprecated APIs. 14 | # In order to do so, uncomment the following line. 15 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 | 18 | SOURCES += \ 19 | main.cpp \ 20 | mainwindow.cpp \ 21 | popupmessagebox.cpp 22 | 23 | HEADERS += \ 24 | mainwindow.h \ 25 | popupmessagebox.h 26 | 27 | # Default rules for deployment. 28 | qnx: target.path = /tmp/$${TARGET}/bin 29 | else: unix:!android: target.path = /opt/$${TARGET}/bin 30 | !isEmpty(target.path): INSTALLS += target 31 | 32 | RESOURCES += \ 33 | resource.qrc 34 | -------------------------------------------------------------------------------- /popupmessagebox/popupmessagebox/black.qss: -------------------------------------------------------------------------------- 1 | QWidget { 2 | font:normal normal 16px "微软雅黑"; 3 | } 4 | 5 | QWidget#titleWidget{ 6 | background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #4D4D4D, stop:1 #292929); 7 | } 8 | 9 | QLabel#iconLbl { 10 | border-image:url(:/popumessagebox/popupmessagebox/home.png); 11 | } 12 | 13 | QLabel#titleLbl { 14 | color:white; 15 | } 16 | 17 | QPushButton#closeBtn { 18 | border-image:url(:/popumessagebox/popupmessagebox/close.png); 19 | } 20 | 21 | QPushButton#closeBtn:hover { 22 | border-image:url(:/popumessagebox/popupmessagebox/close_hover.png); 23 | } 24 | 25 | QPushButton#closeBtn:pressed { 26 | border-image:url(:/popumessagebox/popupmessagebox/close_pressed.png); 27 | } 28 | 29 | QLabel#mainLbl { 30 | color:skyblue; 31 | font-size:28px; 32 | background-color:transparent; 33 | border:0px none transparent; 34 | } 35 | 36 | QPushButton#okBtn, 37 | QPushButton#cancleBtn 38 | { 39 | color:white; 40 | background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #4D4D4D, stop:1 #292929); 41 | border:0px none; 42 | border-radius:5px; 43 | width:80px; 44 | height:35px; 45 | font-size:18px 46 | } 47 | 48 | QPushButton#okBtn:hover, 49 | QPushButton#cancleBtn:hover 50 | { 51 | background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #636363, stop:1 #575757); 52 | } 53 | 54 | QPushButton#okBtn:pressed, 55 | QPushButton#cancleBtn:pressed 56 | { 57 | background:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #4D4D4D, stop:1 #292929); 58 | padding:1px -1px -1px 1px; 59 | } 60 | 61 | -------------------------------------------------------------------------------- /popupmessagebox/popupmessagebox/close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/popupmessagebox/popupmessagebox/close.png -------------------------------------------------------------------------------- /popupmessagebox/popupmessagebox/close_hover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/popupmessagebox/popupmessagebox/close_hover.png -------------------------------------------------------------------------------- /popupmessagebox/popupmessagebox/close_pressed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/popupmessagebox/popupmessagebox/close_pressed.png -------------------------------------------------------------------------------- /popupmessagebox/popupmessagebox/home.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/popupmessagebox/popupmessagebox/home.png -------------------------------------------------------------------------------- /popupmessagebox/resource.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | popupmessagebox/black.qss 4 | popupmessagebox/close.png 5 | popupmessagebox/close_hover.png 6 | popupmessagebox/close_pressed.png 7 | popupmessagebox/home.png 8 | 9 | 10 | -------------------------------------------------------------------------------- /simplemessagebox/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /simplemessagebox/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include "simplemessagebox.h" 4 | #include 5 | 6 | MainWindow::MainWindow(QWidget *parent) 7 | : QWidget(parent) 8 | { 9 | qDebug() << SimpleMessageBox::infomationMessageBox("这是一个测试,今天心情好不啦,啦啦啦,嘿嘿嘿"); 10 | qDebug() << SimpleMessageBox::errorMessageBox("祝你平安喜乐,万事胜意"); 11 | qDebug() << SimpleMessageBox::questionMessageBox("bug修复了吗?"); 12 | } 13 | 14 | MainWindow::~MainWindow() 15 | { 16 | } 17 | 18 | -------------------------------------------------------------------------------- /simplemessagebox/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | class MainWindow : public QWidget 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | MainWindow(QWidget *parent = nullptr); 12 | ~MainWindow(); 13 | }; 14 | #endif // MAINWINDOW_H 15 | -------------------------------------------------------------------------------- /simplemessagebox/resource.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | simplemessagebox/black.qss 4 | simplemessagebox/close.png 5 | simplemessagebox/close_hover.png 6 | simplemessagebox/close_pressed.png 7 | simplemessagebox/error.png 8 | simplemessagebox/home.png 9 | simplemessagebox/info.png 10 | simplemessagebox/question.png 11 | 12 | 13 | -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox.cpp: -------------------------------------------------------------------------------- 1 | #include "simplemessagebox.h" 2 | 3 | SimpleMessageBox::SimpleMessageBox() 4 | { 5 | this->setWindowFlag(Qt::FramelessWindowHint); 6 | 7 | initUI(); 8 | initControl(); 9 | } 10 | 11 | void SimpleMessageBox::initUI() 12 | { 13 | m_iconLbl.setObjectName(QStringLiteral("iconLbl")); 14 | m_titleLbl.setObjectName(QStringLiteral("titleLbl")); 15 | m_closeBtn.setObjectName(QStringLiteral("closeBtn")); 16 | m_mainLbl.setObjectName(QStringLiteral("mainLbl")); 17 | m_textLbl.setObjectName(QStringLiteral("textLbl")); 18 | m_okBtn.setObjectName(QStringLiteral("okBtn")); 19 | m_cancleBtn.setObjectName(QStringLiteral("cancleBtn")); 20 | 21 | //================ 组件属性设置 22 | m_iconLbl.setFixedSize(20, 20); 23 | m_titleLbl.setFixedHeight(24); 24 | 25 | m_closeBtn.setFlat(true); 26 | m_closeBtn.setFixedSize(14, 14); 27 | m_closeBtn.setToolTip(QStringLiteral("关闭")); 28 | 29 | m_mainLbl.setScaledContents(true); 30 | m_mainLbl.setFixedSize(64, 64); 31 | 32 | m_textLbl.setWordWrap(true); 33 | m_textLbl.setAlignment(Qt::AlignCenter); 34 | 35 | m_okBtn.setFocus(); 36 | m_okBtn.setFixedSize(80, 35); 37 | m_cancleBtn.setFixedSize(80, 35); 38 | m_okBtn.setText(QStringLiteral("确定(&O)")); 39 | m_cancleBtn.setText(QStringLiteral("取消(&C)")); 40 | m_okBtn.setCursor(Qt::PointingHandCursor); 41 | m_cancleBtn.setCursor(Qt::PointingHandCursor); 42 | 43 | //================ 组件布局 44 | QWidget *titleWidget = new QWidget(this); 45 | QHBoxLayout *pLayout1 = new QHBoxLayout; 46 | titleWidget->setObjectName(QStringLiteral("titleWidget")); 47 | pLayout1->addSpacing(5); 48 | pLayout1->addWidget(&m_iconLbl); 49 | pLayout1->addSpacing(5); 50 | pLayout1->addWidget(&m_titleLbl); 51 | pLayout1->addStretch(); 52 | pLayout1->addWidget(&m_closeBtn); 53 | pLayout1->addSpacing(5); 54 | pLayout1->setMargin(0); 55 | titleWidget->setFixedHeight(30); 56 | titleWidget->setLayout(pLayout1); 57 | 58 | QHBoxLayout *pLayout2 = new QHBoxLayout; 59 | pLayout2->addWidget(&m_mainLbl); 60 | pLayout2->addSpacing(10); 61 | pLayout2->addWidget(&m_textLbl); 62 | 63 | QHBoxLayout *pLayout3 = new QHBoxLayout; 64 | pLayout3->addStretch(true); 65 | pLayout3->addWidget(&m_okBtn); 66 | pLayout3->addWidget(&m_cancleBtn); 67 | 68 | QVBoxLayout *pLayout4 = new QVBoxLayout; 69 | pLayout4->addLayout(pLayout2); 70 | pLayout4->addLayout(pLayout3); 71 | pLayout4->setContentsMargins(10, 0, 10, 0); 72 | 73 | QVBoxLayout *pMainLayout = new QVBoxLayout; 74 | pMainLayout->addWidget(titleWidget); 75 | pMainLayout->addSpacing(10); 76 | pMainLayout->addLayout(pLayout4); 77 | pMainLayout->setContentsMargins(0, 0, 0, 10); 78 | 79 | //================ 样式表设置 80 | QFile file(QStringLiteral(":/simplemessagebox/simplemessagebox/black.qss")); 81 | file.open(QFile::ReadOnly); 82 | this->setStyleSheet(file.readAll()); 83 | file.close(); 84 | 85 | //================ 86 | setLayout(pMainLayout); 87 | setMinimumSize(300, 40); 88 | } 89 | 90 | void SimpleMessageBox::initControl() 91 | { 92 | connect(&m_okBtn, &QPushButton::clicked, [=](){ 93 | done(QDialog::Accepted); 94 | close(); 95 | }); 96 | 97 | connect(&m_cancleBtn, &QPushButton::clicked, [=](){ 98 | done(QDialog::Rejected); 99 | close(); 100 | }); 101 | 102 | connect(&m_closeBtn, &QPushButton::clicked, [=](){ 103 | done(-1); 104 | close(); 105 | }); 106 | } 107 | 108 | int SimpleMessageBox::infomationMessageBox(const QString &text) 109 | { 110 | SimpleMessageBox box; 111 | 112 | box.m_mainLbl.setPixmap(QPixmap(":/simplemessagebox/simplemessagebox/info.png")); 113 | box.m_titleLbl.setText(QStringLiteral("提示")); 114 | box.m_cancleBtn.setVisible(false); 115 | box.m_textLbl.setText(text); 116 | 117 | return box.exec(); 118 | } 119 | 120 | int SimpleMessageBox::questionMessageBox(const QString &text) 121 | { 122 | SimpleMessageBox box; 123 | 124 | box.m_mainLbl.setPixmap(QPixmap(":/simplemessagebox/simplemessagebox/question.png")); 125 | box.m_titleLbl.setText(QStringLiteral("询问")); 126 | box.m_textLbl.setText(text); 127 | 128 | return box.exec(); 129 | } 130 | 131 | int SimpleMessageBox::errorMessageBox(const QString &text) 132 | { 133 | SimpleMessageBox box; 134 | 135 | box.m_mainLbl.setPixmap(QPixmap(":/simplemessagebox/simplemessagebox/error.png")); 136 | box.m_titleLbl.setText(QStringLiteral("错误")); 137 | box.m_cancleBtn.setVisible(false); 138 | box.m_textLbl.setText(text); 139 | 140 | return box.exec(); 141 | } 142 | 143 | void SimpleMessageBox::mouseMoveEvent(QMouseEvent *event) 144 | { 145 | if (mousePressed) 146 | { 147 | this->move(event->globalPos() - mousePoint); 148 | event->accept(); 149 | } 150 | } 151 | 152 | void SimpleMessageBox::mousePressEvent(QMouseEvent *event) 153 | { 154 | if (event->button() == Qt::LeftButton) 155 | { 156 | mousePressed = true; 157 | mousePoint = event->globalPos() - this->pos(); 158 | event->accept(); 159 | } 160 | } 161 | 162 | void SimpleMessageBox::mouseReleaseEvent(QMouseEvent *event) 163 | { 164 | Q_UNUSED(event) 165 | 166 | mousePressed = false; 167 | } 168 | 169 | -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMONMESSAGEBOX_H 2 | #define COMMONMESSAGEBOX_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | /** 15 | * @brief 简易的的消息对话框 16 | * 1. 消息提示对话框 17 | * 2. 询问提示对框框 18 | * 3. 错误提示对话框 19 | * 使用时需要注意资源文件路径!! 20 | */ 21 | class SimpleMessageBox : public QDialog 22 | { 23 | Q_OBJECT 24 | public: 25 | /* 消息提示对话框 */ 26 | static int infomationMessageBox(const QString &text); 27 | /* 询问提示对框框 */ 28 | static int questionMessageBox(const QString &text); 29 | /* 错误提示对话框 */ 30 | static int errorMessageBox(const QString &text); 31 | 32 | protected: 33 | void mouseMoveEvent(QMouseEvent *event) override; 34 | void mousePressEvent(QMouseEvent *event) override; 35 | void mouseReleaseEvent(QMouseEvent *event) override; 36 | 37 | private: 38 | SimpleMessageBox(); 39 | 40 | void initUI(); 41 | void initControl(); 42 | 43 | private: 44 | QPoint mousePoint; 45 | bool mousePressed = false; 46 | 47 | QLabel m_iconLbl; 48 | QLabel m_titleLbl; 49 | QPushButton m_closeBtn; 50 | QLabel m_mainLbl; 51 | QLabel m_textLbl; 52 | QPushButton m_okBtn; 53 | QPushButton m_cancleBtn; 54 | }; 55 | 56 | #endif // COMMONMESSAGEBOX_H 57 | -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | # The following define makes your compiler emit warnings if you use 8 | # any Qt feature that has been marked deprecated (the exact warnings 9 | # depend on your compiler). Please consult the documentation of the 10 | # deprecated API in order to know how to port your code away from it. 11 | DEFINES += QT_DEPRECATED_WARNINGS 12 | 13 | # You can also make your code fail to compile if it uses deprecated APIs. 14 | # In order to do so, uncomment the following line. 15 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 | 18 | SOURCES += \ 19 | main.cpp \ 20 | mainwindow.cpp \ 21 | simplemessagebox.cpp 22 | 23 | HEADERS += \ 24 | mainwindow.h \ 25 | simplemessagebox.h 26 | 27 | # Default rules for deployment. 28 | qnx: target.path = /tmp/$${TARGET}/bin 29 | else: unix:!android: target.path = /opt/$${TARGET}/bin 30 | !isEmpty(target.path): INSTALLS += target 31 | 32 | RESOURCES += \ 33 | resource.qrc 34 | -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox/black.qss: -------------------------------------------------------------------------------- 1 | QWidget { 2 | font:normal normal 16px "微软雅黑"; 3 | } 4 | 5 | QWidget#titleWidget{ 6 | background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #4D4D4D, stop:1 #292929); 7 | } 8 | 9 | QLabel#iconLbl { 10 | border-image:url(:/simplemessagebox/simplemessagebox/home.png); 11 | } 12 | 13 | QLabel#titleLbl { 14 | color:white; 15 | } 16 | 17 | QPushButton#closeBtn { 18 | border-image:url(:/simplemessagebox/simplemessagebox/close.png); 19 | } 20 | 21 | QPushButton#closeBtn:hover { 22 | border-image:url(:/simplemessagebox/simplemessagebox/close_hover.png); 23 | } 24 | 25 | QPushButton#closeBtn:pressed { 26 | border-image:url(:/simplemessagebox/simplemessagebox/close_pressed.png); 27 | } 28 | 29 | QLabel#textLbl { 30 | font-size:17px; 31 | } 32 | 33 | QPushButton#okBtn, 34 | QPushButton#cancleBtn 35 | { 36 | color:white; 37 | background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #4D4D4D, stop:1 #292929); 38 | border:0px none transparent; 39 | border-radius:5px; 40 | font-size:18px 41 | } 42 | 43 | QPushButton#okBtn:hover, 44 | QPushButton#cancleBtn:hover 45 | { 46 | background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #636363, stop:1 #575757); 47 | } 48 | 49 | QPushButton#okBtn:pressed, 50 | QPushButton#cancleBtn:pressed 51 | { 52 | background:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #4D4D4D, stop:1 #292929); 53 | padding:1px -1px -1px 1px; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox/close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/simplemessagebox/simplemessagebox/close.png -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox/close_hover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/simplemessagebox/simplemessagebox/close_hover.png -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox/close_pressed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/simplemessagebox/simplemessagebox/close_pressed.png -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox/error.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/simplemessagebox/simplemessagebox/error.png -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox/home.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/simplemessagebox/simplemessagebox/home.png -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox/info.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/simplemessagebox/simplemessagebox/info.png -------------------------------------------------------------------------------- /simplemessagebox/simplemessagebox/question.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cocowts/QtCustomcomponent/d9dcaa08e7426424c7c269af94c8df206df630e3/simplemessagebox/simplemessagebox/question.png --------------------------------------------------------------------------------