我开发与3上的每一行的接口QCombobox
与该接口将包括多达50,000的行。每个QCombobox
填充的15'000项的列表。
填充过程非常缓慢。
是否有可能加快这一进程?
我当前的代码填入QCombobox
只有10行。在10行时,该过程耗时,而在50'000行时,该界面不显示。
后端:
import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget,
QMessageBox, QDialog, QFileDialog
from frontend import Ui_MainWindow
class Ui_MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
self.setupUi(self)
myList = ['AAA'] * 15000
for i in range(1, 10):
self.cb1 = QtWidgets.QComboBox()
self.cb2 = QtWidgets.QComboBox()
self.cb3 = QtWidgets.QComboBox()
self.cb1.addItems(myList)
self.cb2.addItems(myList)
self.cb3.addItems(myList)
self.gridLayout_2.addWidget(self.cb2,i,0)
self.gridLayout_2.addWidget(self.cb1,i,1)
self.gridLayout_2.addWidget(self.cb2,i,2)
if __name__ == '__main__':
app = QApplication(sys.argv)
current_path = os.getcwd()
app.processEvents()
prog = Ui_MainWindow()
prog.showMaximized()
sys.exit(app.exec_())
前端:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(606, 446)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 586, 385))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.gridLayout_3 =
QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout_3.addLayout(self.gridLayout_2, 0, 0, 1, 1)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 606, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
您可以尝试覆盖showPopup
方法
import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import ( QMainWindow, QApplication, QWidget,
QMessageBox, QDialog, QFileDialog)
#from frontend import Ui_MainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(606, 446)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 586, 385))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.gridLayout_3 = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout_3.addLayout(self.gridLayout_2, 0, 0, 1, 1)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 606, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
class ComboBox(QtWidgets.QComboBox):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.myList = ['AAA-{}'.format(i) for i in range(1, 15000)] #['AAA'] * 15000
def showPopup(self):
self.hidePopup()
self.addItems(self.myList)
""" # or
for var in range(1, 15000):
self.addItem("TEST " + str(var))
"""
super(ComboBox, self).showPopup()
def hidePopup(self):
super(ComboBox, self).hidePopup()
#class Ui_MainWindow(QMainWindow, Ui_MainWindow):
class My_MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# myList = ['AAA-{}'.format(i) for i in range(1, 1500)] #['AAA'] * 15000
for i in range(1, 10):
self.cb1 = ComboBox() # QtWidgets.QComboBox()
self.cb2 = ComboBox() # QtWidgets.QComboBox()
self.cb3 = ComboBox() # QtWidgets.QComboBox()
# self.cb1.addItems(myList)
# self.cb2.addItems(myList)
# self.cb3.addItems(myList)
self.gridLayout_2.addWidget(self.cb1,i,0) # cb2
self.gridLayout_2.addWidget(self.cb2,i,1) # cb1
self.gridLayout_2.addWidget(self.cb3,i,2) # cb2
if __name__ == '__main__':
app = QApplication(sys.argv)
current_path = os.getcwd()
app.processEvents()
prog = My_MainWindow() # Ui_MainWindow()
prog.showMaximized()
prog.show()
sys.exit(app.exec_())