我正在尝试使用QStackedLayout
创建动态堆叠布局。该程序运行正常。但是,当我动态创建一个新窗口时,我注意到它在转换之前会冻结一段时间。基本上,一个窗口通过QLineEdits
要求用户输入,并在按下Submit按钮后,它连接到change_window
方法,该方法创建一个新的小部件,并将其与用户输入的详细信息一起添加到主堆栈中,然后进行切换到新创建的布局。下一个窗口(即使用上一个窗口中用户的输入创建X个按钮)。以前,我尝试在主UI控制器上与其他窗口一起创建下一个窗口,但是由于所有布局都是在第一个实例期间创建的,因此调用它不会更新布局。有更好的方法吗?
主界面类
class UI(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Main Window")
# Set dimensions and fix scaling
width, height = 480, 720
self.setFixedSize(width, height)
# Center to the screen
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
# Create a stack object
self.stacked_layout = QStackedLayout()
# Set central widget
self.central_widget = QWidget(self)
self.central_widget.setLayout(self.stacked_layout)
self.setCentralWidget(self.central_widget)
# Create window widgets
self.main_window = MainWindow(self)
self.popup_window = PopupWidget(self)
self.detail_window = DetailWindow(self)
# self.input_window = InputWindow(self)
# Add the main window widget to the stack
self.stacked_layout.addWidget(self.main_window)
self.stacked_layout.addWidget(self.popup_window)
self.stacked_layout.addWidget(self.detail_window)
# self.stacked_layout.addWidget(self.input_window)
在“详细信息窗口”类中更改窗口方法
class DetailWindow(QWidget):
def __init__(self, main):
super().__init__()
self.parent = main
# Enable styling and set background
self.setAttribute(Qt.WA_StyledBackground)
self.setObjectName("detail")
self.setStyleSheet('''#detail{background-image:
url(resources/images/detail_bg.jpg)}''')
self.installEventFilter(self)
# Create the line edits
self.label_width, self.label_height = (74, 40)
self.examinee_edit = self.create_line_edit(322, 264, 2)
self.question_edit = self.create_line_edit(322, 332, 2)
self.choices_edit = self.create_line_edit(322, 397, 2)
# Create labels
self.examinee_label = self.create_labels(84, 270, "Number of Examinees")
self.question_label = self.create_labels(84, 342, "Number of Questions")
self.choices_label = self.create_labels(84, 404, "Number of Choices")
self.prompt1 = self.prompt_label(86, 294)
self.prompt2 = self.prompt_label(86, 364)
self.prompt3 = self.prompt_label(86, 427)
# Create the submit button
self.create_submit_button()
# Go to the next line edit if enter/return is pressed
self.examinee_edit.returnPressed.connect(lambda: self.question_edit.setFocus())
self.question_edit.returnPressed.connect(lambda: self.choices_edit.setFocus())
self.choices_edit.returnPressed.connect(lambda: self.submit.setFocus())
def change_window(self):
print('Current Window:', self.parent.stacked_layout.currentIndex())
line_edit_list = (self.examinee_edit, self.question_edit, self.choices_edit)
label_list = (self.examinee_label, self.question_label, self.choices_label)
prompt_list = (self.prompt1, self.prompt2, self.prompt3)
if self.examinee_edit.hasAcceptableInput() and self.question_edit.hasAcceptableInput() and self.choices_edit.hasAcceptableInput():
next_window = InputWindow(self.parent, self.examinee_edit.text())
self.parent.stacked_layout.addWidget(next_window)
self.parent.stacked_layout.setCurrentIndex(3)
else:
self._input_prompt(label_list, line_edit_list, prompt_list)