Results 1 to 5 of 5

Thread: QSlider & QLabel : setText after SIGNAL from QSlider not working

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSlider & QLabel : setText after SIGNAL from QSlider not working

    Hi,

    As the title states, I have some issues with a QSlider/QLabel connection.

    I simply want to show the slider's position in a label as a text and update it every time the slider is changed (and all this in a QMessageBox).

    Here's the failing code :

    Qt Code:
    1. /* In the mainFrame constructor ... */
    2.  
    3. delete choiceBoxB.layout();
    4. choiceBoxBLayout = new QVBoxLayout;
    5. choiceBoxB.setLayout(choiceBoxBLayout);
    6. sliderPosB = new QLabel("100%");
    7. percentageSliderB = new QSlider(Qt::Horizontal);
    8. percentageSliderB->setMinimum(0);
    9. percentageSliderB->setMaximum(200);
    10. percentageSliderB->setSliderPosition(100);
    11. connect(percentageSliderB,SIGNAL(valueChanged(int)),this,SLOT(updateSliderBPos(int)));
    12. choiceBoxBLayout->addWidget(sliderPosB);
    13. choiceBoxBLayout->addWidget(percentageSliderB);
    14.  
    15. /* ... */
    16.  
    17. void mainFrame::updateSliderBPos(int value){
    18. sliderPosB->setText(QString("%1%").arg(value));
    19. }
    To copy to clipboard, switch view to plain text mode 

    Every variable or object or class has been declared in the header file (no compiling issues).

    For some reason I don't understand, it worked almost fine once but I couldn't find out what was not working, that wasn't enough.

    The purpose of the slider and the label is to allow user to choose a percentage to change the brightness of an image (project in an image editor).

    Thanks for your help,

    Faradn

    UPDATE :

    I forgot to mention that the input text is perfectly fine (tested with std::cout) and that the signal works fine too. It is the setText that is not working properly.
    Last edited by Faradn; 28th November 2011 at 22:43.

  2. #2
    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: QSlider & QLabel : setText after SIGNAL from QSlider not working

    Works just fine here:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class Dialog: public QDialog {
    5. Q_OBJECT
    6.  
    7. QVBoxLayout *choiceBoxBLayout;
    8. QLabel *sliderPosB;
    9. QSlider *percentageSliderB;
    10.  
    11. public:
    12. Dialog(QWidget *p = 0): QDialog(p) {
    13.  
    14. choiceBoxBLayout = new QVBoxLayout;
    15. setLayout(choiceBoxBLayout);
    16.  
    17. sliderPosB = new QLabel("100%");
    18. percentageSliderB = new QSlider(Qt::Horizontal);
    19. percentageSliderB->setMinimum(0);
    20. percentageSliderB->setMaximum(200);
    21. percentageSliderB->setSliderPosition(100);
    22. connect(percentageSliderB,SIGNAL(valueChanged(int)),this,SLOT(updateSliderBPos(int)));
    23. choiceBoxBLayout->addWidget(sliderPosB);
    24. choiceBoxBLayout->addWidget(percentageSliderB);
    25. }
    26. public slots:
    27. void updateSliderBPos(int value){ sliderPosB->setText(QString("%1%").arg(value)); }
    28. };
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QApplication app(argc, argv);
    33.  
    34. Dialog d;
    35. d.show();
    36. return app.exec();
    37. }
    38. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Why are you using a QMessageBox and not just your own QDialog subclass?

  3. The following user says thank you to ChrisW67 for this useful post:

    Faradn (1st December 2011)

  4. #3
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSlider & QLabel : setText after SIGNAL from QSlider not working

    Hi ChrisW67 and thank you for helping me out,

    Why does it work with a QDialog and not a QMessageBox, I wonder ...

    I used the QMessageBox class because I need the clickedButton() method in some other functions.

    Now considering your code and mine, the only thing that changed is the QDialog/QMessageBox thing ... Why couldn't it work in a QMessageBox ?

    Again, thanks for your help,


    Faradn


    UPDATE : whatever it is (QMessageBox or QDialog) the slider position Label remains unupdated.
    Last edited by Faradn; 29th November 2011 at 14:08.

  5. #4
    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: QSlider & QLabel : setText after SIGNAL from QSlider not working

    The code I posted compiles and works as-is. You can add your buttons to the QDialog and mimic the parts of the QMessageBox interface you need.

    By using the QMessageBox and ripping the layout out of it you are leaving dangling sub-widgets, which you then overlay with another layout. It is ugly and the message box icon and message are lost, but still the slider works here:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3. class Widget: public QWidget {
    4. Q_OBJECT
    5. QMessageBox choiceBoxB;
    6. QVBoxLayout *choiceBoxBLayout;
    7. QLabel *sliderPosB;
    8. QSlider *percentageSliderB;
    9. public:
    10. Widget(QWidget *p = 0):
    11. QWidget(p),
    12. choiceBoxB(QMessageBox::Question, "Deep title of significance", "Insightful question")
    13. {
    14. QTimer::singleShot(1000, this, SLOT(doit()));
    15. }
    16. public slots:
    17. void doit() {
    18. delete choiceBoxB.layout();
    19. choiceBoxBLayout = new QVBoxLayout;
    20. choiceBoxB.setLayout(choiceBoxBLayout);
    21. sliderPosB = new QLabel("100%");
    22. percentageSliderB = new QSlider(Qt::Horizontal);
    23. percentageSliderB->setMinimum(0);
    24. percentageSliderB->setMaximum(200);
    25. percentageSliderB->setSliderPosition(100);
    26. connect(percentageSliderB,SIGNAL(valueChanged(int)),this,SLOT(updateSliderBPos(int)));
    27. choiceBoxBLayout->addWidget(sliderPosB);
    28. choiceBoxBLayout->addWidget(percentageSliderB);
    29. choiceBoxB.show();
    30. }
    31. void updateSliderBPos(int value){ sliderPosB->setText(QString("%1%").arg(value)); }
    32. };
    33.  
    34. int main(int argc, char *argv[])
    35. {
    36. QApplication app(argc, argv);
    37. Widget w;
    38. w.show();
    39. return app.exec();
    40. }
    41. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    If you are not getting updated labels then you are probably doing something that blocks the program from returning to the event loop.

  6. The following user says thank you to ChrisW67 for this useful post:

    Faradn (1st December 2011)

  7. #5
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSlider & QLabel : setText after SIGNAL from QSlider not working

    Hi ChrisW67 and thanks a lot for all the solutions you are providing me.

    It is true that using a QMessageBox and then delete its layout is quite ugly. I don't like that either but I need the addButton() and buttonClicked() methods.

    Both your options work but I am having trouble finding out what my error is in my code and where it comes from. I'm really sorry, I feel quite bad not being able to use your perfectly suited and simple solutions to my simple issue ...

    So please let me paste more code (the whole message box construction and when it is triggered) :

    Qt Code:
    1. // Brightness parameters setup
    2.  
    3. delete choiceBoxB.layout();
    4. choiceBoxBLayout = new QVBoxLayout;
    5. choiceBoxB.setLayout(choiceBoxBLayout);
    6. choiceBoxBMessage = new QLabel("Choisissez le pourcentage de luminosite souhaitee :");
    7. sliderPosB = new QLabel("100%");
    8. percentageSliderB = new QSlider(Qt::Horizontal);
    9. bOkB = new QPushButton("&Ok");
    10. bCancelB = new QPushButton("&Annuler");
    11. percentageSliderB->setMinimum(0);
    12. percentageSliderB->setMaximum(200);
    13. percentageSliderB->setSliderPosition(100);
    14. connect(percentageSliderB,SIGNAL(valueChanged(int)),this,SLOT(updateSliderBPos(int)));
    15. choiceBoxBLayout->addWidget(choiceBoxBMessage);
    16. choiceBoxBLayout->addWidget(sliderPosB);
    17. choiceBoxB.addButton(bOkB,QMessageBox::AcceptRole);
    18. choiceBoxB.addButton(bCancelB,QMessageBox::RejectRole);
    19. choiceBoxBLayout->addWidget(percentageSliderB);
    20. choiceBoxBLayout->addWidget(bOkB);
    21. choiceBoxBLayout->addWidget(bCancelB);
    To copy to clipboard, switch view to plain text mode 

    and the slot that triggers choiceBoxB's execution (connected to a menu option, it works) :

    Qt Code:
    1. void mainFrame::changeBrightness() {
    2. if(currentImage.getBuffer() != NULL){
    3. choiceBoxB.exec();
    4. if(choiceBoxB.clickedButton()->text() == bOkB->text()){ // I know, this is even uglier ...
    5.  
    6. /* I am using quick and ugly solutions concerning the GUI because those are details in my project which is a duplicata of a GTK & C project. */
    7.  
    8. currentImage.changeBrightness(percentageSliderB->value());
    9. displayImage(currentImage);
    10. }
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    I have also discovered an interesting thing.

    As you can see now my message box elements also contain a message.

    I tried modifying the updateSliderBPos function from

    Qt Code:
    1. void updateSliderBPos(int value) {
    2. sliderPosB->setText(QString("%1%").arg(value));
    3. }
    To copy to clipboard, switch view to plain text mode 

    to :

    Qt Code:
    1. void updateSliderBPos(int value) {
    2. sliderPosB->setText(QString("%1%").arg(value));
    3. choiceBoxBMessage->setText(QString("%1%").arg(value));
    4. }
    To copy to clipboard, switch view to plain text mode 

    The choiceBoxBMessage is updated with the correct position of the slider (plus the percentage symbol) whereas the sliderPosB isn't.

    The sliderPosB label's purpose is only to show the slider position in that box. I do not see what differences there could be with the choiceBoxBMessage label ...


    Again thanks for your help,

    Faradn

Similar Threads

  1. Replies: 4
    Last Post: 24th October 2011, 12:00
  2. Problem with QSlider's signal
    By lni in forum Qt Programming
    Replies: 6
    Last Post: 21st September 2011, 20:29
  3. Replies: 7
    Last Post: 15th June 2011, 06:59
  4. QSlider and sliderReleased() signal
    By Luc4 in forum Qt Programming
    Replies: 1
    Last Post: 19th June 2010, 19:37
  5. Replies: 4
    Last Post: 4th March 2010, 12:44

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.