Hiding a table advances the selected item



  • I've tried this with both a QTableView and a QTableWidget. I want to make my table invisible and then later show it again. If I've selected a table item, and at some point I call hide() on the table, the table deselects the currently selected item and selects the next item. This will be shown visually when I call show() on the table.

    I can manually override this behavior by manually re-selecting the previous item after calling hide(), but this becomes an unfixable mess when multiple items had been selected. No amount of fiddling with clearSelection() and selectRow() seems to be able to restore the original multi-item selection in that situation.

    Long story short, whenever I call hide() on my table, the current selection advances. Is it possible to avoid this strange behavior?


  • Lifetime Qt Champion

    Hi,

    Sounds like a pretty strange bug ! Which version of Qt are you using ? Can you reproduce this with a minimal compilable example ?



  • I'm using PyQt4 on Windows, version 4.8.5. (Update: I also tried the latest version of PyQt, and the latest version of PySide -- same problem).

    This minimalistic code snippet demonstrates the problem. It waits 5 seconds, giving the user time to select a table item, then hides and immediately afterwards shows the table. (It doesn't matter if there's a delay between hiding and showing.)

    [code]
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *

    app = QApplication([])
    window = QMainWindow()
    window.resize(300, 200)

    table = QTableWidget(window)
    table.resize(300, 200)

    table.setRowCount(4)
    table.setColumnCount(2)

    Fill the 4x2 table with items saying "data"

    for r in range(4):
    for c in range(2):
    item = QTableWidgetItem("data")
    table.setItem(r, c, item)

    This function will be called 5 seconds after the program starts

    def DelayedAction():
    table.hide()
    table.show()

    QTimer.singleShot(5000, DelayedAction)

    window.show()
    app.exec_()
    [/code]


Log in to reply
 

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