Note that there are some explanatory texts on larger screens.

plurals
  1. POMaintain Selection of QComboBox under a QStyledItemDelegate
    primarykey
    data
    text
    <p>Below is an example of a custom delegated <code>QComboBox</code>. When I make a selection, click out (or otherwise lose focus with the <code>QComboBox</code>), and then TAB back in (gain focus), I lose my original selection. For example, using the below code if I choose <code>"Item 2"</code>, click out, then TAB back in, the selection will go back to <code>"Item 1."</code></p> <p><strong>How can I maintain the selection?</strong></p> <p>I am assuming this issue occurs because I am using <code>addItem()</code> in <code>TheEditor</code> <code>QComboBox</code> class every time it is initialized except I am not too sure how I should be approaching this method. Should I instead be initalizing <code>TheEditor</code> in the <code>EditDelegate</code> <code>__ init __</code> class so that it is only initialized once and not every time it is focused? How might I do that properly?</p> <pre><code>import sys from PySide import QtCore, QtGui, QtSql class EditDelegate(QtGui.QStyledItemDelegate): def __init__(self, parent=None): super(EditDelegate, self).__init__(parent) def createEditor(self, parent, option, index): editor = TheEditor(parent) return editor class TheEditor(QtGui.QComboBox): def __init__(self, parent=None): super(TheEditor, self).__init__(parent) self.addItem("Item 1") self.addItem("Item 2") self.addItem("Item 3") self.setEditable(True) class TheTable(QtGui.QTableWidget): def __init__(self, columns, parent=None): super(TheTable, self).__init__(parent) self.setItemDelegate(EditDelegate()) self.setEditTriggers(QtGui.QAbstractItemView.AllEditTriggers) self.setColumnCount(1) self.setRowCount(1) self.setHorizontalHeaderLabels(["QCombo"]) class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setCentralWidget(TheTable(self)) if __name__ == '__main__': app = QtGui.QApplication(sys.argv) frame = MainWindow() frame.show() app.exec_() </code></pre> <p>Note: PySide v1.2.0</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload