使用PySide2,我有一个包含QColumnView小部件的界面,为此,我试图为其创建类似于联系人查找功能的功能:选择一个人的姓名,然后在下一列中显示有关该选择的更多选项/详细信息。
[当前,我在加载UI时填充了视图,并显示了用户可用的第一级选项。
所需功能:当用户进行选择时,会将另一列添加到QColumnView中,该列显示基于用户选择的可用选项。
[当前问题:当用户进行选择时,由于我不确定如何向QColumnView添加另一个列/级别,因此,将更新QColumnView的模型,并且仅显示下一级别的选项-意味着上一级别可以这么说,值在视图中被覆盖。尽管此功能有效,但并不理想,因为仍在查看前一列,使用户可以查看他们当前正在搜索的类别。
如此处所示,用户选择了“地址”,然后在右栏中显示与“地址”相关的选项。
只是创建一个包含QColumnView的基本接口,它看起来像这样:
import sys
from PySide2 import QtWidgets, QtCore
import PySide2
class UserInput(object):
def setupUi(self, get_user_input=None):
# Basic shape
self.width = 425
get_user_input.setObjectName("get_user_input")
get_user_input.resize(425, self.width)
self.frame = QtWidgets.QFrame(get_user_input)
self.frame.setGeometry(QtCore.QRect(11, 10, 401, 381))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Sunken)
self.frame.setObjectName("frame")
# Creating the grid layout for most of the display elements
self.gridLayoutWidget = QtWidgets.QWidget(self.frame)
self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 381, 361))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.get_user_input_layout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.get_user_input_layout.setContentsMargins(5, 5, 5, 5)
self.get_user_input_layout.setObjectName("get_user_input_layout")
# Grid layout for the buttons
self.buttonLayoutGrid = QtWidgets.QWidget(get_user_input)
self.buttonLayoutGrid.setGeometry(QtCore.QRect(10, 390, 401, 41))
self.buttonLayoutGrid.setObjectName("buttonLayoutGrid")
self.buttonLayout = QtWidgets.QGridLayout(self.buttonLayoutGrid)
self.buttonLayout.setContentsMargins(0, 0, 0, 0)
self.buttonLayout.setObjectName("buttonLayout")
# Buttons
self.buttonOK = QtWidgets.QPushButton(self.buttonLayoutGrid)
self.buttonOK.setObjectName("buttonOK")
self.buttonOK.setText("OK")
self.buttonLayout.addWidget(self.buttonOK, 0, 1, 1, 1)
self.buttonCancel = QtWidgets.QPushButton(self.buttonLayoutGrid)
self.buttonCancel.setObjectName("buttonCancel")
self.buttonCancel.setText("CANCEL")
self.buttonLayout.addWidget(self.buttonCancel, 0, 2, 1, 1)
#Column View Data
self.t = PySide2.QtWidgets.QColumnView()
self.t.setColumnWidths([10,10])
model = PySide2.QtGui.QStandardItemModel()
model.setColumnCount(1)
model.setHorizontalHeaderLabels(['Data Collections'])
self._dict = {'Test 1': [1, 2, 3], 'Test 2': [4, 5, 6], 'Test 3': [7, 8, 9]}
_data = []
for each in self._dict:
resultItem = PySide2.QtGui.QStandardItem()
insertText = each
resultItem.setText(str(insertText))
model.appendRow(resultItem)
self.t.setModel(model)
self.t.clicked.connect(self.column_view_clicked)
self.get_user_input_layout.addWidget(self.t, 2,0,1,1)
def column_view_clicked(self, row):
model = PySide2.QtGui.QStandardItemModel()
model.setColumnCount(1)
model.setHorizontalHeaderLabels(['Analysis Variables'])
_data = []
for i in self._dict[row.data()]:
resultItem = PySide2.QtGui.QStandardItem()
resultItem.setText(str(i))
model.appendRow(resultItem)
self.t.createColumn(row)
class UserInputPrompt(PySide2.QtWidgets.QDialog, UserInput):
def __init__(self, path_to_image=None):
app = PySide2.QtWidgets.QApplication(sys.argv)
super(UserInputPrompt, self).__init__()
self.setupUi(self)
super(UserInputPrompt, self).exec_()
def get_user_input(self):
print("Get user selection")
def main():
UserInputPrompt()
if __name__ == '__main__':
main()
[当您运行上述代码时,单击界面左侧的任何项(“测试1”,“测试2”或“测试3”),结果将在现有列的右侧添加一列,但那里目前没有数据填充-因为我目前不知道该怎么办。
import sys
from PySide2 import QtCore, QtGui, QtWidgets
def populateTree(children, parent):
for child in children:
child_item = QtGui.QStandardItem(child)
parent.appendRow(child_item)
if isinstance(children, dict):
populateTree(children[child], child_item)
class Dialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.frame = QtWidgets.QFrame(
frameShape=QtWidgets.QFrame.StyledPanel, frameShadow=QtWidgets.QFrame.Sunken
)
self.buttonOK = QtWidgets.QPushButton(self.tr("OK"))
self.buttonCancel = QtWidgets.QPushButton(self.tr("CANCEL"))
self.columnview = QtWidgets.QColumnView()
grid_layout = QtWidgets.QGridLayout(self)
grid_layout.addWidget(self.frame, 0, 0, 1, 2)
grid_layout.addWidget(self.buttonOK, 1, 0)
grid_layout.addWidget(self.buttonCancel, 1, 1)
lay = QtWidgets.QVBoxLayout(self.frame)
lay.addWidget(self.columnview)
data = {
"root1": {
"child11": ["subchild111", "subchild112"],
"child12": ["subchild121"],
},
"root2": {
"child21": ["subchild211"],
"child22": ["subchild221", "subchild222"],
},
}
self.model = QtGui.QStandardItemModel(self)
self.columnview.setModel(self.model)
populateTree(data, self.model.invisibleRootItem())
self.resize(1280, 480)
def main():
app = QtWidgets.QApplication(sys.argv)
w = Dialog()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()