Does Qt controls have a property to store data that is closely associated with the control?
-
Not quite what I am looking for. I was hoping for something like this...
// Data Associated Class
class MyObject : public QObject
{
public:
QString Name;
QString Address;
int value;QString ToHex(int value); QString ToBinary(int value);
}
//Main form calling function
void OnButtonClicked()
{
MyObject* obj = (MyObject*)txt_Value->Tag; // Does Qt contain something like this objectif (HexFlg) txt_Value.SetText(obj->ToHex((int)txt_Value->Text()); else txt_Value.SetText(obj->ToBinary((int)txt_Value->Text());
}
Thanks!
-
Sorry, I did not realize I can do the following....
@// Data Associated Class
class MyObject : public QObject
{
public: // Variables
QString Name;
QString Address;
int value;// public methods QString ToHex(int value); QString ToBinary(int value);
}
//Main form calling function
void OnButtonClicked()
{
// assign object to the control associated data (Does Qt control contain such object???)
MyObject* obj = (MyObject*)txt_Value->Tag;if (HexFlg) txt_Value.SetText(obj->ToHex((int)txt_Value->Text()); else txt_Value.SetText(obj->ToBinary((int)txt_Value->Text());
} @
-
-
What are Name and Address in MyObject for? They are not used!
What type/class is txt_Value?
Do you really want to just cast any text you get with txt_Value->Text() to an int without checking if that's possible?
From what I guess it to be, I'd extend the Qt Widget and handle the signals internally and put the computed result into a Q_PROPERTY that can read afterwards.
-
you can find more info "here":http://doc.trolltech.com/4.7-snapshot/properties.html#qt-s-property-system
-
The code snippet is just a sample of what I would like to do. I have not added any error checking or such.
bq. What type/class is txt_Value?
txt_Value is a lineedit widget.
My main objective is to have all my widget assigned to its associated data class in the constructor so that I can cast the widget generic QObject property (if it exist) when a certain signal is received. I understand that I can create a custom class inherited from any of Qt widget and add a Q_PROPERTY that I can set. I am just wondering if Qt´s basic controls such as combobox, line edit, label, etc.. already contain this and that I have overlooked something.
-
Each Qt widget is derived from QObject. That means you can use the @setProperty()@ function of each widget to dynamically assign any data to the widget. Later you can read out the data with the @property()@ function of the widget.
You wil find more information here "http://doc.qt.nokia.com/4.7/qobject.html#setProperty":http://doc.qt.nokia.com/4.7/qobject.html#setProperty
-
Some of the Qt Widgets support already associating some data with it (e.g. QComboBox can associate a QVariant value to each of the choices you have, see "http://doc.qt.nokia.com/4.7/qcombobox.html#addItem":http://doc.qt.nokia.com/4.7/qcombobox.html#addItem