1

我试图在 pyQT 主窗口上显示一个自定义小部件,但由于某种原因,我得到了两个窗口,而不是函数年份中定义的自定义小部件,即 Dialog_01(主窗口)中显示的可检查组合框。下面是我的代码。感谢有人有我可以查看的任何方向或文件。

import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget, QPushButton
from PyQt5 import QtWidgets
class CheckableComboBox(QtWidgets.QComboBox):
    closedPopup = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(CheckableComboBox, self).__init__(parent)
        self.setView(QtWidgets.QListView(self))
        self.view().pressed.connect(self.handleItemPressed)
        self.setModel(QtGui.QStandardItemModel(self))

        firstItem = QtGui.QStandardItem("Select Years")
        firstItem.setBackground(QtGui.QBrush(QtGui.QColor(200, 200, 200)))
        firstItem.setSelectable(False)
        self.model().appendRow(firstItem)

    @QtCore.pyqtSlot(QtCore.QModelIndex)
    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.checkState() == QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Unchecked)
        else:
            item.setCheckState(QtCore.Qt.Checked)

    def checkedItems(self):
        l = []
        for i in range(self.model().rowCount()):
            it = self.model().item(i)
            if it.checkState() == QtCore.Qt.Checked:
                l.append(it.text())
        return l

    def hidePopup(self):
        self.closedPopup.emit()
        super(CheckableComboBox, self).hidePopup()
        QtCore.QTimer.singleShot(0, lambda: self.setCurrentIndex(0))
class Dialog_01(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "Title"  
        self.top = 100        
        self.left = 100        
        self.width = 800      
        self.height = 500
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)
        self.years()
        self.location()
        self.MaxYears()

        self.layout = QtWidgets.QVBoxLayout(self)
        self.listWidget = self.comboBoxv
        self.layout.addWidget(self.listWidget)
        self.listWidget.show()
    def years(self):
        label0 = QLabel('Years', self)
        label0.move(50,50)
        label0.setGeometry(50,50,500,20)

        years = ["2017", "2018", "2019", "2020", "2021", "2022"]
        self.comboBoxv = CheckableComboBox()
        for i, area in enumerate(years):
            item = QtGui.QStandardItem(area)
            item.setCheckable(True)
            item.setCheckState(QtCore.Qt.Unchecked)
            self.comboBoxv.model().appendRow(item)        
        self.layout = QtWidgets.QVBoxLayout(self)
        self.setLayout(self.layout)
        self.listWidget = self.comboBoxv
        self.layout.addWidget(self.listWidget)
        self.listWidget.move(300, 50)


    @QtCore.pyqtSlot()
    def on_closedPopup(self):
        print(len(self.comboBoxv.checkedItems()))


    def location(self):  
        label1 = QLabel('Location', self)
        label1.move(50,150)
        label1.setGeometry(50,150,500,20)
        comboBox1 = QComboBox(self)
        for i in range(1,15):
            comboBox1.addItem("LOCATION %i" % i)
        comboBox1.move(300, 150)
        self.locationDrop=comboBox1
    def MaxYears(self):
        label2 = QLabel('Maximum Years', self)
        label2.move(50,250)
        label2.setGeometry(50,250,500,20)
        comboBox2 = QComboBox(self)
        for i in range(1, 10):
            comboBox2.addItem(str(i))
        comboBox2.move(300, 250)
        self.maxyearsDrop=comboBox2

if __name__ == '__main__':

    if not QApplication.instance():
        App = QApplication(sys.argv)
    else:
        App = QApplication.instance()
    ui = Dialog_01()
    ui.show()
    sys.exit(App.exec_())
4

1 回答 1

-1

您必须了解编程不会放置无意义的代码,例如:您尝试使用self.listWidget = self.comboBoxv什么?,以及上面有几个不一致的地方。


另一方面 QMainWindow 是一个特殊的小部件,因为它已经有一个布局:

在此处输入图像描述

因此,您必须在设置布局的地方创建一个中央小部件。


如果要使用布局,则不应再使用几何、移动或调整大小方法,因为布局会处理这些属性。


另一方面,我认为您想制作一个有描述和编辑器的公式,在这种情况下,Qt 提供了 QFormLayout。


import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class CheckableComboBox(QtWidgets.QComboBox):
    closedPopup = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(CheckableComboBox, self).__init__(parent)
        self.setView(QtWidgets.QListView(self))
        self.view().pressed.connect(self.handleItemPressed)
        self.setModel(QtGui.QStandardItemModel(self))

        firstItem = QtGui.QStandardItem("Select Years")
        firstItem.setBackground(QtGui.QBrush(QtGui.QColor(200, 200, 200)))
        firstItem.setSelectable(False)
        self.model().appendRow(firstItem)

    @QtCore.pyqtSlot(QtCore.QModelIndex)
    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.checkState() == QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Unchecked)
        else:
            item.setCheckState(QtCore.Qt.Checked)

    def checkedItems(self):
        l = []
        for i in range(self.model().rowCount()):
            it = self.model().item(i)
            if it.checkState() == QtCore.Qt.Checked:
                l.append(it.text())
        return l

    def hidePopup(self):
        self.closedPopup.emit()
        super(CheckableComboBox, self).hidePopup()
        QtCore.QTimer.singleShot(0, lambda: self.setCurrentIndex(0))

class Dialog_01(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "Title"  
        self.top, self.left, self.width, self.height = 100, 100, 800, 500
        self.initUI()

    def initUI(self):
        central_widget = QtWidgets.QWidget()
        self.flayout = QtWidgets.QFormLayout(central_widget)
        self.setCentralWidget(central_widget)

        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)
        self.years()
        self.location()
        self.MaxYears()

    def years(self):
        label = QtWidgets.QLabel('Years')

        years = ["2017", "2018", "2019", "2020", "2021", "2022"]
        self.comboBoxv = CheckableComboBox()
        for i, area in enumerate(years):
            item = QtGui.QStandardItem(area)
            item.setCheckable(True)
            item.setCheckState(QtCore.Qt.Unchecked)
            self.comboBoxv.model().appendRow(item)
        self.flayout.addRow(label, self.comboBoxv)

    @QtCore.pyqtSlot()
    def on_closedPopup(self):
        print(len(self.comboBoxv.checkedItems()))

    def location(self):  
        label = QtWidgets.QLabel('Location')
        combobox = QtWidgets.QComboBox()
        for i in range(1,15):
            combobox.addItem("LOCATION %i" % i)
        self.flayout.addRow(label, combobox)

    def MaxYears(self):
        label = QtWidgets.QLabel('Maximum Years')
        combobox = QtWidgets.QComboBox()
        for i in range(1, 10):
            combobox.addItem(str(i))
        self.flayout.addRow(label, combobox)

if __name__ == '__main__':
    App = QtWidgets.QApplication.instance()
    if App is None:
        App = QtWidgets.QApplication(sys.argv)
    ui = Dialog_01()
    ui.show()
    sys.exit(App.exec_())
于 2019-02-25T22:47:32.573 回答