Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. How to use a custom widget as an ItemDelegate
Forum Updated to NodeBB v4.3 + New Features

How to use a custom widget as an ItemDelegate

Scheduled Pinned Locked Moved Language Bindings
1 Posts 1 Posters 2.4k 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.
  • P Offline
    P Offline
    pezzi
    wrote on last edited by
    #1

    Hi Everyone,

    I am trying to provide a custom editor for a list of string items. Imagine each string as having several elements separated by a :
    "a
    b*c"

    I want to display them as such, but when you edit an item, depending on the value of the first, I want to display a widget in place to edit the element. This could be a drop down for the second item "b" and a text edit for the third item "c".

    I am able to use a QComboBox as a delegate, but when I assemble a widget by placing two QComboBox side by side, funny things happen:
    On the first double click, the item is highlighted light grey, if I click on it again it vanishes.

    Here is my PySide/Python code so far ... any insight would be appreciated.

    @
    from PySide import QtGui, QtCore
    from PySide.QtGui import QStringListModel, QListWidget, QItemDelegate, QLabel, QComboBox, QWidget

    class LinkEditor(QWidget):
    def init(self, parent = None):
    super(LinkEditor, self).init(parent)
    layout = QtGui.QHBoxLayout(self)
    c1 = QComboBox(parent)
    c1.addItem('spam')
    c1.addItem('ham')
    c1.addItem('kumquat')
    layout.addWidget(c1)
    c2 = QComboBox(parent)
    c2.addItem('eat')
    c2.addItem('spill')
    layout.addWidget(c2)
    self.setLayout(layout)

    class LinkElementDelegate(QItemDelegate):
    def init(self, parent = None):
    super(LinkElementDelegate, self).init(parent)

    def createEditor(self, parent, option, index):
        l = LinkEditor(parent)
        return l
    

    class LinkListView(QtGui.QListView):
    def init(self, data = None, parent=None):
    super(LinkListView, self).init(parent)
    self.setAcceptDrops(True)
    self.data = data
    self.delegate = LinkElementDelegate(self)
    self.setItemDelegate(self.delegate )

    class LinkMain(QtGui.QWidget):
    def init(self, et = None, main = None, parent=None):
    super(LinkMain, self).init(parent)
    self.et = et
    self.main = main
    self.links = ['hameat', 'spamspill', 'ham*spill']
    self.model = QStringListModel(self.links)
    layout = QtGui.QVBoxLayout(self)
    layout.setContentsMargins(1,1,1,1)
    link_list = LinkListView(data = self.links, parent = self)
    link_list.setModel(self.model)
    self.setAcceptDrops(True);
    layout.addWidget(link_list)
    self.setLayout(layout)
    self.show()

    if name == 'main':
    import sys
    app = QtGui.QApplication(sys.argv)
    form = LinkMain()
    form.show()
    app.exec_()
    @

    EDIT: moved to Language Bindings, Gerolf

    1 Reply Last reply
    0

    • Login

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