prophet0
13th March 2012, 21:57
widgetmargueelabel.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);
~WidgetMarqueeLabel();
void show();
void setAlignment(Qt::Alignment);
int getSpeed();
public slots: //Public Member Slots
void setSpeed(int s);
void setDirection(int d);
protected: //Member Functions
void paintEvent(QPaintEvent *evt);
void resizeEvent(QResizeEvent *evt);
void updateCoordinates();
private: //Data Members
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_*/
widgetmargueelabel.cpp
#include "widgetmargueelabel.h"
#include <QPainter>
WidgetMarqueeLabel::WidgetMarqueeLabel(QWidget *parent)
{
px = 0;
py = 15;
speed = 1;
direction = RightToLeft;
connect(&timer, SIGNAL(timeout()), this, SLOT(refreshLabel()));
timer.start(40);
}
void WidgetMarqueeLabel::refreshLabel()
{
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);
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.translate(px,0);
}
void WidgetMarqueeLabel::resizeEvent(QResizeEvent *evt)
{
updateCoordinates();
QLabel::resizeEvent(evt);
}
void WidgetMarqueeLabel::updateCoordinates()
{
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());
}
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();
}
main.cpp
QStringList sClientIdData;
sClientIdData = _myParse->getClientID();
for ( QStringList::Iterator it = sClientIdData.begin(); it != sClientIdData.end(); ++it )
{
// debug shows both entries in the QStringList
}
/* Scrolling Text: Start */
ml = new WidgetMarqueeLabel(this);
ml->setTextFormat(Qt::RichText);
this->layout()->addWidget(ml);
ml->setAlignment(Qt::AlignVCenter);
ml->setText(/* STRING DATA HERE */);
ml->setFont(QFont("Arial", 20,20));
ml->setGeometry(80, 5, 731, 31);
ml->setStyleSheet("font: 16pt ""Trebuchet MS""; color: rgb(160, 160, 160)");
/* Scrolling Text: END */
What I'm trying to do is set the text of ml via a stringlist but 1 string at a time following the last character of the string so if the string list was "MyFirstString" and "MySecondString" once the Marguee label is DONE showing the last character of the string refresh the label with a new string and just keep looping it..
What is the best way to accomplish this task ?
please if you could point me in the right direction thanks a lot !
#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);
~WidgetMarqueeLabel();
void show();
void setAlignment(Qt::Alignment);
int getSpeed();
public slots: //Public Member Slots
void setSpeed(int s);
void setDirection(int d);
protected: //Member Functions
void paintEvent(QPaintEvent *evt);
void resizeEvent(QResizeEvent *evt);
void updateCoordinates();
private: //Data Members
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_*/
widgetmargueelabel.cpp
#include "widgetmargueelabel.h"
#include <QPainter>
WidgetMarqueeLabel::WidgetMarqueeLabel(QWidget *parent)
{
px = 0;
py = 15;
speed = 1;
direction = RightToLeft;
connect(&timer, SIGNAL(timeout()), this, SLOT(refreshLabel()));
timer.start(40);
}
void WidgetMarqueeLabel::refreshLabel()
{
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);
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.translate(px,0);
}
void WidgetMarqueeLabel::resizeEvent(QResizeEvent *evt)
{
updateCoordinates();
QLabel::resizeEvent(evt);
}
void WidgetMarqueeLabel::updateCoordinates()
{
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());
}
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();
}
main.cpp
QStringList sClientIdData;
sClientIdData = _myParse->getClientID();
for ( QStringList::Iterator it = sClientIdData.begin(); it != sClientIdData.end(); ++it )
{
// debug shows both entries in the QStringList
}
/* Scrolling Text: Start */
ml = new WidgetMarqueeLabel(this);
ml->setTextFormat(Qt::RichText);
this->layout()->addWidget(ml);
ml->setAlignment(Qt::AlignVCenter);
ml->setText(/* STRING DATA HERE */);
ml->setFont(QFont("Arial", 20,20));
ml->setGeometry(80, 5, 731, 31);
ml->setStyleSheet("font: 16pt ""Trebuchet MS""; color: rgb(160, 160, 160)");
/* Scrolling Text: END */
What I'm trying to do is set the text of ml via a stringlist but 1 string at a time following the last character of the string so if the string list was "MyFirstString" and "MySecondString" once the Marguee label is DONE showing the last character of the string refresh the label with a new string and just keep looping it..
What is the best way to accomplish this task ?
please if you could point me in the right direction thanks a lot !