March 26, 2012

notgary notgary
Lab Rat
37 posts

[SOLVED] Updating the text on a QLabel

 

I trying to use the following code to change the text in a QLabel from “Ready.” to “Boo!” when I press the button marked “Change text”, but when the button is clicked, nothing happens. I’ve looked through the QLabel documentation and can’t find anything that would indicate that I need to set some special flag to allow updating of text, so I was wondering if there was something else that I’m missing here.

  1. #include <QtGui>
  2.  
  3. int main( int argc, char** args )
  4. {
  5.   QApplication app( argc, args );
  6.  
  7.   QLabel *label = new QLabel;
  8.   label->setText( "Ready." );
  9.  
  10.   QPushButton *button = new QPushButton;
  11.   button->setText( "Change text" );
  12.   QObject::connect( button, SIGNAL( clicked() ), label, SLOT( setText( "Boo!" ) ) );
  13.  
  14.   QHBoxLayout *layout = new QHBoxLayout;
  15.   layout->addWidget( button );
  16.   layout->addWidget( label );
  17.  
  18.   QMainWindow *mainWindow = new QMainWindow;
  19.   QWidget *centralWidget = new QWidget;
  20.   centralWidget->setLayout( layout );
  21.   mainWindow->setCentralWidget( centralWidget );
  22.   mainWindow->show();
  23.  
  24.   return app.exec();
  25. }

9 replies

March 26, 2012

umerlaton umerlaton
Lab Rat
7 posts

Hi there,
hmmm i believe the problem is when you connect the signal and the slot. both must have the same parameter.Your button is emiting a clicked signal but nothing else and your qlabel slot settext(QString) is expecting a Qstring from the emitter . I believe you cant just put “Boo!”. You can have a public slot in your class with no parameters and call seText(“Boo!”) from there.

March 26, 2012

1+1=10 1+1=10
Ant Farmer
643 posts

The following code is basically wrong.

  1.   QObject::connect( button, SIGNAL( clicked() ), label, SLOT( setText( "Boo!" ) ) );

When you running the application under debug mode, you should get a warning message says that, the slot you used doesn’t exists.

In addition, this “connect” statement will return false instead of true.

March 26, 2012

maxvanceffer maxvanceffer
Lab Rat
64 posts

Use for debug porpose Q_ASSERT(connect(SIGNAL),SLOT);

And u will see in console output, which connections not connected.

March 26, 2012

mlong mlong
Robot Herder
1551 posts

Using Q_ASSERT(connect(…)) is dangerous because in release mode the Q_ASSERT line will be ignored and your connect will not happen at all.

If you still want to use Q_ASSERT to test your connection, do it as follows:

  1. bool connected = connect(...);
  2. Q_ASSERT(connected);

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

March 26, 2012

notgary notgary
Lab Rat
37 posts

Thanks a lot for your replies. It appears that the code I posted didn’t effectively illustrate my situation. It was only an example I cobbled together for posting here. In my actual code, the signals are successfully connected, as the std::cout statements I placed in them for debugging were successfully triggered.

My problem seems to be that the label is not being updated on the UI, as I can successfully extract the new text using the labsl’s text() method, but the text being displayed is still the old one. Does this behaviour sound familiar to anyone?

March 26, 2012

mlong mlong
Robot Herder
1551 posts

Are you certain that you don’t somehow have a second QLabel somewhere, perhaps?

Can you post a snippet of your code, itself?

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

March 26, 2012

notgary notgary
Lab Rat
37 posts

There are two classes involved in this process, the label itself and one of its members, a dialog, that is being used to accept the new text. The slot that’s actually performing the rename is at the very bottom. The skeletons of both classes are as follows:

  1. class RenameTitleDialog : public QDialog
  2. {
  3.   Q_OBJECT
  4. public:
  5.   RenameTitleDialog( QWidget *parent = NULL );
  6.   QString editorText();
  7.  
  8. private:
  9.   QPushButton *m_confirmButton;
  10.   QLineEdit   *m_titleEditor;
  11. };
  12.  
  13. RenameTitleDialog::RenameTitleDialog( QWidget *parent ) : QDialog( parent )
  14. {
  15.   m_confirmButton = new QPushButton;  
  16.   m_confirmButton->setText( tr( "&Confirm" ) );  
  17.   m_confirmButton->setDefault( true );
  18.   m_confirmButton->setEnabled( false );
  19.   connect( m_confirmButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
  20. }
  21.  
  22. QString RenameTitleDialog::editorText()
  23. {
  24.   return m_titleEditor->text();
  25. }
  26.  
  27. class Task : public QLabel
  28. {
  29.   Q_OBJECT
  30. public:
  31.   Task( QWidget *parent = NULL, QString m_taskTitle = QString() );
  32.  
  33. private:
  34.   RenameTitleDialog *m_renameTitleDialog;
  35.  
  36. private slots:
  37.   void slotOpenRenameTitleDialog();
  38.   void slotRenameTitle();
  39. };
  40.  
  41. Task::Task( QWidget* parent, QString title ) :
  42.   QLabel( parent )
  43. {
  44.   setText( title );
  45.   m_renameTitleDialog = new RenameTitleDialog( this );
  46.   setStyleSheet( "Task{ background-color: yellow; color: black; border: 1px solid black; border-radius: 4px }");
  47.   createActions();
  48. }
  49.  
  50. void Task::createActions()
  51. {
  52.   connect( actionRenameTitle, SIGNAL( triggered() ), this, SLOT( slotOpenRenameTitleDialog() ) );
  53.   connect( m_renameTitleDialog, SIGNAL( accepted() ), this, SLOT( slotRenameTitle() ) );
  54. }
  55.  
  56. void Task::slotOpenRenameTitleDialog()
  57. {
  58.   m_renameTitleDialog->exec();
  59. }
  60.  
  61. void Task::slotRenameTitle()
  62. {
  63.   QString newTitle = m_renameTitleDialog->editorText();
  64.   setText( newTitle );
  65. }

March 28, 2012

joonhwan joonhwan
Lab Rat
94 posts

On your OT, there is QSignalMapper class is for you, I think.

 Signature 

joonhwan at gmail dot com

March 30, 2012

notgary notgary
Lab Rat
37 posts

Ok, I’ve compiled the code on Mac OS and the slots are now working perfectly. I don’t know what was messed up with my Linux environment (where it was being compiled before), but my code was in fact fine.

 
  ‹‹ Mouse Cursor reset problem      OpenGL Multithread ››

You must log in to post a reply. Not a member yet? Register here!