[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 )
    {
    QApplication app( argc, args );

    QLabel *label = new QLabel;
    label->setText( "Ready." );

    QPushButton *button = new QPushButton;
    button->setText( "Change text" );
    QObject::connect( button, SIGNAL( clicked() ), label, SLOT( setText( "Boo!" ) ) );

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget( button );
    layout->addWidget( label );

    QMainWindow *mainWindow = new QMainWindow;
    QWidget *centralWidget = new QWidget;
    centralWidget->setLayout( layout );
    mainWindow->setCentralWidget( centralWidget );
    mainWindow->show();

    return app.exec();
    }@



  • 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.



  • The following code is basically wrong.
    @
    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.



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

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


  • Moderators

    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:
    @
    bool connected = connect(...);
    Q_ASSERT(connected);
    @



  • 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?


  • Moderators

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

    Can you post a snippet of your code, itself?



  • 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:

    @class RenameTitleDialog : public QDialog
    {
    Q_OBJECT
    public:
    RenameTitleDialog( QWidget *parent = NULL );
    QString editorText();

    private:
    QPushButton *m_confirmButton;
    QLineEdit *m_titleEditor;
    };

    RenameTitleDialog::RenameTitleDialog( QWidget *parent ) : QDialog( parent )
    {
    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() ) );
    }

    QString RenameTitleDialog::editorText()
    {
    return m_titleEditor->text();
    }

    class Task : public QLabel
    {
    Q_OBJECT
    public:
    Task( QWidget *parent = NULL, QString m_taskTitle = QString() );

    private:
    RenameTitleDialog *m_renameTitleDialog;

    private slots:
    void slotOpenRenameTitleDialog();
    void slotRenameTitle();
    };

    Task::Task( QWidget* parent, QString title ) :
    QLabel( parent )
    {
    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()
    {
    QString newTitle = m_renameTitleDialog->editorText();
    setText( newTitle );
    }@



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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.