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.
  • P Pl45m4
    25 Jun 2024, 01:15

    @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 25 Jun 2024, 16:36 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

    C 1 Reply Last reply 25 Jun 2024, 16:47
    0
    • J jdent
      25 Jun 2024, 16:36

      @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

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 25 Jun 2024, 16:47 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 25 Jun 2024, 16:50
      0
      • C Christian Ehrlicher
        25 Jun 2024, 16:47

        @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 25 Jun 2024, 16:50 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?

        C J 2 Replies Last reply 25 Jun 2024, 17:02
        0
        • J Offline
          J Offline
          jdent
          wrote on 25 Jun 2024, 16:56 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
            25 Jun 2024, 16:50

            @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?

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 25 Jun 2024, 17:02 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 25 Jun 2024, 17:04
            0
            • C Christian Ehrlicher
              25 Jun 2024, 17:02

              @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 25 Jun 2024, 17:04 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??

              J 1 Reply Last reply 25 Jun 2024, 17:11
              0
              • J jdent
                25 Jun 2024, 17:04

                @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??

                J Online
                J Online
                JonB
                wrote on 25 Jun 2024, 17:11 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 25 Jun 2024, 17:25 last edited by
                  #11

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

                  C 1 Reply Last reply 25 Jun 2024, 17:26
                  0
                  • J jdent
                    25 Jun 2024, 17:25

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

                    C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 25 Jun 2024, 17:26 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
                      25 Jun 2024, 16:50

                      @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 25 Jun 2024, 17:29 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?

                      J 1 Reply Last reply 25 Jun 2024, 17:38
                      0
                      • J jdent
                        25 Jun 2024, 17:29

                        @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?

                        J Online
                        J Online
                        JonB
                        wrote on 25 Jun 2024, 17:38 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 25 Jun 2024, 17:53 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 25 Jun 2024, 17:56 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 25 Jun 2024, 18:07 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?

                              J 1 Reply Last reply 25 Jun 2024, 18:18
                              0
                              • J Offline
                                J Offline
                                jdent
                                wrote on 25 Jun 2024, 18:16 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
                                  25 Jun 2024, 18:07

                                  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?

                                  J Online
                                  J Online
                                  JonB
                                  wrote on 25 Jun 2024, 18:18 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 25 Jun 2024, 18:24 last edited by
                                    #20

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

                                    J 1 Reply Last reply 25 Jun 2024, 18:27
                                    0
                                    • J jdent
                                      25 Jun 2024, 18:24

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

                                      J Online
                                      J Online
                                      JonB
                                      wrote on 25 Jun 2024, 18:27 last edited by
                                      #21

                                      @jdent
                                      If you go to the doc page I referenced and read through you will find it describes exactly what you need to do and how it relates to QSortFilterProxyModel or sorting your model.

                                      The idea is to read through the documentation, search for examples and ask questions here when you have researched and have a problem.

                                      1 Reply Last reply
                                      0
                                      • J Offline
                                        J Offline
                                        jdent
                                        wrote on 25 Jun 2024, 20:10 last edited by jdent
                                        #22

                                        Ok, I implemented a subclass of QSortFilterProxyModel in the following :

                                        class SortFilterProxyModel : public QSortFilterProxyModel
                                        {
                                        	Q_OBJECT
                                        public:
                                        	SortFilterProxyModel(QObject* parent = nullptr) : QSortFilterProxyModel(parent) {}
                                        
                                        	bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override
                                        	{
                                        		QVariant left_data = sourceModel()->data(source_left);
                                        		QVariant right_data = sourceModel()->data(source_right);
                                        
                                        		switch (left_data.userType())
                                        		{
                                        		case QMetaType::QDateTime:
                                        			return left_data.toDateTime() < right_data.toDateTime();
                                        		case QMetaType::Int:
                                        			return left_data.toInt() < right_data.toInt();
                                        		case QMetaType::QString:
                                        			return left_data.toString() < right_data.toString();
                                        		default:
                                        			return QSortFilterProxyModel::lessThan(source_left, source_right);
                                        		}
                                        	}
                                        };
                                        

                                        HOWEVER, I click on the header view and nothing happens!! Why?
                                        This is with the subclass and without it!! I would expect resorting to occur in the reverse order when clicking the header but nothing changes!

                                        1 Reply Last reply
                                        0
                                        • J Offline
                                          J Offline
                                          jdent
                                          wrote on 25 Jun 2024, 20:45 last edited by jdent
                                          #23

                                          Sorting is working fine.... its the header view that does not respond to the mouse! What can be going on with the header?

                                          J 1 Reply Last reply 25 Jun 2024, 21:17
                                          0

                                          13/34

                                          25 Jun 2024, 17:29

                                          • Login

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