Search code examples
c++qtqtablewidget

How to resize QTableWidget columns


I have a MainWindow with a QToolbar, QWidget and a QTabWidget. The layout is "Grid". However, my window is resizeable and since I have a layout it works well. But there is one problem, in my QTabWidget I have a QTableWidget with two columns (layout is also "Grid"). If I resize my whole window, the QTableWidget resizes but not the columns.

For example, whenever I resize my window, my QTabWidget resizes and the QTableWidget in it too. Only the columns in my QTableWidget won't.

How can I resize them if my QTableWidget resizes?


Solution

    1. Change the ResizeMode of the QHeaderView. For example, use:

    horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
    

    to make the first column resize so the QTableWidget is always full.


    1. Override the resizeEvent and set the widths of each column yourself when the QTableWidget has been resized.