Results 1 to 7 of 7

Thread: QTableView header difficulties

  1. #1
    Join Date
    Feb 2007
    Posts
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView header difficulties

    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:

    Qt Code:
    1. QString lbl = "Some Text";
    2. QStringList my_list;
    3. my_list << "one" << "two" << "three";
    4. QStringListModel *pModel = new QStringListModel(my_list);
    5. pModel->setHeaderData(0, Qt::Horizontal, lbl);
    6. ui.inittabTable->setModel(pModel);
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView header difficulties

    Quote Originally Posted by croftj View Post
    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:

    Qt Code:
    1. QString lbl = "Some Text";
    2. QStringList my_list;
    3. my_list << "one" << "two" << "three";
    4. QStringListModel *pModel = new QStringListModel(my_list);
    5. pModel->setHeaderData(0, Qt::Horizontal, lbl);
    6. ui.inittabTable->setModel(pModel);
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView header difficulties

    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.
    J-P Nurmi

  4. #4
    Join Date
    Feb 2007
    Posts
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: QTableView header difficulties

    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

    Quote Originally Posted by jpn View Post
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView header difficulties

    Quote Originally Posted by croftj View Post
    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!"

  6. #6
    Join Date
    Feb 2007
    Posts
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: QTableView header difficulties

    Quote Originally Posted by wysota View Post
    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView header difficulties

    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.

Similar Threads

  1. Advanced QTableView
    By jpujolf in forum Qt Programming
    Replies: 13
    Last Post: 17th April 2010, 09:15
  2. QTableView currentChanged <> selecting header
    By Everall in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2009, 08:24
  3. QTableView header dragging
    By cmaxb in forum Qt Programming
    Replies: 1
    Last Post: 9th December 2006, 12:11
  4. QTableView header in corner
    By danstr in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2006, 20:16
  5. QTableView: disable row header
    By mattie in forum Qt Programming
    Replies: 6
    Last Post: 8th March 2006, 12:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.