Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to rename column headers in Table View?
QtWS25 Last Chance

How to rename column headers in Table View?

Scheduled Pinned Locked Moved Unsolved General and Desktop
headerheader datatable viewset header data
6 Posts 3 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SoleyRan
    wrote on 29 Jul 2019, 21:27 last edited by VRonin
    #1

    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:

    QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if (role != Qt::DisplayRole)
            return QVariant();
     
       if (orientation == Qt::Horizontal){
            //qDebug()<<headerdata.at(section);
            return headerdata.at(section);
        }
        return QVariant();
    }
     
    bool TableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
    {
        if (section < headerdata.size() && role == Qt::EditRole)
        {
            headerdata.replace(section, value.toString());
            //qDebug()<<section;
            emit headerDataChanged(orientation,section,section);
            return true;
        }
        return false;
    }
    

    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! :)

    V 1 Reply Last reply 30 Jul 2019, 08:08
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Jul 2019, 21:35 last edited by
      #2

      Hi and welcome to devnet,

      What model are you subclassing ?
      What version of Qt are you using ?
      On what OS ?

      Can you provide a minimal compilable example that shows that behaviour ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S SoleyRan
        29 Jul 2019, 21:27

        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:

        QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
        {
            if (role != Qt::DisplayRole)
                return QVariant();
         
           if (orientation == Qt::Horizontal){
                //qDebug()<<headerdata.at(section);
                return headerdata.at(section);
            }
            return QVariant();
        }
         
        bool TableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
        {
            if (section < headerdata.size() && role == Qt::EditRole)
            {
                headerdata.replace(section, value.toString());
                //qDebug()<<section;
                emit headerDataChanged(orientation,section,section);
                return true;
            }
            return false;
        }
        

        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! :)

        V Offline
        V Offline
        VRonin
        wrote on 30 Jul 2019, 08:08 last edited by
        #3

        @soleyran said in How to rename column headers in Table View?:

        Is there anything wrong?

        No

        why I can't edit columns header?

        Because QHeaderView doesn't have an "edit mode". It's a feature you have to implement yourself

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        S 1 Reply Last reply 31 Jul 2019, 20:30
        3
        • V VRonin
          30 Jul 2019, 08:08

          @soleyran said in How to rename column headers in Table View?:

          Is there anything wrong?

          No

          why I can't edit columns header?

          Because QHeaderView doesn't have an "edit mode". It's a feature you have to implement yourself

          S Offline
          S Offline
          SoleyRan
          wrote on 31 Jul 2019, 20:30 last edited by
          #4

          @vronin Thank you! You are right, I made it after implementing myself. But I have another question coming... I post it under this topic, please have a look :)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SoleyRan
            wrote on 31 Jul 2019, 20:55 last edited by
            #5

            I was using Dialog to get the text of new header, then call setHeaderData() to change the header. The process was Right click on a header->Rename header->Open a dialog. It looks like below:
            0_1564605120383_Screenshot (2).png
            0_1564605125995_Screenshot (3).png

            It works, but what I want is that it can be changed right in the column header, which will look like this:
            0_1564605141029_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 :)

            1 Reply Last reply
            0
            • V Offline
              V Offline
              VRonin
              wrote on 1 Aug 2019, 08:28 last edited by
              #6

              QHeaderView does not use delegates unfortunately. The solution I'd suggest is to have a QLineEdit widget spawn on top of the header to do the editing rather than spawning a dialog

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              3

              1/6

              29 Jul 2019, 21:27

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved