I changed WidgetMarqueeLabel.h and.cpp to the following.
#ifndef _WIDGETMARQUEELABEL_H_
#define _WIDGETMARQUEELABEL_H_
#include <QLabel>
#include <QTimer>
class WidgetMarqueeLabel
: public QLabel{
Q_OBJECT
public: //Member Functions
enum Direction{LeftToRight,RightToLeft};
WidgetMarqueeLabel
(QWidget *parent
= 0,
int spd
=50,
int dir
=WidgetMarqueeLabel
::RightToLeft,
bool start
=FALSE);
~WidgetMarqueeLabel();
void show();
void setAlignment(Qt::Alignment);
int getSpeed();
public slots: //Public Member Slots
void setSpeed(int s);
void setDirection(int d);
void updateCoordinates();
protected: //Member Functions
private: //Data Members
bool autostart;
int px;
int py;
Qt::Alignment m_align;
int speed;
int direction;
int fontPointSize;
int textLength;
private slots: //Private Member Slots
void refreshLabel();
};
#endif /*_WIDGETMARQUEELABEL_H_*/
#ifndef _WIDGETMARQUEELABEL_H_
#define _WIDGETMARQUEELABEL_H_
#include <QLabel>
#include <QTimer>
class WidgetMarqueeLabel : public QLabel
{
Q_OBJECT
public: //Member Functions
enum Direction{LeftToRight,RightToLeft};
WidgetMarqueeLabel(QWidget *parent = 0,int spd=50,int dir=WidgetMarqueeLabel::RightToLeft,bool start=FALSE);
~WidgetMarqueeLabel();
void show();
void setAlignment(Qt::Alignment);
int getSpeed();
public slots: //Public Member Slots
void setSpeed(int s);
void setDirection(int d);
void updateCoordinates();
protected: //Member Functions
void paintEvent(QPaintEvent *evt);
void resizeEvent(QResizeEvent *evt);
private: //Data Members
bool autostart;
int px;
int py;
QTimer timer;
Qt::Alignment m_align;
int speed;
int direction;
int fontPointSize;
int textLength;
private slots: //Private Member Slots
void refreshLabel();
};
#endif /*_WIDGETMARQUEELABEL_H_*/
To copy to clipboard, switch view to plain text mode
#include "WidgetMarqueeLabel.h"
#include <QPainter>
#include <QDebug>
WidgetMarqueeLabel
::WidgetMarqueeLabel(QWidget *parent,
int spd,
int dir,
bool start
){
px = 0;
py = 15;
speed = spd;
direction = dir;
autostart=start;
connect(&timer, SIGNAL(timeout()), this, SLOT(refreshLabel()));
if (autostart==FALSE){
if (fontMetrics().width(text())> width())
timer.start(10);
}
else
timer.start(10);
}
void WidgetMarqueeLabel::refreshLabel()
{
if(direction==RightToLeft)
{
px -=1;
if(px <= (-textLength))
px = width();
}
else
{
px +=1;
if(px >= width())
px = - textLength;
}
timer.start(speed);
repaint();
}
WidgetMarqueeLabel::~WidgetMarqueeLabel()
{}
void WidgetMarqueeLabel::show()
{
}
void WidgetMarqueeLabel::setAlignment(Qt::Alignment al)
{
m_align = al;
updateCoordinates();
}
{
p.drawText(px, py + fontPointSize, text());
p.translate(px,0);
}
{
updateCoordinates();
}
void WidgetMarqueeLabel::updateCoordinates()
{
px=0;
switch(m_align)
{
case Qt::AlignTop:
py = 10;
break;
case Qt::AlignBottom:
py = height()-10;
break;
case Qt::AlignVCenter:
py = height()/2;
break;
}
fontPointSize = font().pointSize()/2;
textLength = fontMetrics().width(text());
if (textLength>width())
timer.start(10);
else{
timer.stop();
repaint();
}
}
void WidgetMarqueeLabel::setSpeed(int s)
{
speed = s;
}
int WidgetMarqueeLabel::getSpeed()
{
return speed;
}
void WidgetMarqueeLabel::setDirection(int d)
{
direction = d;
if (direction==RightToLeft){
px = width() - textLength;
}
else
px = 0;
refreshLabel();
}
#include "WidgetMarqueeLabel.h"
#include <QPainter>
#include <QDebug>
WidgetMarqueeLabel::WidgetMarqueeLabel(QWidget *parent,int spd,int dir,bool start)
:QLabel(parent)
{
px = 0;
py = 15;
speed = spd;
direction = dir;
autostart=start;
connect(&timer, SIGNAL(timeout()), this, SLOT(refreshLabel()));
if (autostart==FALSE){
if (fontMetrics().width(text())> width())
timer.start(10);
}
else
timer.start(10);
}
void WidgetMarqueeLabel::refreshLabel()
{
if(direction==RightToLeft)
{
px -=1;
if(px <= (-textLength))
px = width();
}
else
{
px +=1;
if(px >= width())
px = - textLength;
}
timer.start(speed);
repaint();
}
WidgetMarqueeLabel::~WidgetMarqueeLabel()
{}
void WidgetMarqueeLabel::show()
{
QLabel::show();
}
void WidgetMarqueeLabel::setAlignment(Qt::Alignment al)
{
m_align = al;
updateCoordinates();
QLabel::setAlignment(al);
}
void WidgetMarqueeLabel::paintEvent(QPaintEvent *evt)
{
QPainter p(this);
p.drawText(px, py + fontPointSize, text());
p.translate(px,0);
}
void WidgetMarqueeLabel::resizeEvent(QResizeEvent *evt)
{
updateCoordinates();
QLabel::resizeEvent(evt);
}
void WidgetMarqueeLabel::updateCoordinates()
{
px=0;
switch(m_align)
{
case Qt::AlignTop:
py = 10;
break;
case Qt::AlignBottom:
py = height()-10;
break;
case Qt::AlignVCenter:
py = height()/2;
break;
}
fontPointSize = font().pointSize()/2;
textLength = fontMetrics().width(text());
if (textLength>width())
timer.start(10);
else{
timer.stop();
repaint();
}
}
void WidgetMarqueeLabel::setSpeed(int s)
{
speed = s;
}
int WidgetMarqueeLabel::getSpeed()
{
return speed;
}
void WidgetMarqueeLabel::setDirection(int d)
{
direction = d;
if (direction==RightToLeft){
px = width() - textLength;
}
else
px = 0;
refreshLabel();
}
To copy to clipboard, switch view to plain text mode
I changed a few things, including the paintEvent function. The scroll was too fast the way it was, so I made sure it was setup to the timer
that they had instead of just the paintEvent. I also added a few features, like only scrolling if the text amount is larger than the space.
If the text changes in my MainWindow.cpp I emit a SIGNAL songChanged and connect it to WidgetMarqueeLabel's SLOT updateCoordinates;
here is a snippet from my code:
song=new WidgetMarqueeLabel(ui->centralWidget,1);
song
->setObjectName
(QString::fromUtf8("song"));
song
->setGeometry
(QRect(60,
10,
281,
21));
connect(this,SIGNAL(songChanged()),song,SLOT(updateCoordinates()));
song->setText("hello");
song->setSpeed(100);
void MainWindow::onSourceChanged(Phonon::MediaSource source)
{
QMultiMap<QString,QString> tags=getTags(source);
if(tags.value("TITLE").isEmpty())
file=source.fileName().mid(source.fileName().lastIndexOf("/")+1,source.fileName().size());
else
file=tags.value("ARTIST")+"-"+tags.value("TITLE");
song->setText(file);
emit songChanged();
global_index=sources.indexOf(source);
populateList();
}
song=new WidgetMarqueeLabel(ui->centralWidget,1);
song->setObjectName(QString::fromUtf8("song"));
song->setGeometry(QRect(60, 10, 281, 21));
connect(this,SIGNAL(songChanged()),song,SLOT(updateCoordinates()));
song->setText("hello");
song->setSpeed(100);
void MainWindow::onSourceChanged(Phonon::MediaSource source)
{
QMultiMap<QString,QString> tags=getTags(source);
QString file;
if(tags.value("TITLE").isEmpty())
file=source.fileName().mid(source.fileName().lastIndexOf("/")+1,source.fileName().size());
else
file=tags.value("ARTIST")+"-"+tags.value("TITLE");
song->setText(file);
emit songChanged();
global_index=sources.indexOf(source);
populateList();
}
To copy to clipboard, switch view to plain text mode
There isn't any reason to include 1 as a speed in my instantiation of song but at least you can see me setting the speed.
take a look at the examples in the zip file.
I tried to make a plugin for this to Designer. I was able to get Designer to display the widget, but as soon as I went to click on it it would crash Designer/Qt Creator. So I'm stuck with programatically adding a WidgetMarqueeLabel. If you have any info on this feel free to share cw9000@hotmail.com
Bookmarks