如何使用ExtendedComboBox在ui文件中工作?

2024-09-30 04:29:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我从这个网址得到了一个例子: How do I Filter the PyQt QCombobox Items based on the text input? 我需要一个例子,但是在创建的ui文件中运行这个有问题。 在ui文件中我有组合框。在

当我使用这个时:

  self.ui.comboBox = ExtendedComboBox()
  self.ui.comboBox.addItems(['test','test1','test3','test4'])

组合框为空且不活动。在

当我使用这个时:

^{pr2}$

ui文件中的组合框仍为空,但显示在一个单独的窗口中,该窗口在ExtendedComboBox中正确运行。在

如何使用ExtendedComboBox在ui文件中工作? 可能是犯了个愚蠢的错误

class ExtendedComboBox(QComboBox):
    def __init__(self, parent=None):
        super(ExtendedComboBox, self).__init__(parent)

        self.setFocusPolicy(Qt.StrongFocus)
        self.setEditable(True)
        # add a filter model to filter matching items
        self.pFilterModel = QSortFilterProxyModel(self)
        self.pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
        self.pFilterModel.setSourceModel(self.model())

        # add a completer, which uses the filter model
        self.completer = QCompleter(self.pFilterModel, self)
        # always show all (filtered) completions
        self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
        self.setCompleter(self.completer)
        # connect signals
        self.lineEdit().textEdited[unicode].connect(self.pFilterModel.setFilterFixedString)
        self.completer.activated.connect(self.on_completer_activated)
    # on selection of an item from the completer, select the corresponding item from combobox
    def on_completer_activated(self, text):
        if text:
            index = self.findText(text)
            self.setCurrentIndex(index)
    # on model change, update the models of the filter and completer as well
    def setModel(self, model):
        super(ExtendedComboBox, self).setModel(model)
        self.pFilterModel.setSourceModel(model)
        self.completer.setModel(self.pFilterModel)
    # on model column change, update the model column of the filter and completer as well
    def setModelColumn(self, column):
        self.completer.setCompletionColumn(column)
        self.pFilterModel.setFilterKeyColumn(column)
        super(ExtendedComboBox, self).setModelColumn(column)

class CaDialogal(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        # Set up the user interface from Designer.
        self.ui = Ui_CaDialog()
        self.ui.setupUi(self)

        **self.ui.comboBox = ExtendedComboBox()
        self.ui.comboBox.addItems(['test','test1','test3','test4'])
        #self.ui.comboBox.show()
        ##self.ui.comboBox.addItems(['test','test1','test3','test4'])**

Tags: 文件thetextselfuimodeliniton
1条回答
网友
1楼 · 发布于 2024-09-30 04:29:26

您不能通过直接分配来替换ui文件中的小部件。在

在这种情况下,可能有几种最简单的方法来推广你的widget。为此,请返回Qt设计器并右键单击要替换的组合框,然后选择“升级到…”。在对话框中,将“Promoted class name”设置为“ExtendedComboBox”,并将“Header file”设置为包含该类的模块的python导入路径(例如myapp.mainwindow)。然后单击“Add”和“Promote”,您将在objectinspector窗格中看到类从“QComboBox”更改为“ExtendedComboBox”。在

现在,当您使用pyuic重新生成ui模块时,您应该看到它使用了您的ExtendedComboBox类,并且在文件的底部将有一个额外的行,如下所示:

    from myapp.mainwindow import ExtendedComboBox

有了这些,你需要做的就是:

^{pr2}$

希望一切都能如你所愿。在

相关问题 更多 >

    热门问题