Important: Please read the Qt Code of Conduct -

Update selected Item(index) after InternalMove in listview

  • I have two listviews, the left shows parent items and the right it's parents children.
    I would like the the children to be able to be rearranged with setDragDropMode(QAbstractItemView.InternalMove). This seems to work as expected. The issue is if I have an item selected and move it, the original row still stays selected. How can I update the moved index to the listview current item? I would expect the selection to move with the InternalMove behavoir.

    I've tried to implement the dropEvent() on the listview, but that was causing duplicate items. when I was using setCurrentIndex().

    I tried to catch the insertedRow signal and try the same thing with similar results. I guess I dont fully understand selections yet. I see the listview has a selectionModel, i have tried to something like
    self.selectionModel().setCurrentIndex(index, QtCore.QItemSelectionModel.Select)
    same results.

    Any help greatly appreciated!

    import sys
    from PySide2.QtCore import Qt, QSize
    from PySide2.QtWidgets import QMainWindow, QApplication, QStyledItemDelegate, QListView, QHBoxLayout, QWidget, QAbstractItemView,QStyle
    from PySide2.QtGui import QStandardItemModel, QStandardItem, QColor, QFont, QFontMetrics
    class CustomDelegate(QStyledItemDelegate):
        def __init__(self, parent=None):
            super(CustomDelegate, self).__init__(parent)
            self.text_title = QColor(109, 210, 240)
            self.text_description = QColor(120, 120, 120)
            self.cell_default = QColor(250, 250, 250)
        def paint(self, painter, option, index):
            current_item = index.model().itemFromIndex(index)
            value = current_item.text()
            x = option.rect.x()
            y = option.rect.y()
            h = option.rect.height()
            w = option.rect.width()
            if option.state & QStyle.State_Selected:
                painter.fillRect(option.rect, self.text_description)
                painter.drawText(x+2, y+h-4, value)
            elif option.state & QStyle.State_MouseOver:
                painter.fillRect(option.rect, self.text_title)
                painter.drawText(x + 2, y + h -4 , value)
                QStyledItemDelegate.paint(self, painter, option, index)
    class MainWindow(QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.task_grp_view = QListView(self)
            self.task_view = QListView(self)
            self.model = QStandardItemModel(self)
            self.h_layout = QHBoxLayout(self)
   = QWidget(self)
        def populate_model(self):
            stuff = ['zero', 'one', 'two']
            for i, s in enumerate(stuff):
                item = QStandardItem(s)
                for sub in stuff:
                    sub_item = QStandardItem(str(i) + '_' + sub)
            self.task_grp_view.setCurrentIndex(self.model.index(0, 0))
        def update_task_view(self, index):
        def update_user_data(self):
            # print('called')
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MainWindow()

  • Hmm... im still a bit lost here.
    I've been trying to catch a signal from the view and the selection model after the interalMove.
    the signals from:

    both give me a start and end index or to and from ints, but if I call the rowCount() on the parent item it returns one additional NULL row for the children. I'm thinking this is the model in the process of inserting a new row and hasn't updated yet. Regardless I cant seem to use the task_view selection Model to select the index (maybe its invalid?), the selection just stays on the row where the drag started.

    I'm just not sure where to grab the internalMove data and update AFTER the model sorts its self out.

Log in to reply