Question

I'm trying to do something that seems like it should be very simple, but the more I look into it I wonder if it's a Qt bug.

So, I have a QTableView that has columns that can be shown/hidden as the user likes. After I initialize the table, I call a custom restoreColumns() method that hides the columns (using QTableView::hideColumn()) that the user had hidden the last time the GUI was open.

The problem then comes when the user tries to show the columns that were hidden by the user the last time the GUI was ran. The appropriate signal/slot gets called and run through but for some reason the QTableView isn't updating to display the column.

What's weird is that any column that is already displayed (was not hidden by the user the last time the GUI was ran) has no problems with getting hidden/shown.

Any thoughts? Thanks!


Here's how I initialize the table...

m_tableModel = new mytablemodel();
m_tableView = new mytableview();
m_tableView->setItemDelegate(m_tableDelegate);
m_tableView->setModel(m_tableModel);

Meat of restoreColumns() method:

for (int i=0; i<horizontalHeader()->count(); i++) {
    // load size to restore previous width
    ...
    horizontalHeader()->resizeSection(i, width); // restore width

    // load previous column position
    ...
    // restore column order
    int currentVisualIndex = horizontalHeader()->visualIndex(i);

    if (currentVisualIndex != visualIndex) 
        horizontalHeader()->moveSection(currentVisualIndex, visualIndex);

    // load previous hidden/shown state
    ...
    if (columnHidden) {
        hideColumn(i);
    } else {
        showColumn(i);
    }
}

Below is some sample code to show/hide one of the columns.

void mytableview::showAColumn(bool checked) {
    // mytableview is a subclass of qtableview
    if (checked)
        showColumn(COLUMN_A); // COLUMN_A is an enum for the column
    else
        hideColumn(COLUMN_A);
}

Which is connected to a QAction that can be accessed from the Menu and Context Menu of the QHeaderView of the QTableView.

connect(action, SIGNAL(toggled(bool)), this, SLOT(showAColumn(bool)));  
Was it helpful?

Solution

When you are loading the previous width of the hidden columns, the width that was saved was 0.
So, when resizing the column make sure that the width is greater than 0.
Do this and then the columns will show/hide as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top