• qt 文本滚动条


    开发项目中学习到的知识,记录一下。文本在qlabel控件中来回滚动

    1. #ifndef SCROLLLABEL_H
    2. #define SCROLLLABEL_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. class ScrollLabel : public QLabel
    8. {
    9. public:
    10. ScrollLabel(QWidget *parent = nullptr);
    11. public:
    12. void paintEvent(QPaintEvent *);
    13. void UpdateIndex();
    14. void StartScroll(int p_IsUp, QString p_Str1, QString p_Str2);
    15. void SetFont(QFont p_Font);
    16. void SetText(QString p_Str);
    17. private:
    18. QTimer *m_Timer;
    19. int m_CurIndex;
    20. int m_IsUp;
    21. QString m_Str1;
    22. QString m_Str2;
    23. QFont m_Font;
    24. };
    25. #endif // SCROLLLABEL_H
    1. #include "scrolllabel.h"
    2. #include
    3. #include
    4. #include
    5. ScrollLabel::ScrollLabel(QWidget *parent): QLabel(parent)
    6. {
    7. m_CurIndex = 0;
    8. m_Timer = new QTimer(this);
    9. m_Timer->setInterval(20);
    10. connect(m_Timer, &QTimer::timeout, this, &ScrollLabel::UpdateIndex);
    11. }
    12. void ScrollLabel::paintEvent(QPaintEvent *)
    13. {
    14. QPen pen;
    15. pen.setColor(QColor(255,255,255));
    16. QPainter painter(this);
    17. painter.setPen(pen);
    18. painter.setFont(m_Font);
    19. QFontMetrics FontMetrics(m_Font);
    20. int Str1Width = FontMetrics.width(m_Str1);//字符的宽度
    21. int Str2Width = FontMetrics.width(m_Str2);//字符的宽度
    22. int CharWidth = FontMetrics.width("a");//每个字符的宽度
    23. int Interval = (width() - Str1Width)/2;
    24. int Interval2 = (width() - Str2Width)/2;
    25. if(0 == m_CurIndex)
    26. {
    27. painter.drawText(Interval , 22, m_Str1);
    28. }
    29. else
    30. {
    31. if(m_IsUp)
    32. {
    33. painter.drawText(Interval+CharWidth*m_CurIndex, 22, m_Str1);
    34. painter.drawText(CharWidth*m_CurIndex - Str2Width + (Interval - Interval2), 22, m_Str2);
    35. if(Interval + CharWidth*m_CurIndex > width())
    36. {
    37. m_Timer->stop( );
    38. m_CurIndex = 0;
    39. }
    40. }
    41. else
    42. {
    43. painter.drawText((width() -Str1Width)/2-CharWidth*m_CurIndex, 22, m_Str1);
    44. painter.drawText(width() - CharWidth*m_CurIndex+(Interval2 - Interval), 22, m_Str2);
    45. if(Interval + Str1Width < CharWidth*m_CurIndex)
    46. {
    47. m_Timer->stop( );
    48. m_CurIndex = 0;
    49. }
    50. }
    51. }
    52. }
    53. void ScrollLabel::UpdateIndex()
    54. {
    55. update();
    56. m_CurIndex++;
    57. }
    58. void ScrollLabel::SetFont(QFont p_Font)
    59. {
    60. m_Font = p_Font;
    61. }
    62. void ScrollLabel::SetText(QString p_Str)
    63. {
    64. m_Str1 = p_Str;
    65. update();
    66. }
    67. void ScrollLabel::StartScroll(int p_IsUp, QString p_Str1, QString p_Str2)
    68. {
    69. m_IsUp = p_IsUp;
    70. m_Str1 = p_Str1;
    71. m_Str2 = p_Str2;
    72. m_Timer->start( );
    73. }
    1. 调用方法:
    2. m_total_mileage_text_label = new ScrollLabel(this);
    3. //开始滚动
    4. m_total_mileage_text_label->StartScroll(IsUp, Str1, Str2);
    5. IsUp 1:指滚向上一条 0:滚向下一条
    6. str1和str2分别指滚动前的字符串和滚动后的字符串

  • 相关阅读:
    ROS1 学习11 坐标系tf 管理系统 简介及demo示例
    51单片机学习笔记4 新建工程及点亮LED实战
    (六)正点原子STM32MP135移植——内核移植
    React Native 技术选型分析
    华为发布“十大发明”,包含计算、智能驾驶等新领域
    Windows下Qt读取系统的内存、CPU、GPU等使用信息
    JAVA 读取写入文件
    在Ubuntu上安装LibRaw
    Kotlin难点
    Open3D RANSAC拟合圆(随机采样一致性)
  • 原文地址:https://blog.csdn.net/zjf535214685/article/details/134075279