Search code examples
c++qt5interactiveqtableviewstretch

QTableView Stretch Headers but keep Interactive


I am struggling triying to accomodate my QTableView to make it easy to the user.

This functions works as i really need:

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

The issue with this is that the headers are not user adjustable anymore, are completely frozzen.

I know we also have this function which allows the user to adjust the Headers, but after using it, the headers go back as if i had not used QHeaderView::Stretch before:

ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);

I have really looked for this on many websites with no luck of finding a understandable answer for my level.

Thanks!


Solution

  • I found the way to do it, first i need to set the width of each column to fit the QTableView size, and then i stretch the last section, so now each column can be resized.

      for(int c = 0;c<=4;c++){
        ui->tableView->horizontalHeader()->resizeSection(c, 150);
    
        }
    
        ui->tableView->horizontalHeader()->setStretchLastSection(true);
    

    Thanks