Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Updating the text on a QLabel
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Updating the text on a QLabel

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 69.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    notgary
    wrote on last edited by
    #1

    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();
    }@

    1 Reply Last reply
    0
    • U Offline
      U Offline
      umerlaton
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maxvanceffer
          wrote on last edited by
          #4

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

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #5

            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);
            @

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0
            • N Offline
              N Offline
              notgary
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mlong
                wrote on last edited by
                #7

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

                Can you post a snippet of your code, itself?

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  notgary
                  wrote on last edited by
                  #8

                  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 );
                  }@

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    joonhwan
                    wrote on last edited by
                    #9

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

                    joonhwan at gmail dot com

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      notgary
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved