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. QAbstractTableModel, headerData() never gets called, how to set headers properly?

QAbstractTableModel, headerData() never gets called, how to set headers properly?

Scheduled Pinned Locked Moved General and Desktop
qabstracttablem
7 Posts 3 Posters 6.6k 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.
  • L Offline
    L Offline
    Lars_-.
    wrote on last edited by Lars_-.
    #1

    I implemented an QAbstractTableModel to show a huge data set in a QTableView. I wanted to set the column headers by returning the desired QString in headerData()
    To my suprise, headerData() never gets called at all.

    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole)
    	{ throw std::exception(); }
    

    This never throws.
    I dont do anything with my QTableView, only setModel(), and my Model doesn't do anything but reimplenting the three necessary methods rowCount(), columnCount(), and data().

    What's the purpose of reimplementing headerData()? How do i set headers properly in QAbstractTableModel?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      When you instanciate the module, do you add the columns?
      if columnCount() returns 0 headerData() will never be called

      "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

      L 1 Reply Last reply
      0
      • VRoninV VRonin

        When you instanciate the module, do you add the columns?
        if columnCount() returns 0 headerData() will never be called

        L Offline
        L Offline
        Lars_-.
        wrote on last edited by
        #3

        @VRonin
        Thanks for respone. My column count is implemented like this:

        	int columnCount(const QModelIndex& parent = QModelIndex()) const
        	{
        		return parent.isValid() ? 0 : m_tableData.GetColumnCount();
        	}
        

        I'm not sure about the " parent.isValid() ? 0 : " part, i took it from another example how to implement a subclass of QAbstractTableModel. I'm not sure what it does, and if it's necessary. It might be the problem.

        When i call "setModel", "m_tableData.GetColumnCount()" is non-zero. Also my table gets filled with data properly, so columncount can't be 0.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          Unless you made a typo here, your headerData function has the wrong signature, it's a const method

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

          L 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi and welcome to devnet,

            Unless you made a typo here, your headerData function has the wrong signature, it's a const method

            L Offline
            L Offline
            Lars_-.
            wrote on last edited by Lars_-.
            #5

            @SGaist

            Damn, never trust examples on the internet.

            Thanks, problem solved.

            Is there a way to mark a thread as "solved"?

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              if you are ok with C++11, using the override keyword in the declaration prevents this type of mistakes

              "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
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Qt provides Q_DECL_OVERRIDE which extends to the override keyword if C++11 is used and to nothing otherwise

                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

                • Login

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