Results 1 to 4 of 4

Thread: How to set header in Table View?

  1. #1
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Question How to set header in Table View?

    Hi guys. I met a problem when I wanted to rename my column headers. I used Table view.

    My re-implemented 'headerData' and 'setHeaderData' in tablemodel.cpp look like below:

    Qt Code:
    1. QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
    2. {
    3. if (role != Qt::DisplayRole)
    4. return QVariant();
    5.  
    6. if (orientation == Qt::Horizontal){
    7. //qDebug()<<headerdata.at(section);
    8. return headerdata.at(section);
    9. }
    10. return QVariant();
    11. }
    12.  
    13. bool TableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
    14. {
    15. if (section < headerdata.size() && role == Qt::EditRole)
    16. {
    17. headerdata.replace(section, value.toString());
    18. //qDebug()<<section;
    19. emit headerDataChanged(orientation,section,section);
    20. return true;
    21. }
    22. return false;
    23. }
    To copy to clipboard, switch view to plain text mode 

    Is there anything wrong? Or if it's not wrong, why I can't edit columns header?
    What I want is that the headers can be editable like other data, i.e. click one column header then type then press return then it will update.

    Please help me. Thanks in advance!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,139
    Thanks
    235
    Thanked 660 Times in 650 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to set header in Table View?

    You probably need to implement the EditRole role for headerData(). You are returning an empty QVariant for everything except DataRole, which is probably telling the header that the cell is not editable.

    I don't see anything that corresponds to the flags() method that applies to the header, and QHeaderView does not seem to have anything either to indicate an editable state.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    SoleyRan (31st July 2019)

  4. #3
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: How to set header in Table View?

    Thanks to d_stranz so much! I have made the header editable. But there is a new problem...

    I was using Dialog to get the text of new header, then call setHeaderData() to change the header. It looks like below:
    Screenshot (2).jpg
    Screenshot (3).jpg

    It works, but what I want is that it can be changed right in the column header, which will look like this:
    renamecolumn.gif

    I think it maybe done by changing the column header to a text edit box via a delegate, but I have no idea...

    Can anyone help? Thanks a lot

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,139
    Thanks
    235
    Thanked 660 Times in 650 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to set header in Table View?

    I think it maybe done by changing the column header to a text edit box via a delegate
    That is almost certainly what you would do. QHeaderView inherits from QAbstractItemView. The base class allows you to set a delegate for a column (QAbstractItemView::setItemDelegateForColumn() so this might work to allow you to edit the header text in place.

    For implementation, you might find some help in the Qt Model / View tutorial or from your friend Google.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 31st March 2016, 10:51
  2. adding image to header column of a table view
    By nageshvk in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2011, 04:14
  3. Replies: 5
    Last Post: 3rd April 2010, 04:07
  4. How to set two rows in a table view Horizontal header?
    By sivollu in forum Qt Programming
    Replies: 11
    Last Post: 29th April 2009, 04:57
  5. How to customize horizontal header (diagonal header view)
    By vairamuthu.g in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2008, 15:59

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.