Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

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

    General and Desktop
    qabstracttablem
    3
    7
    5440
    Loading More Posts
    • 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
      Lars_-. last edited by Lars_-.

      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 Reply Quote 0
      • V
        VRonin last edited by

        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 Reply Quote 0
        • L
          Lars_-. @VRonin last edited by

          @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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 1
            • L
              Lars_-. @SGaist last edited by Lars_-.

              @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 Reply Quote 0
              • V
                VRonin last edited by

                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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • First post
                    Last post