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 implement setHeaderData in a subclass of QAbstractTableModel?

How to implement setHeaderData in a subclass of QAbstractTableModel?

Scheduled Pinned Locked Moved Solved General and Desktop
qtablemodeltable view
34 Posts 4 Posters 3.5k 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.
  • J Offline
    J Offline
    jdent
    wrote on last edited by jdent
    #1

    Hi,

    I have a subclass of QAbstractTableModel for which I am implementing setHeaderData but it is not doing anything (I don't see the header changed)

        bool ok = model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
        ok = model->setHeaderData(1, Qt::Horizontal, QObject::tr("Amount"));
    

    What do I need to do to show the header titles on the QTableView?

    Currently my implementation of the function is:

    	bool setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role) override
    	{
    		headerDataChanged(orientation, section, section);  // emit signal!
    		return true;
    	}
    

    Regards,
    Juan

    Pl45m4P Christian EhrlicherC 2 Replies Last reply
    0
    • J Offline
      J Offline
      jdent
      wrote on last edited by
      #34

      Finally: I removed the call to a new QHeaderView and the header now works! -- it responds to mouse clicks!!
      Thank you!!

      1 Reply Last reply
      0
      • J jdent

        Hi,

        I have a subclass of QAbstractTableModel for which I am implementing setHeaderData but it is not doing anything (I don't see the header changed)

            bool ok = model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
            ok = model->setHeaderData(1, Qt::Horizontal, QObject::tr("Amount"));
        

        What do I need to do to show the header titles on the QTableView?

        Currently my implementation of the function is:

        	bool setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role) override
        	{
        		headerDataChanged(orientation, section, section);  // emit signal!
        		return true;
        	}
        

        Regards,
        Juan

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #2

        @jdent said in How to implement setHeaderData in a subclass of QAbstractTableModel?:

        (I don't see the header changed)

        How is your headerData() implemented?
        Does your header have DisplayRole?


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        J 1 Reply Last reply
        1
        • J jdent

          Hi,

          I have a subclass of QAbstractTableModel for which I am implementing setHeaderData but it is not doing anything (I don't see the header changed)

              bool ok = model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
              ok = model->setHeaderData(1, Qt::Horizontal, QObject::tr("Amount"));
          

          What do I need to do to show the header titles on the QTableView?

          Currently my implementation of the function is:

          	bool setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role) override
          	{
          		headerDataChanged(orientation, section, section);  // emit signal!
          		return true;
          	}
          

          Regards,
          Juan

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @jdent said in How to implement setHeaderData in a subclass of QAbstractTableModel?:

          bool setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role)

          Don't you think you should store (and latert return within headerData()) the values et somewhere?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • Pl45m4P Pl45m4

            @jdent said in How to implement setHeaderData in a subclass of QAbstractTableModel?:

            (I don't see the header changed)

            How is your headerData() implemented?
            Does your header have DisplayRole?

            J Offline
            J Offline
            jdent
            wrote on last edited by
            #4

            @Pl45m4 my headerData() is currently:

            QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
            {
            	if (orientation == Qt::Horizontal)
            	{
            		if (role == Qt::DisplayRole)
            			return QVariant{ tr("Num %1").arg(section) };
            		return "?";
            	}
            	return "---";
            }
            

            I know it isn't correct... please some help.

            Regards,
            Juan

            Christian EhrlicherC 1 Reply Last reply
            0
            • J jdent

              @Pl45m4 my headerData() is currently:

              QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
              {
              	if (orientation == Qt::Horizontal)
              	{
              		if (role == Qt::DisplayRole)
              			return QVariant{ tr("Num %1").arg(section) };
              		return "?";
              	}
              	return "---";
              }
              

              I know it isn't correct... please some help.

              Regards,
              Juan

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @jdent said in How to implement setHeaderData in a subclass of QAbstractTableModel?:

              I know it isn't correct... please some help.

              As I already set - you should return the data which was set through setHeaderData() - how should it work otherwise?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              J 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @jdent said in How to implement setHeaderData in a subclass of QAbstractTableModel?:

                I know it isn't correct... please some help.

                As I already set - you should return the data which was set through setHeaderData() - how should it work otherwise?

                J Offline
                J Offline
                jdent
                wrote on last edited by
                #6

                @Christian-Ehrlicher but what should I return when role is Qt::DisplayRole and when role is otherwise?
                Just imagine for now - to simplify the example - that I have no setHeaderData() overload and that I just want "Num 0" for column 0 and "Num 1" for column 1. What should I return for each value of role?

                Christian EhrlicherC J 2 Replies Last reply
                0
                • J Offline
                  J Offline
                  jdent
                  wrote on last edited by
                  #7

                  what do I need to call in QTableView to display the header of the model?

                  1 Reply Last reply
                  0
                  • J jdent

                    @Christian-Ehrlicher but what should I return when role is Qt::DisplayRole and when role is otherwise?
                    Just imagine for now - to simplify the example - that I have no setHeaderData() overload and that I just want "Num 0" for column 0 and "Num 1" for column 1. What should I return for each value of role?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #8

                    @jdent said in How to implement setHeaderData in a subclass of QAbstractTableModel?:

                    I just want "Num 0" for column 0 and "Num 1" for column 1. What should I return for each value of role?

                    You already return this.

                    what do I need to call in QTableView to display the header of the model?

                    Nothing - except you've hidden the header by yourself.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    J 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @jdent said in How to implement setHeaderData in a subclass of QAbstractTableModel?:

                      I just want "Num 0" for column 0 and "Num 1" for column 1. What should I return for each value of role?

                      You already return this.

                      what do I need to call in QTableView to display the header of the model?

                      Nothing - except you've hidden the header by yourself.

                      J Offline
                      J Offline
                      jdent
                      wrote on last edited by
                      #9

                      @Christian-Ehrlicher "You already return this." No, I don't see any header!!

                      "what do I need to call in QTableView to display the header of the model?

                      Nothing - except you've hidden the header by yourself." What do you mean, I have hidden the header??

                      JonBJ 1 Reply Last reply
                      0
                      • J jdent

                        @Christian-Ehrlicher "You already return this." No, I don't see any header!!

                        "what do I need to call in QTableView to display the header of the model?

                        Nothing - except you've hidden the header by yourself." What do you mean, I have hidden the header??

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #10

                        @jdent
                        Your code uses setHeaderData() to explicitly set some header.
                        From that method you need to save/store that value. Somewhere in your subclass.
                        Then your header() method needs to retrieve that and return it.
                        This is what a model like, say, QStandardItemModel will do for you.
                        When you use your own QAbstractTableModel you have to implement this. In your own member variable structure. For all roles you wish to allow the caller to store data in header.
                        If you do not need to allow callers to set the header data, e.g. you can calculate what you want, then you don't need a setHeaderData() or storage, you can just write header() to compute and return values.
                        It's the same principle as what you do with setData() and data() in your subclassed QAbstractTableModel, only done separately for the header data.

                        1 Reply Last reply
                        1
                        • J Offline
                          J Offline
                          jdent
                          wrote on last edited by
                          #11

                          QAbstractTableModel has no header() method nor its superclass!!

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • J jdent

                            QAbstractTableModel has no header() method nor its superclass!!

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @jdent said in How to implement setHeaderData in a subclass of QAbstractTableModel?:

                            QAbstractTableModel has no header() method nor its superclass!!

                            It's a typo - it's headerData() as you already found out...

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            0
                            • J jdent

                              @Christian-Ehrlicher but what should I return when role is Qt::DisplayRole and when role is otherwise?
                              Just imagine for now - to simplify the example - that I have no setHeaderData() overload and that I just want "Num 0" for column 0 and "Num 1" for column 1. What should I return for each value of role?

                              J Offline
                              J Offline
                              jdent
                              wrote on last edited by jdent
                              #13

                              @jdent I am needing to repeat myself because my question goes unanswered. please help!
                              What is wrong with my headerData()? The problem is I cannot see the header of my QTableView! What am I doing wrong?

                              JonBJ 1 Reply Last reply
                              0
                              • J jdent

                                @jdent I am needing to repeat myself because my question goes unanswered. please help!
                                What is wrong with my headerData()? The problem is I cannot see the header of my QTableView! What am I doing wrong?

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #14

                                @jdent
                                Start by correcting your code, your last 2 return statements are not acceptable for any role.

                                	if (orientation == Qt::Horizontal)
                                	{
                                		if (role == Qt::DisplayRole)
                                			return QVariant{ tr("Num %1").arg(section) };
                                		return QVariant();
                                	}
                                	return QVariant();
                                

                                Then confirm with a breakpoint/debug statement that your headerData() is getting called, and report back.

                                I assume you have a QHeaderView in your QTableView to see the headers?

                                1 Reply Last reply
                                0
                                • J Offline
                                  J Offline
                                  jdent
                                  wrote on last edited by
                                  #15

                                  ok thanks
                                  I get Qt::Vertical with role 13, 6, 0, 1
                                  then I get Qt::Horizontal with roles 13, 6, 0, 1
                                  I did not have a QHeaderView!! Now I have:

                                      ui.tableView->setHorizontalHeader(new QHeaderView{ Qt::Horizontal });
                                  

                                  and I see a header now!

                                  1 Reply Last reply
                                  0
                                  • J Offline
                                    J Offline
                                    jdent
                                    wrote on last edited by
                                    #16

                                    But now I cannot sort by column!!
                                    and I have

                                        VectorModel* model = new VectorModel{ std::unique_ptr<AbstractCollection>(new PersistentCollection{ storage(), select(columns(&Claim::id, &Claim::amount))})};
                                        QSortFilterProxyModel* proxy = new QSortFilterProxyModel{ this };
                                        proxy->setSourceModel(model);
                                        ui.tableView->setModel(proxy);
                                        ui.tableView->setHorizontalHeader(new QHeaderView{ Qt::Horizontal });
                                        ui.tableView->setSortingEnabled(true);
                                        ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);
                                    
                                    1 Reply Last reply
                                    0
                                    • J Offline
                                      J Offline
                                      jdent
                                      wrote on last edited by
                                      #17

                                      Anyway I needed to modify sorting: how do I take control of it? For instance if the column has text then alphanumerical sorting must be done and if it is numeric, then the text in the cell must be converted to numeric and the order set numerically! How is this done?

                                      JonBJ 1 Reply Last reply
                                      0
                                      • J Offline
                                        J Offline
                                        jdent
                                        wrote on last edited by
                                        #18

                                        How does one cell's value compare with another? I want to implement intelligent sorting!

                                        1 Reply Last reply
                                        0
                                        • J jdent

                                          Anyway I needed to modify sorting: how do I take control of it? For instance if the column has text then alphanumerical sorting must be done and if it is numeric, then the text in the cell must be converted to numeric and the order set numerically! How is this done?

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #19

                                          @jdent
                                          You should use a QSortFilterProxyModel for displaying your model sorted.

                                          treeView = new QTreeView;
                                          model = new YourModel;
                                          proxyModel = new QSortFilterProxyModel(this);
                                          proxyModel->setSourceModel(model);
                                          treeview->setModel(proxyModel);
                                          

                                          Then you do sorting (and optionally filtering too) in the proxy model code.

                                          1 Reply Last reply
                                          0
                                          • J Offline
                                            J Offline
                                            jdent
                                            wrote on last edited by
                                            #20

                                            I have this but when I click the header nothing happens!

                                            JonBJ 1 Reply Last reply
                                            0

                                            • Login

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