我写了一段代码,用分数表并排打印问题。但是,当使用QHBoxLayout
(如果运行)尝试当前表时,不会显示该表,但不会显示错误。另一方面,使用QVBoxLayout
时,会在运行代码时显示。
我可以将问题/答案框并排(水平)地呈现在桌子上吗?分离器是否可以作为替代方案,如果是,我怎样才能在我的代码中实现它?
我的代码如下:
import sys, os, random
from PyQt4 import QtCore, QtGui
StudentNames = []
StudentScores = []
class StartTest(QtGui.QMainWindow):
def __init__(self, questions, parent=None):
global StudentNames
global StudentScores
super(StartTest, self).__init__(parent)
self.stack = QtGui.QStackedWidget(self)
self.setCentralWidget(self.stack)
StudentNames.append('Hamzah Akhtar')
StudentNames.append('Bob Marley')
StudentNames.append('John Meadows')
StudentScores.append(int(0))
StudentScores.append(int(0))
StudentScores.append(int(0))
for index, question in enumerate(questions):
page = Question(question, self)
page.submit.clicked[()].connect(
lambda index=index: self.handleSubmit(index))
self.stack.addWidget(page)
self.answers = []
def handleSubmit(self, index):
page = self.stack.widget(index)
answer = page.answer.text()
# validate submitted answer...
self.answers.append(answer)
if index < self.stack.count() - 1:
self.stack.setCurrentIndex(index + 1)
class Question(QtGui.QWidget):
def __init__(self, question, parent=None):
global StudentNames
global StudentScores
super(Question, self).__init__(parent)
self.Table = QtGui.QTableWidget()
self.Table.setRowCount(3)
self.Table.setColumnCount(1)
self.Table.setVerticalHeaderLabels(StudentNames)
self.Table.setHorizontalHeaderLabels(['Scores'])
self.question = QtGui.QLabel(question, self)
self.answer = QtGui.QLineEdit(self)
self.submit = QtGui.QPushButton('Submit', self)
form = QtGui.QFormLayout()
form.addRow(self.question, self.answer)
layout = QtGui.QVBoxLayout(self)
layout.addLayout(form)
layout.addWidget(self.submit)
hlayout = QtGui.QHBoxLayout(self)
hlayout.addLayout(layout)
hlayout.addWidget(self.Table)
self.setLayout(hlayout)
if __name__ == '__main__':
User = ''
app = QtGui.QApplication([])
questions = [
'What is 5+5?',
'What is 45+10?',
'What is 28+47?',
'What is 22+13?',
]
window = StartTest(questions)
window.showFullScreen()
app.exec_()
答案 0 :(得分:1)
您可以将占位符窗口小部件设置为中央窗口小部件。然后添加水平布局并将小部件添加到该布局。
centralWidget = QtGui.QWidget(self)
self.setCentralWidget(centralWidget)
mainLayout = QtGui.QHBoxLayout(centralWidget)
self.scoreTable = QtGui.QTableWidget(self)
self.scoreTable.setRowCount(len(StudentNames))
self.scoreTable.setColumnCount(1)
self.scoreTable.setVerticalHeaderLabels(StudentNames)
self.scoreTable.setHorizontalHeaderLabels(['Scores'])
for i, score in enumerate(StudentScores):
item = QtGui.QTableWidgetItem(str(score))
self.scoreTable.setItem(i, 0, item)
mainLayout.addWidget(self.stack)
mainLayout.addWidget(self.scoreTable)
完整示例:(使用PySide进行测试但在pyqt中的工作方式相同)
import sys, os, random
from PyQt4 import QtCore, QtGui
StudentNames = []
StudentScores = []
class StartTest(QtGui.QMainWindow):
def __init__(self, questions, parent=None):
global StudentNames
global StudentScores
super(StartTest, self).__init__(parent)
self.stack = QtGui.QStackedWidget(self)
StudentNames.append('Hamzah Akhtar')
StudentNames.append('Bob Marley')
StudentNames.append('John Meadows')
StudentScores.append(int(0))
StudentScores.append(int(0))
StudentScores.append(int(0))
for index, question in enumerate(questions):
page = Question(question, self)
page.submit.clicked[()].connect(
lambda index=index: self.handleSubmit(index))
self.stack.addWidget(page)
self.answers = []
centralWidget = QtGui.QWidget(self)
self.setCentralWidget(centralWidget)
mainLayout = QtGui.QHBoxLayout(centralWidget)
self.scoreTable = QtGui.QTableWidget(self)
self.scoreTable.setRowCount(len(StudentNames))
self.scoreTable.setColumnCount(1)
self.scoreTable.setVerticalHeaderLabels(StudentNames)
self.scoreTable.setHorizontalHeaderLabels(['Scores'])
for i, score in enumerate(StudentScores):
item = QtGui.QTableWidgetItem(str(score))
self.scoreTable.setItem(i, 0, item)
mainLayout.addWidget(self.stack)
mainLayout.addWidget(self.scoreTable)
def handleSubmit(self, index):
page = self.stack.widget(index)
answer = page.answer.text()
# validate submitted answer...
self.answers.append(answer)
if index < self.stack.count() - 1:
self.stack.setCurrentIndex(index + 1)
class Question(QtGui.QWidget):
def __init__(self, question, parent=None):
super(Question, self).__init__(parent)
self.question = QtGui.QLabel(question, self)
self.answer = QtGui.QLineEdit(self)
self.submit = QtGui.QPushButton('Submit', self)
form = QtGui.QFormLayout(self)
form.addRow(self.question, self.answer)
form.addRow(self.submit)
if __name__ == '__main__':
User = ''
app = QtGui.QApplication([])
questions = [
'What is 5+5?',
'What is 45+10?',
'What is 28+47?',
'What is 22+13?',
]
window = StartTest(questions)
window.show()
# window.showFullScreen()
app.exec_()