Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

qlabel not changing dynamically



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



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


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


  • Moderators

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



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


  • Moderators

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



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


  • Moderators

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


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


  • Moderators

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


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


  • Moderators

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


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

  • Lifetime Qt Champion

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



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



  • Is materialLayout applied to a widget?


  • Lifetime Qt Champion

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



  • @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().



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


  • Moderators

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


Log in to reply