[Development] About ALIAS in Q_PROPERTY

Svetkin Mikhail mikhail.svetkin at gmail.com
Tue Mar 11 04:12:40 CET 2014


Explain:
I was creating custom widgets with the help of Qt-designer.
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.   Q_PROPERTY(QString <http://qt-project.org/doc/QString.html> value
   READ label WRITE setLabel)
   10.  public:
   11.   explicit
CustomWidgetFirst(QWidget<http://qt-project.org/doc/QWidget.html>
    *parent = 0)
   12.   ~CustomWidgetFirst();
   13.
   14.   QString <http://qt-project.org/doc/QString.html> label() const;
   15.   void setLabel(const QString<http://qt-project.org/doc/QString.html>
    &label);
   16.
   17.  protected:
   18.   void paintEvent(QPaintEvent<http://qt-project.org/doc/QPaintEvent.html>
    *event);
   19.
   20.  private:
   21.   QString <http://qt-project.org/doc/QString.html> m_label;
   22. };
   23.
   24. // custom_widget_first.cpp
   25.
   26. #include "custom_widget_first.h"
   27.
   28. CustomWidgetFirst::CustomWidgetFirst(QWidget<http://qt-project.org/doc/QWidget.html>
    *parent)
   29.   : QWidget <http://qt-project.org/doc/QWidget.html>(parent) {
   30. }
   31.
   32. QString <http://qt-project.org/doc/QString.html> CustomWidgetFirst::
   label() const {
   33.   return m_label;
   34. }
   35.
   36. void CustomWidgetFirst::setLabel(const
QString<http://qt-project.org/doc/QString.html>
    &label)  {
   37.   m_label = label;
   38. }
   39.
   40. void CustomWidgetFirst::paintEvent(QPaintEvent<http://qt-project.org/doc/QPaintEvent.html>
    *event) {
   41.   // some paint
   42. }
   43.
   44. CustomWidgetFirst::~CustomWidgetFirst() {
   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 { // duplicate code
   85.   return ui->CustomWidgetFirst->label();
    // duplicate code
   86. }
     // duplicate code
   87.
   88. void CustomWidgetSecond::setUiCustomWidgetFirstTextLabel(const
   QString <http://qt-project.org/doc/QString.html> &text) { // duplicate
   code
   89.   ui->CustomWidgetFirst->setLabel(text);
              // duplicate code
   90. }
               // duplicate code
   91.
   92. CustomWidgetSecond::~CustomWidgetSecond() {
   93.   delete ui;
   94. }


Would suggest to solve this problem in this way(The code marked as
duplicate does not need anymore.):

   1. Q_PROPERTY(QString <http://qt-project.org/doc/QString.html> label
   ALIAS CustomWidgetFirst ui->custom_widget_first_obj->value )  // where
   "value" property name

The ALIAS attribute indicates that the property will be reference on another
property. If ALIAS property has a signal, you need to declare the same
signal
because moc will automatically  generate connect(aliasObject, aliasSignal,
yourClassObject, aliasSignal)
and you will only need call initAliasNotify function in the class construct.


2014-03-10 22:43 GMT+06:00 Giuseppe D'Angelo <dangelog at gmail.com>:

> Can you please explain what this feature is about, why do you think
> it's useful, how it's supposed to be used, etc.?
>
> On 10 March 2014 17:30, mikhail.svetkin at gmail.com
> <mikhail.svetkin at gmail.com> wrote:
> > Hello, I would like to clarify wherein the complexity
> > (https://codereview.qt-project.org/#change,80412)?
> > Ready to listen to any suggestions and implement them.
> >
> > --
> > Mikhail Svetkin
> > _______________________________________________
> > Development mailing list
> > Development at qt-project.org
> > http://lists.qt-project.org/mailman/listinfo/development
>
>
>
> --
> Giuseppe D'Angelo
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20140311/a7d42363/attachment.html>


More information about the Development mailing list