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. qlabel not changing dynamically
Qt 6.11 is out! See what's new in the release blog

qlabel not changing dynamically

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 3.3k 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.
  • J Offline
    J Offline
    johnby
    wrote on last edited by
    #1

    I have a function (slot) where I try to set the text of a label after the label has already been created.

    void MyTab::editStuff(std::shared_ptr<Stuff> stuff)
    {
        myLabel->setText(stuff->name());
    }
    

    But it's not changing the label in my display. I plan on doing this often (user driven).

    What am I missing?

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      Don't mix std::shared_ptr into slots. Therein lies madness.

      Passing data into a slot isn't as flexible as you're trying to make it. Only pass types that have been registered with the metaobject system. Best to stick with Qt derived types or basic ordinal types.

      MySlot(Qstring& str) {
          myLabel->setText(str);
      }
      

      The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

      1 Reply Last reply
      1
      • J Offline
        J Offline
        johnby
        wrote on last edited by johnby
        #3

        I've changed class Stuff to be derived from QObject, was a POD.

        class Stuff : QObject
        {
            Q_OBJECT
        
            private:
        
                QString name;
            
            public:
                Stuff(const QString &name);
                
                QString name() const;
        };      
        

        But now I can't seem to get past this error ...

        stuff-model.cpp:55:27:   required from here 
        /usr/include/qt5/QtCore/qlist.h:465:65: error: use of deleted function ‘Stuff::Stuff(const Stuff&)’
          465 |     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);                                                                               
          |                                                                 ^~~~~~~~In file included from stuff-
        model.h:6,                                                                                       from stuff-model.
        cpp:3:                                                                          ../../../../stuff.h:6:7: note: ‘Stuff:
        :Stuff(const Stuff&)’ is implicitly deleted because the default definition would be ill-formed:       
        6 | class Stuff : QObject                                                
          |       ^~~~~~~~                                                          
        stuff.h:6:7: error: use of deleted function ‘QObject::QObject(const QObject&)’
        In file included from /usr/include/qt5/QtCore/qatomic.h:41,         
                     from /usr/include/qt5/QtCore/qvariant.h:43,       
                     from /usr/include/qt5/QtCore/qabstractitemmodel.h:43,
                     from /usr/include/qt5/QtCore/QAbstractTableModel:1,                             
        from stuff-model.h:3,                                                                      
                     from stuff-model.cpp:3:
        /usr/include/qt5/QtCore/qobject.h:467:20: note: declared here             
          467 |     Q_DISABLE_COPY(QObject)                                             
          |                    ^~~~~~~                                              
        /usr/include/qt5/QtCore/qglobal.h:435:5: note: in definition of macro ‘Q_DISABLE
        _COPY’                                                                          
          435 |     Class(const Class &) = delete;\
          |     ^~~~~
        

        I'm thinking it's that last line ...

        Class(const Class &) = delete;
        

        But not sure how to handle this now that I'm having to derive from QObject.

        JKSHJ 1 Reply Last reply
        0
        • J johnby

          I've changed class Stuff to be derived from QObject, was a POD.

          class Stuff : QObject
          {
              Q_OBJECT
          
              private:
          
                  QString name;
              
              public:
                  Stuff(const QString &name);
                  
                  QString name() const;
          };      
          

          But now I can't seem to get past this error ...

          stuff-model.cpp:55:27:   required from here 
          /usr/include/qt5/QtCore/qlist.h:465:65: error: use of deleted function ‘Stuff::Stuff(const Stuff&)’
            465 |     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);                                                                               
            |                                                                 ^~~~~~~~In file included from stuff-
          model.h:6,                                                                                       from stuff-model.
          cpp:3:                                                                          ../../../../stuff.h:6:7: note: ‘Stuff:
          :Stuff(const Stuff&)’ is implicitly deleted because the default definition would be ill-formed:       
          6 | class Stuff : QObject                                                
            |       ^~~~~~~~                                                          
          stuff.h:6:7: error: use of deleted function ‘QObject::QObject(const QObject&)’
          In file included from /usr/include/qt5/QtCore/qatomic.h:41,         
                       from /usr/include/qt5/QtCore/qvariant.h:43,       
                       from /usr/include/qt5/QtCore/qabstractitemmodel.h:43,
                       from /usr/include/qt5/QtCore/QAbstractTableModel:1,                             
          from stuff-model.h:3,                                                                      
                       from stuff-model.cpp:3:
          /usr/include/qt5/QtCore/qobject.h:467:20: note: declared here             
            467 |     Q_DISABLE_COPY(QObject)                                             
            |                    ^~~~~~~                                              
          /usr/include/qt5/QtCore/qglobal.h:435:5: note: in definition of macro ‘Q_DISABLE
          _COPY’                                                                          
            435 |     Class(const Class &) = delete;\
            |     ^~~~~
          

          I'm thinking it's that last line ...

          Class(const Class &) = delete;
          

          But not sure how to handle this now that I'm having to derive from QObject.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by JKSH
          #4

          @johnby said in qlabel not changing dynamically:

          I've changed class Stuff to be derived from QObject, was a POD.

          May I ask why?

          See https://doc.qt.io/qt-5/object.html#qt-objects-identity-vs-value -- Think of QObjects as "identities". If your class represents data instead of an identity, there is no reason to inherit QObject.

          /usr/include/qt5/QtCore/qlist.h:465:65: error: use of deleted function ‘Stuff::Stuff(const Stuff&)’
          

          To enforce the idea that QObjects are Identity objects, the QObject copy constructor is deleted. This means they QObjects cannot be copied, and that's why your compiler won't let you store QObjects in a QList (or any other container)

          To solve your original problem, a typical approach is to make your class POD again, and make your slot void MyTab::editStuff(const Stuff &stuff)

          If you want different suggestions, please provide more details about how you've structured your code. (For example: What is the relationship between Stuff and MyTab? Where do you store your Stuff?)

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          3
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by Kent-Dorfman
            #5

            @JKSH said in qlabel not changing dynamically:

            and make your slot void MyTab::editStuff(const Stuff &stuff)

            Unless I'm mistaken the above won't work unless the Stuff class is registered with the metadata system. Is that not true? I'm sure I read somewhere years ago that the slot parameter types needed to be registered with the framework? Not true?

            Is Q_DECLARE_METATYPE(type) not required any more? Shows how frequently I pass class types. LOL

            The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

            JKSHJ 1 Reply Last reply
            0
            • Kent-DorfmanK Kent-Dorfman

              @JKSH said in qlabel not changing dynamically:

              and make your slot void MyTab::editStuff(const Stuff &stuff)

              Unless I'm mistaken the above won't work unless the Stuff class is registered with the metadata system. Is that not true? I'm sure I read somewhere years ago that the slot parameter types needed to be registered with the framework? Not true?

              Is Q_DECLARE_METATYPE(type) not required any more? Shows how frequently I pass class types. LOL

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @Kent-Dorfman said in qlabel not changing dynamically:

              Unless I'm mistaken the above won't work unless the Stuff class is registered with the metadata system. Is that not true?

              You only need to register if you want a Queued connection. Direct connections don't need registration.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              3
              • J Offline
                J Offline
                johnby
                wrote on last edited by johnby
                #7

                I changed it back to a POD and made slot const, to no avail. But at least things are back to compiling. Here's more detail on what I'm doing.

                My goal is to have only one "material" tab on the display and when a user selects a material from a QTableView in another pane, then all the qlabels and other qwidgets within the tab will get set to the values of the selected "material" object that matches that name.

                What I do is have my user select a row in a QTableView which emits materialSelected() and calls the editMaterial() slot.

                connect(qobject_cast<MaterialsProperties*>(materials),
                    &MaterialsProperties::materialSelected,
                    qobject_cast<MaterialsTab*>(tabMaterials),
                    &MaterialsTab::editMaterial);
                

                This works.
                Next is the signal/slot where I try to set the text of a label after the label has already been created.

                void MaterialsProperties::changeTab(const QString &name)
                {   
                    for (auto& material : *materials) {
                        if (material.name().compare(name) == 0) {
                            emit materialSelected(material);
                
                            break;
                        }
                    }
                
                    mainWin->showMainViewTabByName(MATERIALS);
                }   
                
                void MaterialsTab::editMaterial(const Material &material)
                {
                    myLabel->setText(material->name());
                }
                

                But it's not changing the label in the display. Class Material is defined as a POD as follows.

                class Material
                {
                    private:
                        QString materialName;
                    
                    public:
                        Material(const QString &name);
                    
                        QString name() const;
                };
                

                Thoughts?

                JKSHJ 1 Reply Last reply
                0
                • J johnby

                  I changed it back to a POD and made slot const, to no avail. But at least things are back to compiling. Here's more detail on what I'm doing.

                  My goal is to have only one "material" tab on the display and when a user selects a material from a QTableView in another pane, then all the qlabels and other qwidgets within the tab will get set to the values of the selected "material" object that matches that name.

                  What I do is have my user select a row in a QTableView which emits materialSelected() and calls the editMaterial() slot.

                  connect(qobject_cast<MaterialsProperties*>(materials),
                      &MaterialsProperties::materialSelected,
                      qobject_cast<MaterialsTab*>(tabMaterials),
                      &MaterialsTab::editMaterial);
                  

                  This works.
                  Next is the signal/slot where I try to set the text of a label after the label has already been created.

                  void MaterialsProperties::changeTab(const QString &name)
                  {   
                      for (auto& material : *materials) {
                          if (material.name().compare(name) == 0) {
                              emit materialSelected(material);
                  
                              break;
                          }
                      }
                  
                      mainWin->showMainViewTabByName(MATERIALS);
                  }   
                  
                  void MaterialsTab::editMaterial(const Material &material)
                  {
                      myLabel->setText(material->name());
                  }
                  

                  But it's not changing the label in the display. Class Material is defined as a POD as follows.

                  class Material
                  {
                      private:
                          QString materialName;
                      
                      public:
                          Material(const QString &name);
                      
                          QString name() const;
                  };
                  

                  Thoughts?

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @johnby said in qlabel not changing dynamically:

                  Thoughts?

                  First, do some debugging to see which functions are called and which functions are not. You can use breakpoints or print debug messages.

                  For example, put qDebug() inside your for-loop to check if the signal is even emitted:

                  for (const auto& material : *materials) { // NOTE: Use `const` since the variable won't be changed
                  	qDebug() << "Checking" << material.name();
                  	
                  	if (material.name() == name) { // NOTE: Just use `==`, don't need `compare()`
                  		qDebug() << "Found it!";
                  		emit materialSelected(material);
                  		break;
                  	}
                  }
                  

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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

                    But that's what I mean when I said it works above when showing the signal/slot. The slot is definitely being called here ...

                    void MaterialsTab::editMaterial(const Material &material)
                    {
                        myLabel->setText(material->name());
                    }
                    

                    It's just that the text is not changing. Also, once I get this working, I do plan on adding other fields to the Material class and having changes made, hence, not having const there.

                    JKSHJ 1 Reply Last reply
                    0
                    • J johnby

                      But that's what I mean when I said it works above when showing the signal/slot. The slot is definitely being called here ...

                      void MaterialsTab::editMaterial(const Material &material)
                      {
                          myLabel->setText(material->name());
                      }
                      

                      It's just that the text is not changing. Also, once I get this working, I do plan on adding other fields to the Material class and having changes made, hence, not having const there.

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #10

                      @johnby said in qlabel not changing dynamically:

                      It's just that the text is not changing.

                      What do you see when you debug your label?

                      void MaterialsTab::editMaterial(const Material &material)
                      {
                          qDebug() << myLabel->parentWidget() << myLabel->isVisible() << myLabel->pos()
                          qDebug() << myLabel->text();
                      
                          myLabel->setText("Hello");
                      
                          qDebug() << myLabel->text();
                      }
                      

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        johnby
                        wrote on last edited by johnby
                        #11

                        I definitely see the text that I'm expecting from the passed in parameter (const Material &material) when printing to console.

                        I even write ...

                        materialLabel->setText("testing");
                        

                        and nothing changes in the tab.

                        Update:
                        I just added

                        qDebug() << materialLabel->parentWidget() << materialLabel->isVisible() << materialLabel->pos();
                        

                        And got ...

                        QGroupBox(0x558878be3740) false QPoint(0,0)
                        

                        It is in a QGroupBox, but the isVisible says false, so I added setVisible(true) to the QLabel, but nothing changed. I can see all qwidgets within the tab that I'm expecting to see, except for this label which is initialized as new QLabel(); Which I believe means empty. I did initialize w/ a string and that of course displayed the label initialized with the string, but still, the value never updates dynamically when the slot is called.

                        So, don't quite understand why the labels isVisible is showing false, because I can see it when I give it a string to init with.

                        JKSHJ 1 Reply Last reply
                        0
                        • J johnby

                          I definitely see the text that I'm expecting from the passed in parameter (const Material &material) when printing to console.

                          I even write ...

                          materialLabel->setText("testing");
                          

                          and nothing changes in the tab.

                          Update:
                          I just added

                          qDebug() << materialLabel->parentWidget() << materialLabel->isVisible() << materialLabel->pos();
                          

                          And got ...

                          QGroupBox(0x558878be3740) false QPoint(0,0)
                          

                          It is in a QGroupBox, but the isVisible says false, so I added setVisible(true) to the QLabel, but nothing changed. I can see all qwidgets within the tab that I'm expecting to see, except for this label which is initialized as new QLabel(); Which I believe means empty. I did initialize w/ a string and that of course displayed the label initialized with the string, but still, the value never updates dynamically when the slot is called.

                          So, don't quite understand why the labels isVisible is showing false, because I can see it when I give it a string to init with.

                          JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by
                          #12

                          @johnby Is is possible that myLabel and the label that you see on screen are actually 2 different objects?

                          Please show us how you initialized the QGroupBox and the QLabel. In particular:

                          1. Did you use Qt Designer, or did you write pure C++ code?
                          2. How did you add your QLabel to the QGroupBox?

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          1 Reply Last reply
                          1
                          • J Offline
                            J Offline
                            johnby
                            wrote on last edited by johnby
                            #13

                            It's all C++ code, no designer work.
                            Here's how I initialize the label and group in the ctor of my tab widget.

                            class MaterialsTab : public QWidget
                            {
                                Q_OBJECT
                                
                                private:
                                    QLabel *materialLabel;
                                    QVBoxLayout *materialLayout;
                                
                                    // ...
                                
                                public:
                                    explicit MaterialsTab(QWidget *parent = 0);
                                
                                public slots:
                                    void editMaterial(const Material &material);
                            }
                            
                            MaterialsTab::MaterialsTab(QWidget *parent) : QWidget(parent)
                            {
                                materialLabel = new QLabel();
                                materialLabel->setVisible(true); // shouldn't need this, but tried anyway
                            
                                // ...
                            
                                materialLayout = new QVBoxLayout;
                            
                                // ...
                                QGroupBox *nameGroup = new QGroupBox;
                            
                                QGridLayout *layout = new QGridLayout;
                                layout->addWidget(materialLabel, 0, 1, 1, 3);
                                // ...
                            
                                nameGroup->setLayout(layout);
                            
                                materialLayout->addWidget(nameGroup);
                            }
                            
                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Your code you show now don't use myLabel at all ...

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                johnby
                                wrote on last edited by johnby
                                #15

                                Ugh, forgot the other half there. I've updated that post to show the declaration of the QLabel in question.

                                1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  ChrisW67
                                  wrote on last edited by
                                  #16

                                  Is materialLayout applied to a widget?

                                  J 1 Reply Last reply
                                  1
                                  • Christian EhrlicherC Online
                                    Christian EhrlicherC Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    I still don't see where myLabel comes from and where you initialize it.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    J 1 Reply Last reply
                                    0
                                    • C ChrisW67

                                      Is materialLayout applied to a widget?

                                      J Offline
                                      J Offline
                                      johnby
                                      wrote on last edited by
                                      #18

                                      @ChrisW67 said in qlabel not changing dynamically:

                                      Is materialLayout applied to a widget?

                                      Yes, materlalLayout gets applied to the tab, and again, all this is fine. I have more widgets in that mix and they all show up. even materialLabel displays fine w/ whatever it's initialized with. It just doesn't update dynamically when my slot gets called and does a setText().

                                      JKSHJ 1 Reply Last reply
                                      0
                                      • Christian EhrlicherC Christian Ehrlicher

                                        I still don't see where myLabel comes from and where you initialize it.

                                        J Offline
                                        J Offline
                                        johnby
                                        wrote on last edited by
                                        #19

                                        @Christian-Ehrlicher said in qlabel not changing dynamically:

                                        I still don't see where myLabel comes from and where you initialize it.

                                        Ah, you're not looking at the latest post where things are renamed and I've given all the details. Forget anything prefixed w/ my.

                                        1 Reply Last reply
                                        0
                                        • J johnby

                                          @ChrisW67 said in qlabel not changing dynamically:

                                          Is materialLayout applied to a widget?

                                          Yes, materlalLayout gets applied to the tab, and again, all this is fine. I have more widgets in that mix and they all show up. even materialLabel displays fine w/ whatever it's initialized with. It just doesn't update dynamically when my slot gets called and does a setText().

                                          JKSHJ Offline
                                          JKSHJ Offline
                                          JKSH
                                          Moderators
                                          wrote on last edited by JKSH
                                          #20

                                          @johnby said in qlabel not changing dynamically:

                                          Yes, materlalLayout gets applied to the tab, and again, all this is fine. I have more widgets in that mix and they all show up. even materialLabel displays fine w/ whatever it's initialized with. It just doesn't update dynamically when my slot gets called and does a setText().

                                          Please provide a minimal compilable example (including main.cpp) which contains this issue -- copy your project and remove the parts that are not involved with the issue. It is difficult to troubleshoot the code when bits are missing. For example, we can't see how materialLayout is applied to the tab, or where/when the connection is made.

                                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                          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