[Development] About Q_PROPERTY in custom widget plugin for designer

Svetkin Mikhail mikhail.svetkin at gmail.com
Fri Feb 7 09:05:09 CET 2014


Hellow all.
I was creating custom widgets with the help of Qt-designer model.
And I have noticed I have to duplicate my code, if I want to edit
properties of the widget in qt-designer property editor.

Simple Example:

   1. // custom_widget_first.h
   2.
   3. #include <QWidget>
   4. #include <QString>
   5. #include <QPaintEvent>
   6.
   7. class CustomWidgetFirst : public
QWidget<http://qt-project.org/doc/QWidget.html>
     {
   8.   Q_OBJECT
   9.  public:
   10.   explicit
CustomWidgetFirst(QWidget<http://qt-project.org/doc/QWidget.html>
    *parent = 0)
   11.   ~CustomWidgetFirst();
   12.
   13.   QString <http://qt-project.org/doc/QString.html> label() const;
   14.   void setLabel(const QString<http://qt-project.org/doc/QString.html>
    &text);
   15.
   16.  protected:
   17.   void paintEvent(QPaintEvent<http://qt-project.org/doc/QPaintEvent.html>
    *event);
   18.
   19.  private:
   20.   QString <http://qt-project.org/doc/QString.html> label;
   21. };
   22.
   23. // custom_widget_first.cpp
   24.
   25. #include "custom_widget_first.h"
   26.
   27. CustomWidgetFirst::CustomWidgetFirst(QWidget<http://qt-project.org/doc/QWidget.html>
    *parent)
   28.   : QWidget <http://qt-project.org/doc/QWidget.html>(parent) {
   29. }
   30.
   31. QString <http://qt-project.org/doc/QString.html> CustomWidgetFirst::
   label() const {
   32.   return label;
   33. }
   34.
   35. void CustomWidgetFirst::setLabel(const
QString<http://qt-project.org/doc/QString.html>
    &text)  {
   36.   label = text;
   37. }
   38.
   39. void CustomWidgetFirst::paintEvent(QPaintEvent<http://qt-project.org/doc/QPaintEvent.html>
    *event) {
   40.   // some paint
   41. }
   42.
   43. CustomWidgetFirst::~CustomWidgetFirst() {
   44.   delete ui;
   45. }
   46.
   47. // custom_widget_second.h
   48.
   49. #include <QWidget>
   50. #include <QString>
   51.
   52. namespace Ui {
   53.   class Form;
   54. }
   55.
   56. class CustomWidgetSecond : public
QWidget<http://qt-project.org/doc/QWidget.html>
     {
   57.   Q_OBJECT
   58.   Q_PROPERTY(QString <http://qt-project.org/doc/QString.html> text
   59.              READ getUiCustomWidgetSecondTextLabel
   60.              WRITE setUiCustomWidgetSecondTextLabel)
   61.
   62.  public:
   63.   explicit
CustomWidgetSecond(QWidget<http://qt-project.org/doc/QWidget.html>
    *parent = 0)
   64.   ~CustomWidgetSecond();
   65.
   66.   QString <http://qt-project.org/doc/QString.html>
    getUiCustomWidgetFirstLabel() const;               // dublicate code
   67.   void setUiCustomWidgetFirstTextLabel(const
QString<http://qt-project.org/doc/QString.html>
    &text); // dublicate code
   68.
   69.  private:
   70.   Ui::Form *ui; // ui generated from uic, and have several widgets,
   including CustomWidgetFirst
   71. };
   72.
   73. // custom_widget_second.cpp
   74.
   75. #include "custom_widget_second.h"
   76. #include "ui_form.h"
   77.
   78. CustomWidgetSecond::CustomWidgetSecond(QWidget<http://qt-project.org/doc/QWidget.html>
    *parent)
   79.   : QWidget <http://qt-project.org/doc/QWidget.html>(parent),
   80.     ui(new Ui::Form) {
   81.   ui->setupUi(this);
   82. }
   83.
   84. QString <http://qt-project.org/doc/QString.html> CustomWidgetSecond::
   getUiCustomWidgetFirstTextLabel() const { // dublicate code
   85.   return ui->CustomWidgetFirst->label();
    // dublicate code
   86. }
     // dublicate code
   87.
   88. void CustomWidgetSecond::setUiCustomWidgetFirstTextLabel(const
   QString <http://qt-project.org/doc/QString.html> &text) { // dublicate
   code
   89.   ui->CustomWidgetFirst->setLabel(text);
              // dublicate code
   90. }
               // dublicate code
   91.
   92. CustomWidgetSecond::~CustomWidgetSecond() {
   93.   delete ui;
   94. }

Would suggest to solve this problem in this way:

   1. Q_PROPERTY(QString <http://qt-project.org/doc/QString.html> label
   READ ui->CustomWidgetFirst->label WRITE ui->CustomWidget->label)

If this solution is adopted, then I have a working MOC and I can submit to
gerrit.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20140207/c906ee5b/attachment.html>


More information about the Development mailing list