Important: Please read the Qt Code of Conduct -

[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>


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

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


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

    CustomCheckBox::CustomCheckBox(QWidget* parent) : QCheckBox(parent)
    // doesn't work

     // doesn't work


  • 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