[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.
- #include <QtGui>
- int main( int argc, char** args )
- {
- label->setText( "Ready." );
- button->setText( "Change text" );
- layout->addWidget( button );
- layout->addWidget( label );
- centralWidget->setLayout( layout );
- mainWindow->setCentralWidget( centralWidget );
- mainWindow->show();
- return app.exec();
- }
9 replies
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.
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?
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:
- {
- Q_OBJECT
- public:
- private:
- };
- {
- m_confirmButton = new QPushButton;
- m_confirmButton->setText( tr( "&Confirm" ) );
- m_confirmButton->setDefault( true );
- m_confirmButton->setEnabled( false );
- connect( m_confirmButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
- }
- {
- return m_titleEditor->text();
- }
- {
- Q_OBJECT
- public:
- private:
- RenameTitleDialog *m_renameTitleDialog;
- private slots:
- void slotOpenRenameTitleDialog();
- void slotRenameTitle();
- };
- {
- setText( title );
- m_renameTitleDialog = new RenameTitleDialog( this );
- setStyleSheet( "Task{ background-color: yellow; color: black; border: 1px solid black; border-radius: 4px }");
- createActions();
- }
- void Task::createActions()
- {
- connect( actionRenameTitle, SIGNAL( triggered() ), this, SLOT( slotOpenRenameTitleDialog() ) );
- connect( m_renameTitleDialog, SIGNAL( accepted() ), this, SLOT( slotRenameTitle() ) );
- }
- void Task::slotOpenRenameTitleDialog()
- {
- m_renameTitleDialog->exec();
- }
- void Task::slotRenameTitle()
- {
- setText( newTitle );
- }
You must log in to post a reply. Not a member yet? Register here!



