Qt-interest Archive, June 2007
Custom Model Class
Message 1 in thread
Hi,
I'm trying to figure out how best to make my own model class but I'm
going round in circles figuring out what to inherit from and what to
reimplement.
Currently I have it inheriting from QStandardItemModel like so:
class PlayersModel : public QStandardItemModel
{
Q_OBJECT
public:
PlayersModel(QObject* parent = 0);
};
And then the constructor is set up like so:
PlayersModel::PlayersModel(QObject* parent) : QStandardItemModel(0, 2,
parent)
{
this->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
this->setHeaderData(1, Qt::Horizontal, QObject::tr("Email"));
}
But then I am aware that the user of PlayersModel could use the add
column functions which I don't want them to be able to do. I simple want
to define a list of name and email address (and maybe other things later).
Can anyone point me in the right direction of a tutorial (or just simply
help by giving me a bit of code to help) which I can use to learn this?
I've tried the model view tutorial in the Qt docs but it doesn't really
explain what I need.
Thanks so much guys!
Matt Galloway
--
[ signature omitted ]
Message 2 in thread
On 03.06.07 09:46:53, Matt Galloway wrote:
> I'm trying to figure out how best to make my own model class but I'm going
> round in circles figuring out what to inherit from and what to reimplement.
Why? I mean what are the reasons for using a custom model.
> Currently I have it inheriting from QStandardItemModel like so:
>
>
> class PlayersModel : public QStandardItemModel
Thats not really a custom model.
> {
> Q_OBJECT
> public:
> PlayersModel(QObject* parent = 0);
> };
>
> And then the constructor is set up like so:
>
> PlayersModel::PlayersModel(QObject* parent) : QStandardItemModel(0, 2, parent)
> {
> this->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
> this->setHeaderData(1, Qt::Horizontal, QObject::tr("Email"));
> }
>
>
> But then I am aware that the user of PlayersModel could use the add column
> functions which I don't want them to be able to do. I simple want to define a
> list of name and email address (and maybe other things later).
Then you want QAbstractItemModel or QAbstractTableModel (depending on
wether you have a table or tree).
> Can anyone point me in the right direction of a tutorial (or just simply help
> by giving me a bit of code to help) which I can use to learn this? I've tried
> the model view tutorial in the Qt docs but it doesn't really explain what I
> need.
What you need you have to find out yourself. What you need to do to
implement a custom model say as QAbstractTableModel subclass is
documented in that class API doc.
Andreas
--
[ signature omitted ]
Message 3 in thread
Andreas Pakulat wrote:
> On 03.06.07 09:46:53, Matt Galloway wrote:
>
>> I'm trying to figure out how best to make my own model class but I'm going
>> round in circles figuring out what to inherit from and what to reimplement.
>>
>
> Why? I mean what are the reasons for using a custom model.
>
>
>> Currently I have it inheriting from QStandardItemModel like so:
>>
>>
>> class PlayersModel : public QStandardItemModel
>>
>
> Thats not really a custom model.
>
>
>> {
>> Q_OBJECT
>> public:
>> PlayersModel(QObject* parent = 0);
>> };
>>
>> And then the constructor is set up like so:
>>
>> PlayersModel::PlayersModel(QObject* parent) : QStandardItemModel(0, 2, parent)
>> {
>> this->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
>> this->setHeaderData(1, Qt::Horizontal, QObject::tr("Email"));
>> }
>>
>>
>> But then I am aware that the user of PlayersModel could use the add column
>> functions which I don't want them to be able to do. I simple want to define a
>> list of name and email address (and maybe other things later).
>>
>
> Then you want QAbstractItemModel or QAbstractTableModel (depending on
> wether you have a table or tree).
>
>
>> Can anyone point me in the right direction of a tutorial (or just simply help
>> by giving me a bit of code to help) which I can use to learn this? I've tried
>> the model view tutorial in the Qt docs but it doesn't really explain what I
>> need.
>>
>
> What you need you have to find out yourself. What you need to do to
> implement a custom model say as QAbstractTableModel subclass is
> documented in that class API doc.
>
> Andreas
>
>
Ok, I have tried to get my head round this with no luck at the moment...
Can anyone help me with trying to figure out how to set this up?
I'm now inheriting from QAbstractTableModel and reimplementing rowCount,
columnCount, etc, but how do I store the data in the class? I have two
strings being stored for each row of the model. In a similar model I
have 2 strings and 2 colours, so should I use a QList<QVariant> to store
the information?
Argh this is messing me up, it's surely simple but I want to get the
implementation correct before I go ahead and use it everywhere!
Does anyone have an example of such a model? Surely storing information
like this is common? It's just like a list of names and corresponding
emails... much like an address book...
Any help appreciated!
Many thanks
--
[ signature omitted ]
Message 4 in thread
On 03.06.07 11:16:57, Matt Galloway wrote:
> I'm now inheriting from QAbstractTableModel and reimplementing rowCount,
> columnCount, etc, but how do I store the data in the class? I have two strings
> being stored for each row of the model. In a similar model I have 2 strings and
> 2 colours, so should I use a QList<QVariant> to store the information?
That depends on the structure of the data, if you just have two strings
that form a group and you've got a list of this group then something
like a
QList<QPair<QString, QString> >
should suffice.
> Argh this is messing me up, it's surely simple but I want to get the
> implementation correct before I go ahead and use it everywhere!
>
> Does anyone have an example of such a model? Surely storing information like
> this is common? It's just like a list of names and corresponding emails... much
> like an address book...
If you rather have a list of names and each names has multiple emails
something like
QHash<QString,QStringList>
would be more apropriate. There's however no general as to how the
internal data structure of a model looks like, you have to decide that
on a case-by-case basis depending on what your model will contain.
Andreas
--
[ signature omitted ]