Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. ComboBox Delegate edit the wrong row in QTableView
Forum Updated to NodeBB v4.3 + New Features

ComboBox Delegate edit the wrong row in QTableView

Scheduled Pinned Locked Moved Unsolved Qt for Python
12 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hachbani
    wrote on last edited by
    #1

    Hello,

    I want to be able to edit values in a specific column in my TableView, through a combobox. I've create a QItemDelegate subclass.

    here's my code:

    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    
    	def __init__(self, mlist=None):
    		super(MainWindow, self).__init__()
    		self.Table = QtWidgets.QTableView
    		self.model = TableModel() #QAbstractTableModel subclass
    		self.proxyModel = ProxyModel() #QSortFilterProxyModel subclass
    
    		self.proxyModel.setsourceModel(self.model)
    		self.Table.setModel(self.proxyModel)
    		self.Table.setItemDelegateForColumn(10, ComboBoxItemDelegate())
    
    class ComboBoxItemDelegate(QtWidgets.QItemDelegate):
    	
    	def __init__(self, parent=None):
    		QtWidgets.QItemDelegate.__init__(self,parent)
    		self._items = [str(type) for type in TYPES_POINTS.values()] 
    
    	def createEditor(self, widget, option, index):
    		editor = QtWidgets.QComboBox(widget)
    		editor.addItems(self._items)
    		return editor
    
    	def setEditorData(self, editor, index):
    		value = int(index.model().sourceModel().data(index, QtCore.Qt.EditRole))
    		if value:
    			editor.setCurrentIndex(value)
    
    	def setModelData(self, editor, model, index):
    		model.sourceModel().setData(index, editor.currentIndex(), QtCore.Qt.EditRole)
    
    	def updateEditorGeometry(self, editor, option, index):
    		editor.setGeometry(option.rect)
    

    What happens is when I double click a cell in the 10th column, I displays the values I want, I choose I value, hit enter, the value changes but in a different row. The problem is caused (in my opinion) by the proxyModel, the rows are displayed in different order because of the ProxyModel sort method (Lessthan() ).

    Any way to solve that ?

    thanks,

    JonBJ 1 Reply Last reply
    0
    • H hachbani

      Hello,

      I want to be able to edit values in a specific column in my TableView, through a combobox. I've create a QItemDelegate subclass.

      here's my code:

      class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
      
      	def __init__(self, mlist=None):
      		super(MainWindow, self).__init__()
      		self.Table = QtWidgets.QTableView
      		self.model = TableModel() #QAbstractTableModel subclass
      		self.proxyModel = ProxyModel() #QSortFilterProxyModel subclass
      
      		self.proxyModel.setsourceModel(self.model)
      		self.Table.setModel(self.proxyModel)
      		self.Table.setItemDelegateForColumn(10, ComboBoxItemDelegate())
      
      class ComboBoxItemDelegate(QtWidgets.QItemDelegate):
      	
      	def __init__(self, parent=None):
      		QtWidgets.QItemDelegate.__init__(self,parent)
      		self._items = [str(type) for type in TYPES_POINTS.values()] 
      
      	def createEditor(self, widget, option, index):
      		editor = QtWidgets.QComboBox(widget)
      		editor.addItems(self._items)
      		return editor
      
      	def setEditorData(self, editor, index):
      		value = int(index.model().sourceModel().data(index, QtCore.Qt.EditRole))
      		if value:
      			editor.setCurrentIndex(value)
      
      	def setModelData(self, editor, model, index):
      		model.sourceModel().setData(index, editor.currentIndex(), QtCore.Qt.EditRole)
      
      	def updateEditorGeometry(self, editor, option, index):
      		editor.setGeometry(option.rect)
      

      What happens is when I double click a cell in the 10th column, I displays the values I want, I choose I value, hit enter, the value changes but in a different row. The problem is caused (in my opinion) by the proxyModel, the rows are displayed in different order because of the ProxyModel sort method (Lessthan() ).

      Any way to solve that ?

      thanks,

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @hachbani
      When you use a proxy model you must be prepared to map between proxy indexes and source model indexes, because the whole point is they may not have the same row/column numbers. See https://doc.qt.io/qt-5/qabstractproxymodel.html#mapFromSource & https://doc.qt.io/qt-5/qabstractproxymodel.html#mapSelectionToSource.

      H 1 Reply Last reply
      1
      • H Offline
        H Offline
        hachbani
        wrote on last edited by
        #3

        I've read about reimplementing mapfromSource & mapToSource methods, but can't figure out how to reiplement them. I'm struggling to find examples to guide me. Any idea about where to start ?
        thanks

        JonBJ 1 Reply Last reply
        0
        • H hachbani

          I've read about reimplementing mapfromSource & mapToSource methods, but can't figure out how to reiplement them. I'm struggling to find examples to guide me. Any idea about where to start ?
          thanks

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @hachbani
          You do not need to do any re-implementing here. The QSortFilterProxyModel you are using has already done that in its implementation of the map functions. You just need to call them, wherever you need to convert between indexes into the proxy model versus indexes into the source model.

          1 Reply Last reply
          1
          • JonBJ JonB

            @hachbani
            When you use a proxy model you must be prepared to map between proxy indexes and source model indexes, because the whole point is they may not have the same row/column numbers. See https://doc.qt.io/qt-5/qabstractproxymodel.html#mapFromSource & https://doc.qt.io/qt-5/qabstractproxymodel.html#mapSelectionToSource.

            H Offline
            H Offline
            hachbani
            wrote on last edited by
            #5

            @JonB I've solved the issue by replacing model.sourceModel() by model . It works even though it's not a clean way to do it.

            Do you know how can I open the editor just by a single click event ? because now I have to first double click the cell, then an additional click to show the comboBox list

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Why is it not clean ?

              You could have several proxies on top of each other.

              You should always access the top most model.

              You can change the edit trigger and select single click.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                Why is it not clean ?

                You could have several proxies on top of each other.

                You should always access the top most model.

                You can change the edit trigger and select single click.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @SGaist said in ComboBox Delegate edit the wrong row in QTableView:

                You can change the edit trigger and select single click.

                I actually looked at https://doc.qt.io/qt-5/qabstractitemview.html#EditTrigger-enum and not sure you can do that? I don't think QAbstractItemView::SelectedClicked is what the OP is asking for. @hachbani There is a whole stackoverflow topic for this, Qt: start editing of cell after one click for a combobox, you might want to read the possibilities there.

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  I had CurrentChanged in mind.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  JonBJ 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    I had CurrentChanged in mind.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #9

                    @SGaist
                    I would never argue with you :) Yes, I didn't even notice that one. We'll see what the OP makes of it, there must be a reason for all those solutions!

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @JonB said in ComboBox Delegate edit the wrong row in QTableView:

                      I would never argue with you :)

                      No please do, I do make mistakes :-)

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      JonBJ 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        @JonB said in ComboBox Delegate edit the wrong row in QTableView:

                        I would never argue with you :)

                        No please do, I do make mistakes :-)

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #11

                        @SGaist
                        @hachbani has complained that

                        then an additional click to show the comboBox list

                        For CurrentChanged, and other edit triggers, don't they just put the combobox in the cell but not expand it? I think the stackoverflow proposals are to do with getting the combo's dropdown list to pop up immediately (on the single-click-to-edit), which the OP was asking for.

                        SGaistS 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @SGaist
                          @hachbani has complained that

                          then an additional click to show the comboBox list

                          For CurrentChanged, and other edit triggers, don't they just put the combobox in the cell but not expand it? I think the stackoverflow proposals are to do with getting the combo's dropdown list to pop up immediately (on the single-click-to-edit), which the OP was asking for.

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @JonB said in ComboBox Delegate edit the wrong row in QTableView:

                          @SGaist
                          @hachbani has complained that

                          then an additional click to show the comboBox list

                          For CurrentChanged, and other edit triggers, don't they just put the combobox in the cell but not expand it? I think the stackoverflow proposals are to do with getting the combo's dropdown list to pop up immediately (on the single-click-to-edit), which the OP was asking for.

                          See ? You were right to argue, I missed the "triple click" issue :-)

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          2

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved