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. using QHeaderView

using QHeaderView

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 798 Views 1 Watching
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by mzimmers
    #1

    Hi all -

    In this ancient post, the grandmaster advises someone who wanted to do the same thing I want to do: get the columns in a QTableView to expand when the parent widget is resized.

    I think I understand the concept, but I'm having trouble connecting the dots between retrieving the header data, and setting it.

        // format the column widths.
        QVariant qv;
        QHeaderView *qhv = new QHeaderView(Qt::Horizontal);
        bool rc;
        
        for (int i = 0; i < m_nbrTableRowsVisible; ++i)
        {
            
            qv = m_modelDevice->getModel()->headerData(0, Qt::Horizontal);
    //    what do I do here?
            rc = m_modelDevice->getModel()->setHeaderData(0, Qt::Horizontal, qv);
        }
    

    Can someone clue me in? Thanks...

    jsulmJ 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      In this ancient post, the grandmaster advises someone who wanted to do the same thing I want to do: get the columns in a QTableView to expand when the parent widget is resized.

      I think I understand the concept, but I'm having trouble connecting the dots between retrieving the header data, and setting it.

          // format the column widths.
          QVariant qv;
          QHeaderView *qhv = new QHeaderView(Qt::Horizontal);
          bool rc;
          
          for (int i = 0; i < m_nbrTableRowsVisible; ++i)
          {
              
              qv = m_modelDevice->getModel()->headerData(0, Qt::Horizontal);
      //    what do I do here?
              rc = m_modelDevice->getModel()->setHeaderData(0, Qt::Horizontal, qv);
          }
      

      Can someone clue me in? Thanks...

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @mzimmers said in using QHeaderView:

      // what do I do here?

      Well, call https://doc.qt.io/qt-5/qheaderview.html#setSectionResizeMode on qv before setting it?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      mzimmersM 1 Reply Last reply
      2
      • jsulmJ jsulm

        @mzimmers said in using QHeaderView:

        // what do I do here?

        Well, call https://doc.qt.io/qt-5/qheaderview.html#setSectionResizeMode on qv before setting it?

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @jsulm ahh...thanks for the tip. That led me to do some more looking, and I discovered the qvariant_cast() method; that was a new one on me. Does this look better?

            QVariant qv;
            QHeaderView *qhv = new QHeaderView(Qt::Horizontal);
            bool rc;
        
            for (int i = 0; i < m_nbrTableRowsVisible; ++i)
            {
        
                qv = m_modelDevice->getModel()->headerData(0, Qt::Horizontal);
                qhv = qvariant_cast<QHeaderView *>(qv);
                qhv->setSectionResizeMode(QHeaderView::Stretch);
                qv = QVariant::fromValue(qhv);
                rc = m_modelDevice->getModel()->setHeaderData(0, Qt::Horizontal, qv);
            }
        
        

        Unfortunately, I can't test it because there's still something wrong with how Creator is handling my Widget class, but...that's another post in another forum.

        jsulmJ 1 Reply Last reply
        0
        • mzimmersM mzimmers

          @jsulm ahh...thanks for the tip. That led me to do some more looking, and I discovered the qvariant_cast() method; that was a new one on me. Does this look better?

              QVariant qv;
              QHeaderView *qhv = new QHeaderView(Qt::Horizontal);
              bool rc;
          
              for (int i = 0; i < m_nbrTableRowsVisible; ++i)
              {
          
                  qv = m_modelDevice->getModel()->headerData(0, Qt::Horizontal);
                  qhv = qvariant_cast<QHeaderView *>(qv);
                  qhv->setSectionResizeMode(QHeaderView::Stretch);
                  qv = QVariant::fromValue(qhv);
                  rc = m_modelDevice->getModel()->setHeaderData(0, Qt::Horizontal, qv);
              }
          
          

          Unfortunately, I can't test it because there's still something wrong with how Creator is handling my Widget class, but...that's another post in another forum.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @mzimmers said in using QHeaderView:

          QHeaderView *qhv = new QHeaderView(Qt::Horizontal);

          Why do you create here a QHeaderView instance?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          mzimmersM 1 Reply Last reply
          1
          • jsulmJ jsulm

            @mzimmers said in using QHeaderView:

            QHeaderView *qhv = new QHeaderView(Qt::Horizontal);

            Why do you create here a QHeaderView instance?

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            @jsulm good point. Better?

                QVariant qv;
                QHeaderView *qhv;
                bool rc;
            
                for (int i = 0; i < m_nbrTableRowsVisible; ++i)
                {
                    qv = m_modelDevice->getModel()->headerData(0, Qt::Horizontal);
                    qhv = qvariant_cast<QHeaderView *>(qv);
                    qhv->setSectionResizeMode(QHeaderView::Stretch);
                    qv = QVariant::fromValue(qhv);
                    rc = m_modelDevice->getModel()->setHeaderData(0, Qt::Horizontal, qv);
                }
            

            (The bool rc is just for my debugging purposes.)

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @mzimmers said in using QHeaderView:

              qhv = qvariant_cast<QHeaderView *>(qv);

              Are you serious? Why should a model return a pointer to a view??
              Take a look at the documentation of your view to see how to access the headers!

              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
              • mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

                @Christian-Ehrlicher ahh, yes. When the fog lifts, it can be a beautiful view:

                    QHeaderView *qhv;
                    qhv = ui->tableView->horizontalHeader();
                    qhv->setSectionResizeMode(QHeaderView::Stretch);
                

                Thanks for helping me get unwrapped from around the axle, Christian.

                For icing on the cake, I don't suppose there's any way to combine this feature with a "resize to contents" functionality? One of my columns (the last one) needs a little more space than the others...not sure what the best way to go about this might be.

                table.PNG

                Thanks...

                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