[SOLVED] std::string and Q_PROPERTY()



  • I am unable to get std::string properties for my custom widget to appear in QtDesigner.

    Below is an example of the code i'm running that isn't working. Any suggestions?

    @
    // CustomCheckBox.h
    #pragma once
    #include <QtWidgets/QCheckBox>

    #include <string>

    Q_DECLARE_METATYPE(std::string)

    class CustomCheckBox : public QCheckBox
    {
    Q_OBJECT
    // this property does NOT appear in QtDesigner
    Q_PROPERTY(std::string name READ getName WRITE setName)

     public:
          CustomCheckBox(QWidget* parent = nullptr);
          virtual ~CustomCheckBox();
    
          void setName(const std::string& x);
          std::string getName() const;
    
     private:
          std::string name;
    

    };
    @

    @
    // CustomCheckBox.cpp
    #include "CustomCheckBox.h"

    CustomCheckBox::CustomCheckBox(QWidget* parent) : QCheckBox(parent)
    {
    // doesn't work
    qRegisterMetaTypestd::string();

     // doesn't work
     qRegisterMetaType<std::string>("std::string");
    

    }
    @



  • Hi,

    std::string is a template specification (some kind of typedef).
    I think that Q_PROPERTY doesn't workfor templates (check on the documentation).

    Anyway IMHO it's better to use QString as property.



  • I...should have known that. Thank you. Using QString will probably be what I do now.


Log in to reply
 

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