PDA

View Full Version : QTableView header difficulties



croftj
29th January 2008, 14:56
Hi Yall,

I'm using a QTableView with a QStringListModel and can't seem to set the label text for the column in the table view. Heres the code:



QString lbl = "Some Text";
QStringList my_list;
my_list << "one" << "two" << "three";
QStringListModel *pModel = new QStringListModel(my_list);
pModel->setHeaderData(0, Qt::Horizontal, lbl);
ui.inittabTable->setModel(pModel);


Where am I going wrong? No matter how I try, the header is always a "1".

EricF
29th January 2008, 15:06
Hi Yall,

I'm using a QTableView with a QStringListModel and can't seem to set the label text for the column in the table view. Heres the code:



QString lbl = "Some Text";
QStringList my_list;
my_list << "one" << "two" << "three";
QStringListModel *pModel = new QStringListModel(my_list);
pModel->setHeaderData(0, Qt::Horizontal, lbl);
ui.inittabTable->setModel(pModel);


Where am I going wrong? No matter how I try, the header is always a "1".

setHeaderData has a fourth argument which is role. By default it is Qt::EditRole. You should pass Qt:: DisplayRole because this the role used to display your header.

jpn
29th January 2008, 15:12
I'm sorry to tell that QStringListModel is a very simple model interface wrapper around given string list. It is not capable of holding separate header data. You'd have to subclass and store header data yourself. Or switch to QStandardItemModel.

croftj
29th January 2008, 16:01
I was afraid this would be the answer. Mind you, to me it seems silly that any model, no matter how simple it is, would not allow you to set the header displayed to the user. I guess even Qt has it's warts. Fortunately they are few.

Thanks,
Joe


I'm sorry to tell that QStringListModel is a very simple model interface wrapper around given string list. It is not capable of holding separate header data. You'd have to subclass and store header data yourself. Or switch to QStandardItemModel.

wysota
29th January 2008, 17:33
I was afraid this would be the answer. Mind you, to me it seems silly that any model, no matter how simple it is, would not allow you to set the header displayed to the user.
It would be an overkill. QStringListModel is meant to be used with QListView which doesn't show headers.

If you need headers, subclass the model and reimplement headerData(). Currently your approach is "man, that tcp socket sucks - it can't even make coffee!"

croftj
30th January 2008, 14:20
It would be an overkill. QStringListModel is meant to be used with QListView which doesn't show headers.

If you need headers, subclass the model and reimplement headerData(). Currently your approach is "man, that tcp socket sucks - it can't even make coffee!"

Well, I wouldn't go as far as use that wording. Better wording would be, gee this socket stuff sucks, it won't let me perform icmp requests (disclaimer: I used this as an example. I don't know if the socket classes actually let you do icmp or not).

In defense of overkill, from reading all that I have about Model/View programming, the point is that you make models and you make views with the point that you can display the same data set in various ways using different views. With that in mind, I would think that a model should be written with at least the basic functionality to have it work with the various views.

With that said, I will reimplement the model to show the header. :)

wysota
30th January 2008, 20:58
In general you wouldn't use headers for one column "lists" the same way as you wouldn't title rows with numbers if you only have one row (I mean you wouldn't make it a list at all). There are some design rules that should be followed and that's a perfectly valid argument in favour of the string list model not having headers. I don't say this is the case - I'd say simplicity was the crucial argument here.