Important: Please read the Qt Code of Conduct -

[RESOLVE] Dynamic Property in custom widget plugin for designer (Q_PROPERTY)

  • I decided to create a custom widget for the qt-designer.
    The widget itself I made using the qt-designer, then as described here ( I made a plugin and it worked. Further wanted to add a custom widget properties in the property editor in the designer. To do this, I found only one solution:

    // custom_widget.h

    #include <QWidget>
    #include <QString>

    namespace Ui {
    class Form;

    class CustomWidget : public QWidget {
    Q_PROPERTY(QString text READ getUiTextLabel WRITE setUiTextLabel)

    explicit CustomWidget(QWidget *parent = 0)

    QString getUiTextLabel() const;
    void setUiTextLabel(const QString &text);

    Ui::Form *ui; // ui generated from uic, and have just QLabel *label

    // custom_widget.cpp

    #include "custom_widget.h"
    #include "ui_form.h"

    CustomWidget::CustomWidget(QWidget *parent)
    : QWidget(parent),
    ui(new Ui::Form) {

    QString CustomWidget::getUiTextLabel() const {
    return ui->label->text();

    void CustomWidget::setUiTextLabel(const QString &text) {

    CustomWidget::~CustomWidget() {
    delete ui;

    May be for this one has a simple solution, without duplicating code?
    And why can't use?
    Q_PROPERTY(QString text READ ui->label->text WRITE ui->label->setText)

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    This is currently the correct and simple solution.

    Have a look at "Q_PROPERTY's documentation": to see how it works

  • Thank you SGaist :)

Log in to reply