Results 1 to 3 of 3

Thread: headerView of the QTableView not shown

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default headerView of the QTableView not shown

    Hi,
    I have a QTableModel with 5 columns, but when I return labels for more than 4 columns the horizontal headerview isn't shown at all. Here is a simple application to highlight the problem. Save it as main.cpp and compile it using qmake -project && qmake && make

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class TableModel : public QAbstractTableModel
    4. {
    5. Q_OBJECT
    6. public:
    7. TableModel(QObject* parent = 0) : QAbstractTableModel(parent)
    8. {
    9. }
    10. protected:
    11. protected:
    12. int rowCount(const QModelIndex & parent = QModelIndex()) const
    13. {
    14. return 20;
    15. }
    16.  
    17. int columnCount(const QModelIndex & parent = QModelIndex()) const
    18. {
    19. return 5;
    20. }
    21.  
    22. QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
    23. {
    24. if (index.isValid() && role == Qt::DisplayRole) {
    25. return QString("index: %1,%2").arg(index.row()).arg(index.column());
    26. }
    27. return QVariant();
    28. }
    29.  
    30. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
    31. {
    32. if (orientation == Qt::Horizontal) {
    33. switch(section) {
    34. case 0: return QString("one");
    35. case 1: return QString("two");
    36. case 2: return QString("three");
    37. case 3: return QString("four");
    38. case 4: return QString("five");
    39. }
    40. }
    41. return QVariant();
    42. }
    43. };
    44.  
    45. int main(int argc, char** argv)
    46. {
    47. QApplication app(argc, argv);
    48. TableModel* model = new TableModel();
    49. QTableView* view = new QTableView;
    50. view->setModel(model);
    51. view->show();
    52. return app.exec();
    53. }
    54. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Any Ideas?

    Thanx in advance
    momesana

  2. #2
    Join Date
    Apr 2008
    Location
    Cluj-Napoca, Romania
    Posts
    10
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: headerView of the QTableView not shown

    The problem is, you have to emit the signal headerDataChanged(...) if you re implement this function. Just before returning the QVariant() will do just fine.

    http://doc.trolltech.com/4.1/qabstra...derDataChanged

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: headerView of the QTableView not shown

    Thanks but that's not the solution. The reason for the issue was that I didn't check for the correct role (Qt:isplayRole). Checking for the role solves the issue.

    Cheers
    momesana

Similar Threads

  1. One Model, Two Views (QTreeView and QTableView)
    By dgarrett97 in forum Newbie
    Replies: 2
    Last Post: 14th September 2009, 18:10
  2. Replies: 1
    Last Post: 9th August 2009, 15:27
  3. Replies: 2
    Last Post: 7th June 2009, 10:47
  4. QTableView in ui with model/delegate
    By tpf80 in forum Qt Programming
    Replies: 7
    Last Post: 6th November 2008, 23:00
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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
  •  
Qt is a trademark of The Qt Company.