Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

using QHeaderView



  • 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...


  • Lifetime Qt Champion

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



  • @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.


  • Lifetime Qt Champion

    @mzimmers said in using QHeaderView:

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

    Why do you create here a QHeaderView instance?



  • @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.)


  • Lifetime Qt Champion

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



  • @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...


Log in to reply