在QListWidget中动态创建Row

发布于 2024-11-23 20:02:41 字数 262 浏览 0 评论 0原文

你好,

我想创建一个自定义小部件。请帮忙提供一些想法。
我的目的是制作一个显示一些信息的列表小部件,当单击特定行时,它必须通过在所选行下方立即创建一个新区域(行或文本编辑)并将其他行拉到所选行下方来显示与该行对应的详细信息创建新区域后向下行。
在附件中,当我单击行 Nancy(将其视为行)时,她的详细信息将显示在所选行的下方。请对此提供帮助

在此处输入图像描述

Hi,

I want to create a custome widget. Please help by giving some idea.
My intention is to make a Listwidget which shows some Information and when clicking particulat row it have to show the details coresponding to that row by creating a new area(Row or text edit) immediately below the selected row and pull the other rows below the selected row to down after the created new area.
In attachment when I click row Nancy (consider it as row) her details is coming below the selected row. Please help on this

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮是我掰弯的 2024-11-30 20:02:42

您要使用的是 QTreeView。它能够显示带有文本和图标的行。
您需要为表定义 QStandardItemModel,这将为其提供列定义。

以下是我为类似应用程序编写的类中的三个相关函数。请注意,我不需要在行中使用任何图像,因此您必须自己弄清楚该部分。

def drawPeopleListBox(self):
    header = ["Picture","First Name","Last Name","Title","Address", "City", "Region"]
    self.model = QStandardItemModel(0, len(header), self)
    for i in range(len(header):
        self.model.setHeaderData(i, Qt.Horizontal, self.selectionDict[i+1].upper())

    self.itemList = QTreeView()
    self.itemList.setRootIsDecorated(False)
    self.itemList.setAlternatingRowColors(True)
    self.itemList.setSortingEnabled(False)
    self.itemList.setModel(self.model)
    self.itemList.NoEditTriggers=True

    #self.itemList.clicked[QModelIndex].connect(self.onRowClick)
    self.itemList.clicked.connect(self.onRowClick)
    self.itemList.setCursor(Qt.PointingHandCursor)

    self.itemList.setColumnWidth(0,70)
    self.itemList.setColumnWidth(1,140)
    self.itemList.setColumnWidth(2,70)
    self.itemList.setColumnWidth(3,180)
    self.itemList.setColumnWidth(4,100)
    self.itemList.setColumnWidth(5,100)
    self.itemList.setColumnWidth(6,100)

    self.populateList(self.userDataList)

def populateList(self, userDataList):
        row=[]
        for user in userDataList:
            for attrib in user:
                row.append(QStandardItem(attrib))

            for item in row:
                item.setEditable(False)

            self.model.appendRow(row)

def onRowClick(self, index):
    print index.row()
    '''
    Here you need to resize the clicked row height. Also resise the image.
    Or insert another row that matches your design requirement. 
    self.model.insertRow(rowNumber, listOfQStandardItems)
    '''         
    self.repaint()

What you want to use is the QTreeView. It is capable of showing rows with text and icons.
You need to define the QStandardItemModel for the table, this will give it column definition.

Below are three relevant functions from a class I wrote for a similar app. Note that I did not need to use any images in my rows so you'll have to figure that part out yourself.

def drawPeopleListBox(self):
    header = ["Picture","First Name","Last Name","Title","Address", "City", "Region"]
    self.model = QStandardItemModel(0, len(header), self)
    for i in range(len(header):
        self.model.setHeaderData(i, Qt.Horizontal, self.selectionDict[i+1].upper())

    self.itemList = QTreeView()
    self.itemList.setRootIsDecorated(False)
    self.itemList.setAlternatingRowColors(True)
    self.itemList.setSortingEnabled(False)
    self.itemList.setModel(self.model)
    self.itemList.NoEditTriggers=True

    #self.itemList.clicked[QModelIndex].connect(self.onRowClick)
    self.itemList.clicked.connect(self.onRowClick)
    self.itemList.setCursor(Qt.PointingHandCursor)

    self.itemList.setColumnWidth(0,70)
    self.itemList.setColumnWidth(1,140)
    self.itemList.setColumnWidth(2,70)
    self.itemList.setColumnWidth(3,180)
    self.itemList.setColumnWidth(4,100)
    self.itemList.setColumnWidth(5,100)
    self.itemList.setColumnWidth(6,100)

    self.populateList(self.userDataList)

def populateList(self, userDataList):
        row=[]
        for user in userDataList:
            for attrib in user:
                row.append(QStandardItem(attrib))

            for item in row:
                item.setEditable(False)

            self.model.appendRow(row)

def onRowClick(self, index):
    print index.row()
    '''
    Here you need to resize the clicked row height. Also resise the image.
    Or insert another row that matches your design requirement. 
    self.model.insertRow(rowNumber, listOfQStandardItems)
    '''         
    self.repaint()
寄居人 2024-11-30 20:02:42

您可能想尝试使用自定义委托来查看行。我相信代表们知道他们是否被选中。使用此功能,您可以正常绘制未选定的行,并为选定的行绘制更多信息。问题是我不知道您是否可以在选择时调整小部件的大小。如果没有,QTreeView 解决方案应该仍然有效。

You may want to try using a custom delegate to view the rows. I believe the delegates know if they are selected. Using this, you would draw the unselected rows normally, and draw more information for the selected rows. The problem with this is that I don't know if you can resize the widget on selection. If not, the QTreeView solution should still work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文