Qt 中文跑马灯

news/2024/11/24 18:39:48/

直接上代码:
textticker.h

#ifndef TEXTTICKER_H
#define TEXTTICKER_H#include <QWidget>
#include <QLabel>
#include <QString>
#include <QTimer>
#include <QPen>
#include <QFont>
#include <QPainter>
#include <QDebug>class TextTicker : public QLabel
{Q_OBJECT
public:TextTicker(QWidget *parent = 0);~TextTicker();protected:void paintEvent(QPaintEvent *event);void updateIndex();private:int m_charWidth;int m_stringWidth ;int m_curIndex;int m_pos;int pointSize ;QString m_showText;};#endif // TEXTTICKER_H

textticker.cpp

#include "textticker.h"TextTicker::TextTicker(QWidget *parent):QLabel(parent)
{this->setFixedSize(1920,114);QFont ft;pointSize = 44;ft.setPointSize(pointSize);this->setFont(ft);QPalette pa;pa.setColor(QPalette::WindowText,Qt::white);this->setPalette(pa);m_curIndex = 0;//当前角码m_pos = 0 ;m_showText = QStringLiteral("欢迎使用Qt");;//显示的文字m_charWidth =fontMetrics().width("欢");//每个字符的宽度m_stringWidth = fontMetrics().width(QStringLiteral("欢迎使用Qt"));qDebug()<<m_charWidth<<"  "<<m_stringWidth;QTimer *timer = new QTimer(this);timer->setInterval(10);connect(timer, &QTimer::timeout, this, &TextTicker::updateIndex);timer->start();
}TextTicker::~TextTicker()
{}void TextTicker::paintEvent(QPaintEvent *event)
{QPen pen;pen.setColor(QColor(Qt::white));pen.setStyle(Qt::DashDotDotLine);int hPos = height()/2 + pointSize/2 ;QPainter painter(this);painter.setPen(pen);if(m_pos > m_stringWidth)painter.drawText(-(m_pos),hPos,m_showText);painter.drawText(width()-m_pos,hPos,m_showText.left(m_curIndex));
}void TextTicker::updateIndex()
{update();m_pos +=2 ;if(m_pos > width()+m_stringWidth)m_pos = 0 ;m_curIndex = m_pos/m_charWidth+1;
}

这份代码现在,不断的刷新绘制的时候,如果速度加快,会发现明显的抖动。


http://www.ppmy.cn/news/848001.html

相关文章

跑马灯(VB6代码)

Option Explicit 直接F5:) Private Const lDir As Long -1 方向,1与-1分别为右转与左转 Dim WithEvents Timer1 As TimerPrivate Sub Form_Load()用代码添加控件并设置控件的初始状态. 这样方便点,可以直接粘贴F5:)With Me.Controls.Add "VB.Label&quo…

文字跑马灯HTML代码解析

<table style"color:white; font-size:10pt" border"1" style"color:white; font-size:10pt" bordercolor"#FF9900"> <td background"图档网址.gif"> <p> <font color"#FFFF00" face"新…

跑马灯代码的解释

android:layopt_marginTop"10dp"(与上一行的间隔) android:singleine"true"(只能单行显示) 1、定义textView标签的4个属性&#xff1a; android:singleLine"true"//使其只能单行 android:ellipsize"marquee"//去掉省略号 android:foc…

51单片机跑马灯c语言,51单片机——跑马灯详解(示例代码)

#include #include intmain() { unsignedchar temp 0xFE;int num; //temp 转化为十进制数字 char j[20]; //二进制数字字符串 while(1){ numtemp; itoa(num,j,2); //十进制转二进制 itoa(待转,输出,进制) printf("%s\n",j); //输出转换后的二进制编码 temp (temp &…

寄存器跑马灯

之前看的库函数操作的LED灯&#xff0c;和平常调用读卡器无异。这次读下芯片文档&#xff0c;用空工程模板操作寄存器控制LED灯。 VID_20220322_205657 第一次接触寄存器操作&#xff0c;有点烧脑&#xff0c;烧脑过后感觉比库函数操作还清晰些。APB2ENR&#xff0c;CRL&#x…

代码 动态 设置 跑马灯

代码设置跑马灯&#xff0c;直接上代码 mTitleTv.setMarqueeRepeatLimit(Integer.MAX_VALUE);mTitleTv.setFocusable(true);mTitleTv.setEllipsize(TextUtils.TruncateAt.MARQUEE);mTitleTv.setSingleLine();mTitleTv.setFocusableInTouchMode(true);mTitleTv.setHorizontallyS…

跑马灯源代码

希望对小白有作用&#xff1a;首先献上资料&#xff1a; arqueeRepeatLimit属性是文字滚动起来http://xlover.iteye.com/blog/1443280 android:ellipsiz属性&#xff1a;省略文字http://www.ablanxue.com/prone_9786_1.html 自定义组件http://www.android100.org/html/201506/2…

html跑马灯走马灯效果

演示 <marquee width"100%" scrollamount"5"> <a href"http://www.taobaojp5.tk"><font face"楷体_GB2312" color"#ff0000" size"3"></font><strong>带有超链接的跑马灯!点我试试&…