QT scroll text for a GUI project
-
Hello.
I'm trying to create a project that shows in a window an image and text, to show those objects in a LED panel. My LED panel has the size of 48x80 pixels or 80x48, it depends in the position we put it.
My question is that we have to show text that we can see it setting the size of 13 for one of the font we've selected. The problem is that deppending on the length of the text we can't see all the letters. To show short words we haven't got a problem but when we put larger words or a phrase, we have problems because we can't see more than words that fit on the panel.
We did a demo that uses a Qlabel, but as I've commented, I don't know how to scroll the text if it doesn't fit and know we have to plain it better. Maybe the solution is use different class but I don't know which is the best because I'm new in programming in Qt.
Do you know a class or how to solve this problem?
-
Hi,
Did you take a look at the QTextBrowser? It has automatic scrollbars when needed. (offcourse settable in software). -
Looks like you could use code from the following thread:
http://qt-project.org/forums/viewthread/43379/
Go for the animations version as the animators are lagging. -
Finally I used this code I obtained from: http://stackoverflow.com/questions/10651514/text-scrolling-marquee-in-qlabel
*MarqueeLabel.h
*
@#ifndef MARQUEELABEL_H
#define MARQUEELABEL_H#include <QLabel>
#include <QTimer>
//#include <QtDesigner/QDesignerExportWidget>//class QDESIGNER_WIDGET_EXPORT MarqueeLabel : public QLabel
class MarqueeLabel : public QLabel
{
Q_OBJECTpublic: //Member Functions
MarqueeLabel(QWidget *parent = 0);
~MarqueeLabel();
void show();
void setAlignment(Qt::Alignment);
//int getSpeed();
float getSpeed();public slots: //Public Member Slots
//void setSpeed(int s);
void setSpeed(float s);protected: //Member Functions
void paintEvent(QPaintEvent *evt);
void resizeEvent(QResizeEvent *evt);
void updateCoordinates();private: //Data Members
//int px;
float px;
//int py;
float py;
QTimer timer;
Qt::Alignment m_align;
//int speed;
float speed;
//int fontPointSize;
float fontPointSize;
//int textLength;
float textLength;private slots: //Private Member Slots
void refreshLabel();
};#endif // MARQUEELABEL_H@
*MarqueeLabel.cpp
*
@#include "MarqueeLabel.h"
#include <QPainter>MarqueeLabel::MarqueeLabel(QWidget *parent)
{
px = 0;
py = 15;
speed = 2;
connect(&timer, SIGNAL(timeout()), this, SLOT(refreshLabel()));
timer.start(10);
}void MarqueeLabel::refreshLabel()
{
repaint();
}MarqueeLabel::~MarqueeLabel()
{
}void MarqueeLabel::show()
{
QLabel::show();
}void MarqueeLabel::setAlignment(Qt::Alignment al)
{
m_align = al;
updateCoordinates();
QLabel::setAlignment(al);
}void MarqueeLabel::paintEvent(QPaintEvent evt)
{
/
* FROM RIGHT TO LEFT
*/
QPainter p(this);
px -= speed;
if(px <= (-textLength))
{
px = width();
}
p.drawText(px, py+fontPointSize, text()); //DESPLAZA Y RECARGA EL TEXTO
//p.drawText(px+width(), py+fontPointSize, text()); //DESPLAZA PERO NO RECARGA EL TEXTO
//p.drawText(px-width(), py+fontPointSize, text()); //NO MUESTRA NADA
p.translate(px,0);/*
//CHANGES DIRECTION OF THE TEXT
QPainter p(this);
px -= speed;
if(px <= (-textLength))
{
px = height(); // LOADS FASTER THAN px = width();
}
p.rotate(-90);
//p.drawText(px, py+fontPointSize, text()); //DESPLAZA Y RECARGA EL TEXTO
p.drawText(px, py+fontPointSize, text()); //DESPLAZA Y RECARGA EL TEXTO
p.translate(px,0);
*/QPainter p(this); px -= speed; if(px <= (-textLength)) { //px = width(); px = height(); //LA RECARGA DEL TEXTO ES MAS RAPIDA CUANDO FINALIZA EL SCROLL EN VEZ DE px = width(); } p.rotate(-90); p.drawText(px, py+fontPointSize, text()); //DESPLAZA Y RECARGA EL TEXTO p.translate(px,0);
}
void MarqueeLabel::resizeEvent(QResizeEvent *evt)
{
updateCoordinates();
QLabel::resizeEvent(evt);
}void MarqueeLabel::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 MarqueeLabel::setSpeed(int s)
void MarqueeLabel::setSpeed(float s)
{
speed = s;
}//int MarqueeLabel::getSpeed()
float MarqueeLabel::getSpeed()
{
return speed;
}
@*main.cpp
*
@#include <QApplication>
#include "mainwindow.h"#include "MarqueeLabel.h" //PARA UTILIZAR EL TEXTO EN MOVIMIENTO.
#include <QFontMetrics> //CALCULA VALORES PARA LAS FUENTES UTILIZADAS, alturas
#include <qdebug.h> //PARA PODER UTILZIAR qDebug();#define VELOCIDAD_DE_SCROLL 0.25
int main(int argc, char *argv[])
{
QApplication app(argc, argv);MarqueeLabel lbl; QFont f("Verdana", 20, 15); //Con un tamaño de fuente de 30, ocupamos de alto 48 pixeles, el tope del panel en posicion horizontal //Con un tamaño de fuente de 20, ocupamos de alto 32 pixeles, el tope del panel en posicion vertical, para la parte 3x2 QFontMetrics prop_font(f); int altura_font = prop_font.height(); int anchura_font = prop_font.width("PROXIMA SALIDA A 2KM POR ACCIDENTE"); qDebug() << "Altura fuente: " << altura_font << ""; qDebug() << "Anchura fuente: " << anchura_font << ""; lbl.setFont(f); lbl.setSpeed(VELOCIDAD_DE_SCROLL); lbl.setAlignment(Qt::AlignLeft); //la alineacion Horizontal es la mas adecuada para que el texto se muestre verticalmente //lbl.setAlignment(Qt::AlignVCenter); //lbl.setWindowFlags(Qt::CustomizeWindowHint); lbl.setText("PROXIMA SALIDA"); lbl.show(); app.setQuitOnLastWindowClosed(true); return app.exec();
}@
In this moment I have for my LED panel the scroll text in a vertical position working correctly. I need that the text scrolls vertically, but I don't know how to change it. As you can see in the text, if I use rotate(-90) method, it changes all the orientation of the all the QPainter, but I only need to change the words of the QPainter, something like in C:
@#include <stdio.h>
#include <string.h>
#include <unistd.h> //it allows to use usleepchar string_to_scroll[256] = "THIS IS THE STRING WE WANT TO SCROLL";
int size = strlen(string_to_scroll);
int i = 0;int main (void)
{
while(1)
{
for(i = 0; i < size; i++)
{
printf("\t%c\n", string_to_scroll[size - i]);
}if(i == size-1)
{
printf("\t \n");
}usleep(125000);
}
return 0;
}@Any ideas about how to implement something like this in Qt, using the MarqueeLabel class?