PDA

View Full Version : Q_PROPERTY with binding to designer



Big_Stone88
15th April 2016, 07:57
Hello to all:-)

I have a question about the Q_PROPERTY macro.
In Qt-documentation
(http://doc.qt.io/qt-4.8/properties.html#requirements-for-declaring-properties),
they tell about the "DISIGNABLE"-attribute:


The DESIGNABLE attribute indicates whether the property should be visible in the property editor of GUI design tool (e.g., Qt Designer). Most properties are DESIGNABLE (default true). Instead of true or false, you can specify a boolean member function.

Now, i simply built a small test-widget with a label and a Pushbutton. In the code, i have a QString-property "smallText"

.h-file

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT
Q_PROPERTY(QString smallText READ smallText WRITE setSmallText NOTIFY smallTextChanged)

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

QString smallText() const
{
return m_smallText;
}

public slots:
void setSmallText(QString smallText);
{
if (m_smallText == smallText)
return;

m_smallText = smallText;
emit smallTextChanged(smallText);
}
signals:
void smallTextChanged(QString smallText);

private slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;
QString m_smallText;
};

when i click on the Pushbutton, the Property "smallText" is changed, but where can I tell the label to change?
Of course, I could move the definition of "setSmallText" to the *.cpp-file and add
ui->label->setText(smallText);, but is there any possibility to use the the "DESIGNABLE"-attribute?

I unfortunately have to use Qt4.5.2 with Qt-Creator
Thanks for any help:)
Robby

anda_skoa
15th April 2016, 10:27
If you want "ui->label" to change when smallText is changed, simply connect smallTextChanged() to the label's setText() slot.

Cheers,
_

Big_Stone88
15th April 2016, 12:05
Thanks for your answer:)

Yeah.....

sure, that would be a simple solution...:o
But what is the function of the mentioned "DISIGNABLE"-attribute then?

Robby

anda_skoa
15th April 2016, 13:32
As the documentation says: properties that are marked as designable (most of them), will be displayed in the QtDesigner's property editor.

Cheers,
_

Big_Stone88
18th April 2016, 06:35
What the documentation doesn't give away:
WHERE will it be displayed in the QtDesigner's property editor?

Robby

anda_skoa
18th April 2016, 09:26
I am not sure what you mean.

The property editor iterates over the properties and displays them grouped by the class they are declared in, starting with QObject, ending with the actual class itself.

E.g. if you look at the properties of a QPushButton, the first section has the properties of QObject, then QWidget, then QAbstractButton, then QPushButton.

So if you derive from QPushButton and add another property, it will be in its own section, following the QPushButton section.

Cheers,
_

Big_Stone88
18th April 2016, 14:18
That's exactly my problem:
My ui is a
mainWindow : QMainWindow
when I select the mainWindow in the object-tree, there are only three categories in the property editor: QObject, QWidget and QMainWindow. There is no Category as the name of my class "mainWindow" (see following pic).
11898
even if I build two new classes (Form1 : QWidget AND Form2 : Form1) and i set a Q_Property with the name smallText in Form1, i see in the property-editor of "Form2" neither a category "Form1" nor a property "smallText" in any other category.

What am i doing wrong?
Thanks for your help:)

Robby

anda_skoa
18th April 2016, 14:36
That's exactly my problem:
My ui is a
mainWindow : QMainWindow
when I select the mainWindow in the object-tree, there are only three categories in the property editor: QObject, QWidget and QMainWindow. There is no Category as the name of my class "mainWindow" (see following pic).

The type is QMainWindow, as you can see in the second column in the object tree view.




even if I build two new classes (Form1 : QWidget AND Form2 : Form1) and i set a Q_Property with the name smallText in Form1, i see in the property-editor of "Form2" neither a category "Form1" nor a property "smallText" in any other category.

Does the designer plugin that provides "Form1" create instances of Form1 or maybe just QWidget?
Does Form1 have the Q_OBJECT macro additional to the Q_PROPERTY macro?

Cheers,
_