pyqt将焦点传递给委托编辑器

时间:2014-11-14 18:31:33

标签: delegates pyqt setfocus qstyleditemdelegate

我的QStyledItemDelegate使用QDialog(Qt.Popup)来显示QLineEdit和QListWidget。我没有在QLineEdit上正确设置焦点。焦点似乎仍然存在于编辑开始的QTreeView上。

我试过了, QDialog编辑器中的-setFocus()和setFocusPolicy(Qt.StrongFocus)以及QLineEdit 编辑器上的-activateWindow()(虽然pop已经在顶部)

我的QDialog和QLineEdit子类:

class ValidatedLineEdit(QLineEdit):
def __init__(self, list_widget, parent=None):
    QLineEdit.__init__(self, parent)

    self._listW = list_widget
    self._parent = parent

    self.setFocusPolicy(Qt.StrongFocus)
    self.setFocus()
    self.activateWindow()

    validator = QRegExpValidator(QRegExp("[\w\*]*"), self)
    self.setValidator(validator)

def keyReleaseEvent(self, event):
    '''
    @param event  QKeyEvent
    '''
    self._listW.clear()
    text = self.text()
    reg_exp = QRegExp(text.replace('*', '.*'))

    self._listW.addItems(QStringList([a for a in self._parent._elementList if reg_exp.exactMatch(a)]))

    return super(ValidatedLineEdit, self).keyReleaseEvent(event)


class ValidatedFilterListDialog(QDialog):
def __init__(self, element_list=[], window_flag=Qt.Popup, parent=None):
    QDialog.__init__(self, parent, window_flag)

    self._elementList = element_list

    layout = QVBoxLayout(self)

    self._listW = QListWidget(self)
    self._lineE = ValidatedLineEdit(self._listW, parent=self)
    self._lineE.setFocus()

    self._listW.addItems(QStringList(sorted(self._elementList)))

    layout.addWidget(self._lineE)
    layout.addWidget(self._listW)

@property 
def value(self):
    return self._lineE.text()

@property 
def line_edit(self):
    return self._lineE

@property 
def list_widget(self):
    return self._listW

我的代表班,

class AddValidatedFilterDelegate(QStyledItemDelegate):
'''
A custom validator for the include/exclude items.
'''
def __init__(self, parent=None):
    QStyledItemDelegate.__init__(self, parent)
    self._parent = parent

def createEditor(self, parent, option, index):
    '''
    @param parent QWidget
    @param option QStyleOptionViewItem 
    @param index  QModelIndex 
    '''
    if not index.isValid():
        return 0
    if index.column() == 0: #only on the cells in the first column

        editor = generic.ValidatedFilterListDialog(element_list=sorted(index.model()._parent._elementList), parent=parent)
        editor.setFocusPolicy(Qt.StrongFocus)
        editor.activateWindow()
        editor.line_edit.setFocus()
        return editor

    return super(AddValidatedFilterDelegate, self).createEditor(parent, option, index)

def sizeHint(self, option, index):
    text = index.model().data(index)
    document = QTextDocument()
    document.setDefaultFont(option.font)
    document.setHtml(text)
    return QSize(document.idealWidth() + 5,
                 option.fontMetrics.height())


def setEditorData(self, editor, index):
    value = index.model().data(index, Qt.DisplayRole)
    editor.line_edit.setText(value)
    editor.line_edit.setFocus()


def setModelData(self, editor, model, index):
    model.setData(index, QVariant(editor.value))


def updateEditorGeometry(self, editor, option, index):

    top_left = self._parent.mapToGlobal(self._parent.rect().topLeft())
    x = top_left.x()
    y = top_left.y() + editor.rect().y()
    w = self._parent.width()

    editor.setGeometry(x, y, w, 400)

一切都搞定了?

0 个答案:

没有答案