Results 1 to 2 of 2

Thread: Qt4 set and read header data in tableView

  1. #1
    Join Date
    Feb 2009
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Qt4 set and read header data in tableView

    I tried the Cities example in the book C++ -GUI Programming with Qt 4 first ed., and i tried to set a header data string by using the row

    cityModel.setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));

    in the main.cpp file.

    The code can be compiled and run without any complaints but the added code has not any effects.

    The code is
    Qt Code:
    1. #include <QHeaderView>
    2. #include <QTableView>
    3.  
    4. #include "citymodel.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. QStringList cities;
    11. cities << "Arvika" << "Boden" << "Eskilstuna" /*<< "Falun"
    12.   << "Filipstad" << "Halmstad" << "Helsingborg" << "Karlstad"
    13.   << "Kiruna" << "Kramfors" << "Motala" << "Sandviken"
    14.   << "Skara" << "Stockholm" << "Sundsvall" << "Trelleborg" */ ;
    15. QStringList layers;
    16. layers << "l1" << "l2" << "l3" << "l4";
    17.  
    18. CityModel cityModel;
    19. cityModel.setCities(cities);
    20. cityModel.setLayers(layers);
    21.  
    22. QTableView tableView;
    23. tableView.setModel(&cityModel);
    24. tableView.setAlternatingRowColors(true);
    25. tableView.setWindowTitle(QObject::tr("Cities"));
    26.  
    27. cityModel.setHeaderData(0, Qt::Horizontal, QObject::tr("Name")); // <------
    28.  
    29. tableView.show();
    30.  
    31. return app.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

    How should you set and read header data in this case?

    teele

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 set and read header data in tableView

    It didn't work because view will take header data from model as you set model & implemented header data in model.

    try this: you need to do all the manipulations in model only

    Qt Code:
    1. QVariant CityModel::headerData(int section, Qt::Orientation orientation, int role) const
    2. {
    3. if (role == Qt::DisplayRole)
    4. {
    5.  
    6. if((0 == section ) && (orientation == Qt::Horizontal)
    7. return QVariant("Name");
    8. else
    9. return cities[section];
    10. }
    11. return QVariant();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Thanks :-)

  3. The following user says thank you to prasad_N for this useful post:

    teele (22nd September 2015)

Similar Threads

  1. header for tableView of QStandardItemModel
    By Omid123 in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2015, 00:15
  2. tableview wordwrap header but not data columns
    By RolandHughes in forum Qt Programming
    Replies: 2
    Last Post: 28th October 2014, 01:05
  3. Change tableView vertical header
    By tinysoft in forum Newbie
    Replies: 1
    Last Post: 17th September 2011, 08:35
  4. TableView with header and footer for sums, avg and so on
    By extreme001 in forum Qt Programming
    Replies: 0
    Last Post: 18th April 2011, 20:56
  5. QT: editable tableview header issue.
    By raschko in forum Qt Programming
    Replies: 0
    Last Post: 27th February 2011, 19:15

Tags for this Thread

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.