[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 (http://qt-project.org/doc/qt-5.0/qtdesigner/customwidgetplugin.html) 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_OBJECT
    Q_PROPERTY(QString text READ getUiTextLabel WRITE setUiTextLabel)

    public:
    explicit CustomWidget(QWidget *parent = 0)
    ~CustomWidget();

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

    private:
    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) {
    ui->setupUi(this);
    }

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

    void CustomWidget::setUiTextLabel(const QString &text) {
    ui->label->setText(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":http://qt-project.org/doc/qt-5/qobject.html#Q_PROPERTY to see how it works



  • Thank you SGaist :)


Log in to reply
 

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