自己写的Qt控件(带三角指示的弹出框),开源给你们

  • A+
所属分类:Qt专栏

效果

自己写的Qt控件(带三角指示的弹出框),开源给你们

源码

trianglepopupwidget.h文件

  1. #ifndef TRIANGLEPOPUPWIDGET_H
  2. #define TRIANGLEPOPUPWIDGET_H
  3. #include <QWidget>
  4. #include <QPoint>
  5. #include <QRect>
  6. #include <QSize>
  7. class TrianglePopupWidget: public QWidget
  8. {
  9.     Q_OBJECT
  10. public:
  11.     TrianglePopupWidget(QPoint triangleStartPosition
  12.                         ,QSize rectSize,QRect windowGeometry=QRect(0,0,1920,1080),QWidget *parent = 0);
  13.     ~TrianglePopupWidget();
  14.     // 设置中间区域widget;
  15.     void addCenterWidget(QWidget* widget);
  16.     void setTriangleSize(const QSize &triangleSize);
  17.     QRect windowGeometry() const;
  18.     void setWindowGeometry(const QRect &windowGeometry);
  19.     void setMargins(int left, int top, int right, int bottom);
  20.     QColor backgroundColor() const;
  21.     void setBackgroundColor(const QColor &backgroundColor);
  22. protected:
  23.     void paintEvent(QPaintEvent *);
  24. private:
  25.     // 小三角起始位置;
  26.     QPoint m_triangleStartPosition;
  27.     // 小三角的size;
  28.     QSize m_triangleSize;
  29.     // 是否绘制阴影
  30.     int m_isDrawShadowOrNot;
  31.     // 内容矩形的长宽
  32.     QSize m_rectSize;
  33.     QColor m_backgroundColor;
  34.     QLayout* m_layout;
  35.     //qmaindow大小
  36.     QRect m_windowGeometry;
  37.     void calculateGeometry();
  38. };
  39. #endif // TRIANGLEPOPUPWIDGET_H

