Originally Posted by
anda_skoa
There are a cooupl of options, maybe this one is the easiest:
Create a QObject derived class that has one Q_PROPERTY for each of your fields, e.g.
class PersonAdapter
: public QObject{
Q_OBJECT
Q_PROPERTY(int health READ health WRITE setHealth)
public:
// use this in SetTreePreferences
void setPerson(PERSON *person) { m_person = person; }
// this is the READ function described above
int health() const { return m_person->health; }
// this is the WRITE function described above
void setHealth(int health) { m_person->health = health; }
private:
PERSON *m_person;
};
class PersonAdapter : public QObject
{
Q_OBJECT
Q_PROPERTY(int health READ health WRITE setHealth)
public:
// use this in SetTreePreferences
void setPerson(PERSON *person) { m_person = person; }
// this is the READ function described above
int health() const { return m_person->health; }
// this is the WRITE function described above
void setHealth(int health) { m_person->health = health; }
private:
PERSON *m_person;
};
To copy to clipboard, switch view to plain text mode
typedef struct
{
float x;
float y;
float z;
} POSITION;
typedef struct
{
int health;
POSITION pos[ 3 ];
} PERSON;
class PersonAdapter
: public QObject{
Q_OBJECT
Q_PROPERTY(POSITION position1 READ position1 WRITE setPosition1)
Q_PROPERTY(POSITION position2 READ position2 WRITE setPosition2)
Q_PROPERTY(POSITION position3 READ position3 WRITE setPosition3)
public:
// use this in SetTreePreferences
void setPerson(PERSON *person) { m_person = person; }
// this is the READ function described above
int position1() const { return m_person->position[0]; }
int position2() const { return m_person->position[1]; }
int position3() const { return m_person->position[2]; }
// this is the WRITE function described above
void setPosition1(POSITION pos) { m_person->position[0].x = pos.x; m_person->position[0].y = pos.y; m_person->position[0].z = pos.z; }
void setPosition2(POSITION pos) { m_person->position[1].x = pos.x; m_person->position[1].y = pos.y; m_person->position[1].z = pos.z; }
void setPosition3(POSITION pos) { m_person->position[2].x = pos.x; m_person->position[2].y = pos.y; m_person->position[2].z = pos.z; }
private:
PERSON *m_person;
};
typedef struct
{
float x;
float y;
float z;
} POSITION;
typedef struct
{
int health;
POSITION pos[ 3 ];
} PERSON;
class PersonAdapter : public QObject
{
Q_OBJECT
Q_PROPERTY(POSITION position1 READ position1 WRITE setPosition1)
Q_PROPERTY(POSITION position2 READ position2 WRITE setPosition2)
Q_PROPERTY(POSITION position3 READ position3 WRITE setPosition3)
public:
// use this in SetTreePreferences
void setPerson(PERSON *person) { m_person = person; }
// this is the READ function described above
int position1() const { return m_person->position[0]; }
int position2() const { return m_person->position[1]; }
int position3() const { return m_person->position[2]; }
// this is the WRITE function described above
void setPosition1(POSITION pos) { m_person->position[0].x = pos.x; m_person->position[0].y = pos.y; m_person->position[0].z = pos.z; }
void setPosition2(POSITION pos) { m_person->position[1].x = pos.x; m_person->position[1].y = pos.y; m_person->position[1].z = pos.z; }
void setPosition3(POSITION pos) { m_person->position[2].x = pos.x; m_person->position[2].y = pos.y; m_person->position[2].z = pos.z; }
private:
PERSON *m_person;
};
To copy to clipboard, switch view to plain text mode
With this adapter class, is this how you would handle an array of another structure inside the original structure?
Originally Posted by
anda_skoa
In the slot that retrieves the new value, you need the name of the property to change
QByteArray propertyName;
// get that e.g. from a list based on the clicked index
// m_adapter is an instance of the PersonAdapter class
m_adapter->setProperty(propertyName.constData(), valueFromDialog);
QByteArray propertyName; // get that e.g. from a list based on the clicked index
// m_adapter is an instance of the PersonAdapter class
m_adapter->setProperty(propertyName.constData(), valueFromDialog);
To copy to clipboard, switch view to plain text mode
Cheers,
_
void PopulateTreeNames( )
{
treeList << "Health" << "Mana" << "Gold";
treeModel.setStringList( treeList );
ui->listView->setModel( &treeModel );
}
void SetPropertyByName( int index )
{
m_adapter->setProperty( propertyName.constData(), valueFromDialog );
}
QStringList treeList;
QStringListModel treeModel;
void PopulateTreeNames( )
{
treeList << "Health" << "Mana" << "Gold";
treeModel.setStringList( treeList );
ui->listView->setModel( &treeModel );
}
void SetPropertyByName( int index )
{
QByteArray propertyName = treeList.at( index );
m_adapter->setProperty( propertyName.constData(), valueFromDialog );
}
To copy to clipboard, switch view to plain text mode
propertyName would be the string equivalent of the index in the ListView's QStringListModel? So the Q_PROPERTY name would need to match whatever I input into the QStringList at the same index, correct?
Bookmarks