tomicious
tomicious

Reputation: 41

Qt Designer custom plugin and dynamic properties

I am trying to write some set of custom widget as Qt Designer plugin. Some of the properties are same for all my widgets, so I want to make a base class. But there is a problem with multiple inheritnece, when base class inherits from QObject (because of Q_PROPERTY macro to make its properties editable in Qt Designer) and the custom widget needs to be child of QWidget. But I found Qt's dynamic property so base class gets the widget pointer and sets the dynamic property.

class BaseClass {
  QWidget *widget;
  BaseClass(QWidget *widget) {
    this->widget = widget;
    this->widget->setProperty("Some", 0.0);
  }
  void setSome(double some) {
    this->widget->setProperty("Some", some);
  }
...

Thats ok. I can set the dynamic property and see them and edit them in Qt Designer, all is ok. BUT when I want to read from the property something is wrong. When I am using:

this->widget->property("Some").value<double>();

Qt Designer doesn't start (?!).

And when I use this more then once

QVariant var = this->widget->property("Some");
return var.toDouble();

It's the same problem! Qt Designer fails to start with no message or other hint. I am using Qt 4.7.4 with its designer. The same proble is in Qt Creators designer (tested on 2.0.0, 2.0.1).

Am I wrong or it is Qt's issue?

Thanks for any suggestions!

Upvotes: 4

Views: 2637

Answers (1)

The QWidget passed to the constructor when creating another QWidget, is actually the parent, so if you use something as

Derived::Derived(QWidget* parent):Base(parent){

}

You will set that property to the parent instead of the child.

As Qt is intended to be used using simple inheritane when referring to QObjects, create a base class with the functions that will set the Q_PROPERTY and add some macros to define the required properties.

Hope it helps.

Upvotes: 2

Related Questions