Results 1 to 3 of 3

Thread: QLabel in QStatusBar does freaky stuff

  1. #1
    Join Date
    May 2012
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QLabel in QStatusBar does freaky stuff

    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!

    Qt Code:
    1. //in header file:
    2. QStatusBar *stat;
    3. QLabel *barStatus;
    4.  
    5. //in constructor:
    6. barStatus = new QLabel ( tr ("Ready" ) );
    7. bar = new QProgressBar(this);
    8. bar -> setFixedWidth(175);
    9.  
    10. stat = new QStatusBar(this);
    11. stat -> addWidget(bar, Qt::AlignLeft);
    12. stat -> addWidget(barStatus, Qt::AlignRight);
    13.  
    14. //in other method that updates counter
    15. bar -> setValue(counter);
    16. barStatus -> clear();
    17. barStatus -> setText("Saved /home/../output" + counter);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel in QStatusBar does freaky stuff

    Qt Code:
    1. stat = new QStatusBar( this );
    2.  
    3. barStatus = new QLabel ( tr ("Ready" ), stat );
    4.  
    5. bar = new QProgressBar( stat );
    6. bar->setOrientation( Qt::Horizontal );
    7. bar->setAlignment( Qt::AlignAbsolute );
    8. bar->setFixedWidth( 175 );
    9.  
    10. stat->addWidget( barStatus, 1 );
    11. stat->addPermanentWidget( bar );
    12. stat->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Minimum );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QLabel in QStatusBar does freaky stuff

    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.
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MainWindow: public QMainWindow {
    5. Q_OBJECT
    6. public:
    7. MainWindow(QWidget *p = 0): QMainWindow(p) {
    8. resize(320, 240);
    9. barLabel = new QLabel("Ready", this);
    10. barProgress = new QProgressBar(this);
    11. barProgress->setMinimum(0);
    12. barProgress->setMaximum(99);
    13. statusBar()->addWidget(barProgress, 1); // or statusBar()->addWidget(barProgress)
    14. statusBar()->addWidget(barLabel, 1); // or statusBar()->addWidget(barLabel)
    15.  
    16. value = 0;
    17. QTimer *timer = new QTimer(this);
    18. connect(timer, SIGNAL(timeout()), SLOT(tick()));
    19. timer->start(50);
    20. }
    21. public slots:
    22. void tick() {
    23. value = (value + 1) % 100;
    24. barLabel->setText(QString("Value %1").arg(value));
    25. barProgress->setValue(value);
    26. }
    27. private:
    28. QLabel *barLabel;
    29. QProgressBar *barProgress;
    30. int value;
    31. };
    32.  
    33. int main(int argc, char *argv[])
    34. {
    35. QApplication app(argc, argv);
    36.  
    37. MainWindow m;
    38. m.show();
    39. return app.exec();
    40. }
    41. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Is it necessary to setDomainName and other stuff ?
    By Alir3z4 in forum General Discussion
    Replies: 1
    Last Post: 1st December 2011, 22:07
  2. Replies: 1
    Last Post: 29th September 2009, 19:44
  3. Phonon stuff
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2009, 12:26
  4. QStatusBar
    By newermind in forum Qt Programming
    Replies: 3
    Last Post: 1st June 2009, 21:48
  5. Get Hot New Stuff!!!!
    By Mystical Groovy in forum KDE Forum
    Replies: 4
    Last Post: 16th December 2008, 00:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.