QTableWidget: Make columns resizable and movable



  • Hi,

    I want to achieve a behavior on a QTableWidget just like in a file browser.
    The first section should be stretched, the next sections should be resized to their contents and the columns should be resizable (without leaving space) and movable.

    I tried the following:

        multi_list_widget(int columns, QWidget* const parent = nullptr) : QTableWidget{0, columns, parent}{
            verticalHeader()->setVisible(false);
            horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
            horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
            horizontalHeader()->setSectionsMovable(true);
            setSelectionBehavior(QTableWidget::SelectRows);
        }
    

    but it won't work as expected.
    I also tried the following:

    horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive | QHeaderView::Stretch);
    

    but this won't compile.

    How do I get a QTableWidget behaving like in a file browser or a music player?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you take into account that the logical index should exist at the time this function is called.

    See setSectionResizeMode's documentation.

    Hope it helps


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.