PDA

View Full Version : dynamic QLabel update



satanowicz
24th May 2010, 10:27
hi,

I've got a parent QLabel with some graphics (setPixmap) and a child QLabel (with some text)
I'm looking for a way to update the child QLabel text and show the change right after it's done
already tried label->setText but nothing seems to happen
what do I have to do to "refresh the view"?

tbscope
24th May 2010, 10:29
Can you please post your code?

satanowicz
24th May 2010, 10:45
this is an attempt to do it after a button click (nothing happens)

Explorer::Explorer()
{
imageLabel = new QLabel;
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixe d);
imageLabel->setWindowFlags(Qt::FramelessWindowHint);
imageLabel->setFrameStyle(QFrame::NoFrame);


setCentralWidget(imageLabel);

setWindowTitle(tr(name));


QLabel *test = new QLabel("<h1><font color=white>222</font></h1>", imageLabel);
test->move(7,80);

QPushButton *button = new QPushButton("test", imageLabel);
button->setFlat(true);
QObject::connect(button, SIGNAL(clicked()),
this, SLOT(updateHP()));

}

void Explorer::open(char *file)
{
QString fileName = file;
if (!fileName.isEmpty()) {
QImage image(fileName);
if (image.isNull()) {
QMessageBox::information(this, tr("Error"),
tr("Cannot load %1.").arg(fileName));
return;
}
imageLabel->setPixmap(QPixmap::fromImage(image));

}

}

void Explorer::updateHP()
{
QMessageBox::information(this, tr("Update HP"),
tr("HP update button clicked"));
test->setText("<h1><font color=white>444</font></h1>");


}

tbscope
24th May 2010, 10:51
You're very lucky that you didn't get a segmentation fault, although, I guess it's better to get one instead of none.

This is what you wrote in your program:

Explorer::Explorer()
{
...
QLabel *test = new QLabel(...);
....
}

The QLabel object named test is ONLY available in the scope of your constructor.
You did it correctly with imageLabel.

Thus, your solution is: define your QLabel named test in your class definition, like you probably did with imageLabel.
Then you can use test in the other functions of your class.

satanowicz
24th May 2010, 10:54
I had a QLabel named test all the time

sorry for that, this is my header file:

#ifndef EXPLORER_H
#define EXPLORER_H

#include <QMainWindow>
#include <QPrinter>

class QAction;
class QLabel;

class Explorer : public QMainWindow
{
Q_OBJECT

public:
Explorer(char *name);
void open(char *fileName);
void print();
QLabel *test;
QLabel *imageLabel;

void updateLife(QWidget *life);
void updateMana(QWidget *mana);
void updateGold(QWidget *gold);

public slots:
void updateHP();



private:
char *hitPoints;
QPrinter printer;
void createActions();
void updateExplorer();


};

#endif

tbscope
24th May 2010, 10:59
My last comment is still valid!

You created a local object test that is only available in your constructor.

Write this instead:

Explorer::Explorer()
{
...
test = new QLabel(...);
....
}

satanowicz
24th May 2010, 11:05
it came to me as soon i wrote the answer- sorry ;)
thx for the help

SixDegrees
24th May 2010, 15:14
Yes, but the way your code is currently written, the class member 'test' will never be initialized. The member variable 'test' will be initialized, then destroyed at the end of your constructor. Get rid of the declaration statement "QLabel*" in front of test in the constructor.

satanowicz
25th May 2010, 23:20
ok, another little problem with dynamic QLabels and buttons:

void Explorer::setEquipment() {

QPushButton *items = new QPushButton("Friends", mainLabel);
QObject::connect(items, SIGNAL(clicked()),
mainLabel, SLOT(showFriends()));
items-> move(47,277);
items-> setStyleSheet("color: white; font-family:Verdana, Sans-serif; font-weight:bold; border: none;");

}

void Explorer::showFriends() {

QMessageBox msgBox;
msgBox.setText("Friends label");
msgBox.exec();
friendsLabel = new QLabel("TEST");
}


class Explorer : public QMainWindow
{
Q_OBJECT

public:
Explorer(char *name);
void mainWindow();
void open(char *fileName);
QLabel *mainLabel;
QLabel *friendsLabel;
void setEquipment();

private slots:
void showFriends();



};

the new QLabel doesn't appear after clicking
BTW How to debug this kind of problems?

EDIT:
ok, all what was needed was:

friendsLabel->show();