PDA

View Full Version : QLabel in QStatusBar does freaky stuff



dsab123
21st June 2012, 03:15
I have a QStatusBar at the bottom of my gui, and added a QProgressBar and a QLabel to it; I have a counter that increments the QProgressBar's value every second, and while that's going on, I'm setting the QLabel text to a string that confirms the user that a file was saved.

SO the progress bar kept getting bigger and bigger after each iteration, and started eating up the QLabel; so I set a fized width for it. Then I realized that somehow the QLabel's text kept moving one character over to the left (towards the progress bar), and eventually other strings I defined in completely different places, like inside of dialog boxes in other methods, started popping up in the QLabel..I'm not sure how else to describe it.
I'm only setting the text of the QLabel in one place.

Am I doing something wrong here? Or is there something else I'm possibly forgetting?

Thanks!



//in header file:
QStatusBar *stat;
QProgressBar *bar;
QLabel *barStatus;

//in constructor:
barStatus = new QLabel ( tr ("Ready" ) );
bar = new QProgressBar(this);
bar -> setFixedWidth(175);

stat = new QStatusBar(this);
stat -> addWidget(bar, Qt::AlignLeft);
stat -> addWidget(barStatus, Qt::AlignRight);

//in other method that updates counter
bar -> setValue(counter);
barStatus -> clear();
barStatus -> setText("Saved /home/../output" + counter);

Jonny174
21st June 2012, 05:49
stat = new QStatusBar( this );

barStatus = new QLabel ( tr ("Ready" ), stat );

bar = new QProgressBar( stat );
bar->setOrientation( Qt::Horizontal );
bar->setAlignment( Qt::AlignAbsolute );
bar->setFixedWidth( 175 );

stat->addWidget( barStatus, 1 );
stat->addPermanentWidget( bar );
stat->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Minimum );

ChrisW67
21st June 2012, 07:26
There is no QStatusBar::addWidget() that takes an alignment as its second argument. You are giving the widgets stretch factors of 1 and 2.

Have you set an upper/lower bound on the progress bar?

If I add the widgets without a stretch factor (default 0, minimum size allocated) then I get a transient glitch when the progress bar reaches its maximum value that disappears when the value is reset. This is presumably a glitch in the progress bar's size hints. If I give both widgets a non-zero stretch the glitch goes away.


#include <QtGui>
#include <QDebug>

class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
resize(320, 240);
barLabel = new QLabel("Ready", this);
barProgress = new QProgressBar(this);
barProgress->setMinimum(0);
barProgress->setMaximum(99);
statusBar()->addWidget(barProgress, 1); // or statusBar()->addWidget(barProgress)
statusBar()->addWidget(barLabel, 1); // or statusBar()->addWidget(barLabel)

value = 0;
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(tick()));
timer->start(50);
}
public slots:
void tick() {
value = (value + 1) % 100;
barLabel->setText(QString("Value %1").arg(value));
barProgress->setValue(value);
}
private:
QLabel *barLabel;
QProgressBar *barProgress;
int value;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"