trianglepopupwidget.cpp文件

  1. #include "trianglepopupwidget.h"
  2. #include <QHBoxLayout>
  3. #include <QPainter>
  4. #include <QGraphicsDropShadowEffect>
  5. #define SHADOW_WIDTH 0                 // 窗口阴影宽度;
  6. #define TRIANGLE_WIDTH 15               // 小三角的宽度;
  7. #define TRIANGLE_HEIGHT 10              // 小三角的高度;
  8. #define BORDER_RADIUS 5                 // 窗口边角的弧度;
  9. TrianglePopupWidget::TrianglePopupWidget(QPoint triangleStartPosition
  10.                                          ,QSize rectSize
  11.                                          ,QRect windowGeometry
  12.                                          ,QWidget *parent)
  13.     : QWidget(parent)
  14.     , m_rectSize(rectSize)
  15.     , m_triangleStartPosition(triangleStartPosition)
  16.     , m_isDrawShadowOrNot(false)
  17.     , m_windowGeometry(windowGeometry)
  18.     , m_layout(NULL)
  19. {
  20.     setWindowFlags(Qt::FramelessWindowHint);
  21.     setAttribute(Qt::WA_TranslucentBackground);
  22.     m_triangleSize = QSize(TRIANGLE_WIDTH,TRIANGLE_HEIGHT);
  23.     m_backgroundColor = QColor(255,255,255);
  24.     // 设置阴影边框;
  25.     if(m_isDrawShadowOrNot)
  26.     {
  27.         QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
  28.         shadowEffect->setOffset(0, 0);
  29.         shadowEffect->setColor(Qt::gray);
  30.         shadowEffect->setBlurRadius(SHADOW_WIDTH);
  31.         this->setGraphicsEffect(shadowEffect);
  32.     }
  33.     calculateGeometry();
  34. }
  35. TrianglePopupWidget::~TrianglePopupWidget()
  36. {
  37. }
  38. //计算整个widget的Geometry
  39. void TrianglePopupWidget::calculateGeometry()
  40. {
  41.     int x=0;
  42.     int y=0;
  43.     int width = m_rectSize.width();
  44.     int height = m_rectSize.height()+m_triangleSize.height();
  45.     int standard_startX = m_triangleStartPosition.x()-m_rectSize.width()/2;
  46.     int standard_startY = m_triangleStartPosition.y()-m_triangleSize.height()-m_rectSize.height();
  47.     int standard_endX = m_triangleStartPosition.x()-m_rectSize.width()/2+width;
  48.     int standard_endY = m_triangleStartPosition.y()-m_triangleSize.height()-m_rectSize.height() + height;
  49.     if(standard_startX<m_windowGeometry.x()+10)
  50.     {
  51.         standard_startX =  m_windowGeometry.x()+10;
  52.     }
  53.     if(standard_endX>m_windowGeometry.y()+m_windowGeometry.width()-10)
  54.     {
  55.         standard_startX =  standard_startX - (standard_endX-(m_windowGeometry.y()+m_windowGeometry.width()-10));
  56.     }
  57.     //微调
  58.     if(m_triangleStartPosition.x()<standard_startX+m_triangleSize.width()/2+BORDER_RADIUS)
  59.     {
  60.         standard_startX = m_triangleStartPosition.x() - BORDER_RADIUS - m_triangleSize.width()/2;
  61.     }
  62.     //微调
  63.     if(m_triangleStartPosition.x()>standard_startX+width-m_triangleSize.width()/2-BORDER_RADIUS )
  64.     {
  65.         standard_startX = standard_startX+m_triangleSize.width()/2+BORDER_RADIUS ;
  66.     }
  67.     x = standard_startX;
  68.     y = standard_startY;
  69.     this->setGeometry(x,y,width,height);
  70. }
  71. void TrianglePopupWidget::addCenterWidget(QWidget* widget)
  72. {
  73.     if(NULL == m_layout)
  74.     {
  75.         m_layout = new QHBoxLayout(this);
  76.         m_layout->setSpacing(10);
  77.         m_layout->setContentsMargins(10,10,10,10+TRIANGLE_HEIGHT);
  78.     }
  79.     m_layout->addWidget(widget);
  80. }
  81. void TrianglePopupWidget::setMargins(int left,int top,int right,int bottom)
  82. {
  83.     if(NULL != m_layout)
  84.     {
  85.         m_layout->setContentsMargins(left,top,right,bottom+TRIANGLE_HEIGHT);
  86.     }
  87. }
  88. void TrianglePopupWidget::paintEvent(QPaintEvent *)
  89. {
  90.     QPainter painter(this);
  91.     painter.setRenderHint(QPainter::Antialiasing, true);
  92.     painter.setPen(Qt::NoPen);
  93.     painter.setBrush(m_backgroundColor);
  94.     // 小三角区域
  95.     QPolygon trianglePolygon;
  96.     QPoint pt_triStartPosRelaThis(m_triangleStartPosition.x()-geometry().x(),
  97.                                    m_triangleStartPosition.y()-geometry().y());
  98. //    int x1=m_triangleStartPosition.x()-geometry().x();
  99. //    int y1=m_triangleStartPosition.y()-geometry().y();
  100. //    int x2=pt_triStartPosRelaThis.x() + m_triangleSize.width() / 2;
  101. //    int y2=pt_triStartPosRelaThis.y()-m_triangleSize.height();
  102. //    int x3=pt_triStartPosRelaThis.x() - m_triangleSize.width() / 2;
  103. //    int y3=pt_triStartPosRelaThis.y()-m_triangleSize.height();
  104.     trianglePolygon << pt_triStartPosRelaThis;
  105.     trianglePolygon << QPoint(pt_triStartPosRelaThis.x() + m_triangleSize.width() / 2
  106.                               , pt_triStartPosRelaThis.y()-m_triangleSize.height());
  107.     trianglePolygon << QPoint(pt_triStartPosRelaThis.x() - m_triangleSize.width() / 2
  108.                               , pt_triStartPosRelaThis.y()-m_triangleSize.height());
  109.     QPainterPath drawPath;
  110.     //矩形区域
  111.     drawPath.addRoundedRect(QRect(0,0,m_rectSize.width(),m_rectSize.height()),BORDER_RADIUS,BORDER_RADIUS);
  112.     // Rect + Triangle;
  113.     drawPath.addPolygon(trianglePolygon);
  114.     painter.drawPath(drawPath);
  115. }
  116. QColor TrianglePopupWidget::backgroundColor() const
  117. {
  118.     return m_backgroundColor;
  119. }
  120. void TrianglePopupWidget::setBackgroundColor(const QColor &backgroundColor)
  121. {
  122.     m_backgroundColor = backgroundColor;
  123. }
  124. QRect TrianglePopupWidget::windowGeometry() const
  125. {
  126.     return m_windowGeometry;
  127. }
  128. void TrianglePopupWidget::setWindowGeometry(const QRect &windowGeometry)
  129. {
  130.     m_windowGeometry = windowGeometry;
  131. }
  132. void TrianglePopupWidget::setTriangleSize(const QSize &triangleSize)
  133. {
  134.     m_triangleSize = triangleSize;
  135. }

使用方法

  1. TrianglePopupWidget* w = new TrianglePopupWidget(
  2.             pt
  3.             ,QSize(400,100)
  4.             ,QRect(0,0,800,600)
  5.             ,this);
  6. QLabel* textLabel = new QLabel;
  7. textLabel->setAlignment(Qt::AlignCenter);
  8. textLabel->setText("ArrowWidget");
  9. textLabel->setStyleSheet("background-color:red");
  10. w->addCenterWidget(textLabel);
  11. QLabel* textLabel2 = new QLabel;
  12. textLabel2->setAlignment(Qt::AlignCenter);
  13. textLabel2->setText("DDDDA");
  14. textLabel2->setStyleSheet("background-color:green");
  15. w->addCenterWidget(textLabel2);
  16. QLabel* textLabel3 = new QLabel;
  17. textLabel3->setAlignment(Qt::AlignCenter);
  18. textLabel3->setText("bbbb");
  19. textLabel3->setStyleSheet("background-color:yellow");
  20. w->addCenterWidget(textLabel3);
  21. QColor clr(0,0,255);
  22. w->setBackgroundColor(clr);
  23. w->show();

代码下载地址

https://github.com/2273629531/TrianglePopupWidget

 

  • 我的微信
  • weinxin
  • 微信公众号
  • weinxin
阿拉灯aladeng

发表评论

您必须才能发表评论!