Qt QLabel中的文本滚动(选框)
我在学习课程:Qt QLabel中的文本滚动(选框),qt,text,scroll,label,marquee,Qt,Text,Scroll,Label,Marquee,我在学习课程: 这是可能的吗?类似于以下的方法应该可以工作。填充是硬编码为25,这听起来像是你想要的。如果希望标签始终保持一定的大小,可以使用类似QString::leftjustized的内容 class MarqueeLabel : public QLabel { public: explicit MarqueeLabel(const QString &text) : QLabel(text), pos_(0) { QString pad(25, ' '); a
这是可能的吗?类似于以下的方法应该可以工作。填充是硬编码为25,这听起来像是你想要的。如果希望标签始终保持一定的大小,可以使用类似
QString::leftjustized
的内容
class MarqueeLabel : public QLabel {
public:
explicit MarqueeLabel(const QString &text) : QLabel(text), pos_(0) {
QString pad(25, ' ');
actual_text_ = text + pad;
startTimer(100);
}
protected:
void timerEvent(QTimerEvent *) {
pos_ = ++pos_ % actual_text_.length();
setText(actual_text_.mid(pos_).append(actual_text_.left(pos_)));
}
private:
QString actual_text_;
int pos_;
};
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MarqueeLabel lbl("WidgetMarqueeLabel");
lbl.show();
return a.exec();
}
真的很容易。只需重新绘制按控件宽度替换的文本:
void WidgetMarqueeLabel::paintEvent(QPaintEvent *evt)
{
QPainter p(this);
if(direction==RightToLeft)
{
px -= speed;
if(px <= (-textLength))
px = width();
}
else
{
px += speed;
if(px >= width())
px = - textLength;
}
p.drawText(px, py+fontPointSize, text());
__p.drawText(px-width(), py+fontPointSize, text());
p.drawText(px+width(), py+fontPointSize, text());
p.translate(px,0);
}
void WidgetMarqueeLabel::paintEvent(QPaintEvent*evt)
{
油漆工p(本);
如果(方向==右至左)
{
px-=速度;
如果(px=width())
px=-textLength;
}
p、 drawText(px,py+fontPointSize,text());
__p、 drawText(px-width(),py+fontPointSize,text());
p、 drawText(px+width(),py+fontPointSize,text());
p、 翻译(px,0);
}
为此,我曾经编写了一个类
显示文本“这是一个示例文本。它将水平滚动”的示例屏幕截图。注意两侧的alpha混合 守则: scrolltext.h:
#ifndef SCROLLTEXT_H
#define SCROLLTEXT_H
#include <QWidget>
#include <QStaticText>
#include <QTimer>
class ScrollText : public QWidget
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(QString separator READ separator WRITE setSeparator)
public:
explicit ScrollText(QWidget *parent = 0);
public slots:
QString text() const;
void setText(QString text);
QString separator() const;
void setSeparator(QString separator);
protected:
virtual void paintEvent(QPaintEvent *);
virtual void resizeEvent(QResizeEvent *);
private:
void updateText();
QString _text;
QString _separator;
QStaticText staticText;
int singleTextWidth;
QSize wholeTextSize;
int leftMargin;
bool scrollEnabled;
int scrollPos;
QImage alphaChannel;
QImage buffer;
QTimer timer;
private slots:
virtual void timer_timeout();
};
#endif // SCROLLTEXT_H
#include "scrolltext.h"
#include <QPainter>
ScrollText::ScrollText(QWidget *parent) :
QWidget(parent), scrollPos(0)
{
staticText.setTextFormat(Qt::PlainText);
setFixedHeight(fontMetrics().height());
leftMargin = height() / 3;
setSeparator(" --- ");
connect(&timer, SIGNAL(timeout()), this, SLOT(timer_timeout()));
timer.setInterval(50);
}
QString ScrollText::text() const
{
return _text;
}
void ScrollText::setText(QString text)
{
_text = text;
updateText();
update();
}
QString ScrollText::separator() const
{
return _separator;
}
void ScrollText::setSeparator(QString separator)
{
_separator = separator;
updateText();
update();
}
void ScrollText::updateText()
{
timer.stop();
singleTextWidth = fontMetrics().width(_text);
scrollEnabled = (singleTextWidth > width() - leftMargin);
if(scrollEnabled)
{
scrollPos = -64;
staticText.setText(_text + _separator);
timer.start();
}
else
staticText.setText(_text);
staticText.prepare(QTransform(), font());
wholeTextSize = QSize(fontMetrics().width(staticText.text()), fontMetrics().height());
}
void ScrollText::paintEvent(QPaintEvent*)
{
QPainter p(this);
if(scrollEnabled)
{
buffer.fill(qRgba(0, 0, 0, 0));
QPainter pb(&buffer);
pb.setPen(p.pen());
pb.setFont(p.font());
int x = qMin(-scrollPos, 0) + leftMargin;
while(x < width())
{
pb.drawStaticText(QPointF(x, (height() - wholeTextSize.height()) / 2) + QPoint(2, 2), staticText);
x += wholeTextSize.width();
}
//Apply Alpha Channel
pb.setCompositionMode(QPainter::CompositionMode_DestinationIn);
pb.setClipRect(width() - 15, 0, 15, height());
pb.drawImage(0, 0, alphaChannel);
pb.setClipRect(0, 0, 15, height());
//initial situation: don't apply alpha channel in the left half of the image at all; apply it more and more until scrollPos gets positive
if(scrollPos < 0)
pb.setOpacity((qreal)(qMax(-8, scrollPos) + 8) / 8.0);
pb.drawImage(0, 0, alphaChannel);
//pb.end();
p.drawImage(0, 0, buffer);
}
else
{
p.drawStaticText(QPointF(leftMargin, (height() - wholeTextSize.height()) / 2), staticText);
}
}
void ScrollText::resizeEvent(QResizeEvent*)
{
//When the widget is resized, we need to update the alpha channel.
alphaChannel = QImage(size(), QImage::Format_ARGB32_Premultiplied);
buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
//Create Alpha Channel:
if(width() > 64)
{
//create first scanline
QRgb* scanline1 = (QRgb*)alphaChannel.scanLine(0);
for(int x = 1; x < 16; ++x)
scanline1[x - 1] = scanline1[width() - x] = qRgba(0, 0, 0, x << 4);
for(int x = 15; x < width() - 15; ++x)
scanline1[x] = qRgb(0, 0, 0);
//copy scanline to the other ones
for(int y = 1; y < height(); ++y)
memcpy(alphaChannel.scanLine(y), (uchar*)scanline1, width() * 4);
}
else
alphaChannel.fill(qRgb(0, 0, 0));
//Update scrolling state
bool newScrollEnabled = (singleTextWidth > width() - leftMargin);
if(newScrollEnabled != scrollEnabled)
updateText();
}
void ScrollText::timer_timeout()
{
scrollPos = (scrollPos + 2)
% wholeTextSize.width();
update();
}
\ifndef SCROLLTEXT\u H
#定义滚动文本
#包括
#包括
#包括
类ScrollText:公共QWidget
{
Q_对象
Q_属性(QString text READ text WRITE setText)
Q_属性(QString separator读取separator写入separator设置separator)
公众:
显式滚动文本(QWidget*parent=0);
公众时段:
QString text()常量;
void setText(QString文本);
QString分隔符()常量;
空隙设置分离器(QString分离器);
受保护的:
虚拟虚空paintEvent(QPaintEvent*);
虚拟void resizeEvent(QResizeEvent*);
私人:
void updateText();
QString_文本;
QString_分离器;
静态文本;
int单文本宽度;
QSize-wholetexsize;
int左边距;
布尔滚动启用;
int-scrollPos;
QImage字母通道;
QImage缓冲区;
定时器;
专用插槽:
虚拟无效计时器_超时();
};
#endif//滚动文本
scrolltext.cpp:
#ifndef SCROLLTEXT_H
#define SCROLLTEXT_H
#include <QWidget>
#include <QStaticText>
#include <QTimer>
class ScrollText : public QWidget
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(QString separator READ separator WRITE setSeparator)
public:
explicit ScrollText(QWidget *parent = 0);
public slots:
QString text() const;
void setText(QString text);
QString separator() const;
void setSeparator(QString separator);
protected:
virtual void paintEvent(QPaintEvent *);
virtual void resizeEvent(QResizeEvent *);
private:
void updateText();
QString _text;
QString _separator;
QStaticText staticText;
int singleTextWidth;
QSize wholeTextSize;
int leftMargin;
bool scrollEnabled;
int scrollPos;
QImage alphaChannel;
QImage buffer;
QTimer timer;
private slots:
virtual void timer_timeout();
};
#endif // SCROLLTEXT_H
#include "scrolltext.h"
#include <QPainter>
ScrollText::ScrollText(QWidget *parent) :
QWidget(parent), scrollPos(0)
{
staticText.setTextFormat(Qt::PlainText);
setFixedHeight(fontMetrics().height());
leftMargin = height() / 3;
setSeparator(" --- ");
connect(&timer, SIGNAL(timeout()), this, SLOT(timer_timeout()));
timer.setInterval(50);
}
QString ScrollText::text() const
{
return _text;
}
void ScrollText::setText(QString text)
{
_text = text;
updateText();
update();
}
QString ScrollText::separator() const
{
return _separator;
}
void ScrollText::setSeparator(QString separator)
{
_separator = separator;
updateText();
update();
}
void ScrollText::updateText()
{
timer.stop();
singleTextWidth = fontMetrics().width(_text);
scrollEnabled = (singleTextWidth > width() - leftMargin);
if(scrollEnabled)
{
scrollPos = -64;
staticText.setText(_text + _separator);
timer.start();
}
else
staticText.setText(_text);
staticText.prepare(QTransform(), font());
wholeTextSize = QSize(fontMetrics().width(staticText.text()), fontMetrics().height());
}
void ScrollText::paintEvent(QPaintEvent*)
{
QPainter p(this);
if(scrollEnabled)
{
buffer.fill(qRgba(0, 0, 0, 0));
QPainter pb(&buffer);
pb.setPen(p.pen());
pb.setFont(p.font());
int x = qMin(-scrollPos, 0) + leftMargin;
while(x < width())
{
pb.drawStaticText(QPointF(x, (height() - wholeTextSize.height()) / 2) + QPoint(2, 2), staticText);
x += wholeTextSize.width();
}
//Apply Alpha Channel
pb.setCompositionMode(QPainter::CompositionMode_DestinationIn);
pb.setClipRect(width() - 15, 0, 15, height());
pb.drawImage(0, 0, alphaChannel);
pb.setClipRect(0, 0, 15, height());
//initial situation: don't apply alpha channel in the left half of the image at all; apply it more and more until scrollPos gets positive
if(scrollPos < 0)
pb.setOpacity((qreal)(qMax(-8, scrollPos) + 8) / 8.0);
pb.drawImage(0, 0, alphaChannel);
//pb.end();
p.drawImage(0, 0, buffer);
}
else
{
p.drawStaticText(QPointF(leftMargin, (height() - wholeTextSize.height()) / 2), staticText);
}
}
void ScrollText::resizeEvent(QResizeEvent*)
{
//When the widget is resized, we need to update the alpha channel.
alphaChannel = QImage(size(), QImage::Format_ARGB32_Premultiplied);
buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
//Create Alpha Channel:
if(width() > 64)
{
//create first scanline
QRgb* scanline1 = (QRgb*)alphaChannel.scanLine(0);
for(int x = 1; x < 16; ++x)
scanline1[x - 1] = scanline1[width() - x] = qRgba(0, 0, 0, x << 4);
for(int x = 15; x < width() - 15; ++x)
scanline1[x] = qRgb(0, 0, 0);
//copy scanline to the other ones
for(int y = 1; y < height(); ++y)
memcpy(alphaChannel.scanLine(y), (uchar*)scanline1, width() * 4);
}
else
alphaChannel.fill(qRgb(0, 0, 0));
//Update scrolling state
bool newScrollEnabled = (singleTextWidth > width() - leftMargin);
if(newScrollEnabled != scrollEnabled)
updateText();
}
void ScrollText::timer_timeout()
{
scrollPos = (scrollPos + 2)
% wholeTextSize.width();
update();
}
#包括“scrolltext.h”
#包括
ScrollText::ScrollText(QWidget*父项):
QWidget(父项),scrollPos(0)
{
setTextFormat(Qt::明文);
setFixedHeight(fontMetrics().height());
leftMargin=高度()/3;
设置分隔符(“--”);
连接(&计时器,信号(超时()),此,插槽(计时器超时());
定时器设置间隔(50);
}
QString ScrollText::text()常量
{
返回文本;
}
void ScrollText::setText(QString文本)
{
_文本=文本;
updateText();
更新();
}
QString ScrollText::separator()常量
{
回流分离器;
}
无效滚动文本::设置分隔符(QString分隔符)
{
_分离器=分离器;
updateText();
更新();
}
void ScrollText::updateText()
{
timer.stop();
singleTextWidth=fontMetrics().width(_text);
scrollEnabled=(singleTextWidth>width()-leftMargin);
如果(已启用滚动)
{
scrollPos=-64;
staticText.setText(_text+_分隔符);
timer.start();
}
其他的
staticText.setText(_text);
staticText.prepare(QTransform(),font());
wholeTextSize=QSize(fontMetrics().width(staticText.text()),fontMetrics().height());
}
void ScrollText::paintEvent(QPaintEvent*)
{
油漆工p(本);
如果(已启用滚动)
{
缓冲区填充(qRgba(0,0,0,0));
QPainter pb(和缓冲区);
pb.setPen(p.pen());
pb.setFont(p.font());
intx=qMin(-scrollPos,0)+左边距;
而(x64)
{
//创建第一条扫描线
QRgb*扫描线1=(QRgb*)字母通道扫描线(0);
对于(int x=1;x<16;++x)
scanline1[x-1]=scanline1[width()-x]=qRgba(0,0,0,x width()-leftMargin);
如果(newScrollEnabled!=已启用滚动)
updateText();
}
无效滚动文本::计时器超时()
{
scrollPos=(scrollPos+2)
%wholetexsize.width();
更新();
}
我假设您示例输出中的第二行是错误的——它似乎不符合模式。干得好,我正在使用您的类在QvideoWidget上绘制文本,我有两个问题,我无法将文本颜色更改为白色,也无法使滚动文本背景不可见,它显示一个黑框。我可以更改文本颜色现在变了,但是小部件没有正确地更新自身,当它在视频前面使用时,它不会擦除以前的绘图widget@fredcrs很抱歉,我不知道如何解决这个问题。这是一个普通的问题,在另一个小部件上有一个透明的动画小部件。我猜下面的小部件必须更新注册表为了解决这个问题,我必须使用黑色背景,我认为QVideoWidget没有重新绘制自己,也没有使用repaint()方法重新绘制自己。无论如何,谢谢!非常好的cde!O