QComboBox 显示 QTreeView 的整行 [英] QComboBox show whole row of QTreeView

查看:75
本文介绍了QComboBox 显示 QTreeView 的整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 QtreeView 来组织 QComboBox 显示的数据.正如您在我的示例中看到的,到目前为止,创建框和设置数据是有效的.但我的问题是,组合框本身只显示第一个参数而不是整行.我想要的是,显示整行,而不仅仅是行的第一项.

I want to use QtreeView to organize the data shown by a QComboBox. As you can see in my example, creating the box and setting up data works so far. But my problem is, that the combobox itself only shows the first argument and not the whole line. what I want to have is, that there is shown the whole row, not only the first item of the row.

这可能与每个单元格都是可选的事实有关吗?是否必须禁止选择树分支末端的项目?

Is this maybe related to the fact, that each cell is selectable? Do I have to prohibit to select items at the end of the tree branch?

如何在将元素添加到 QtreeView-data 时实现此目的?

How can I achieve this while adding the elements to the QtreeView-data?

最小示例:

import sys

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


data = [['a','b','c'],['d','e','f'],['g','h','i']]

class MainWindow(QMainWindow):
    dispatcher = 0

    def __init__(self):
        super().__init__()
        # buil UI
        self.init_ui()

    def init_ui(self):
        # layout
        self.box_window = QVBoxLayout()
        # content
        model = QStandardItemModel(len(data),len(data[0]))
        row = 0
        for r in data:
            col = 0
            for item in r:
                model.setData(model.index(row, col), item)
                col += 1
            row += 1
        tree_view = QTreeView()
        tree_view.setHeaderHidden(True)
        tree_view.setRootIsDecorated(False)
        tree_view.setAlternatingRowColors(True)
        combobox = QComboBox()
        combobox.setMinimumSize(250,50)
        combobox.setView(tree_view)
        combobox.setModel(model)
        self.box_window.addWidget(combobox)
        self.box_window.addStretch()
        # build central widget and select it
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        self.centralWidget().setLayout(self.box_window)

        # show window
        self.setGeometry(50,50,1024,768)
        self.setWindowTitle("Test")
        self.show()

def main():
    app = QApplication(sys.argv)
    main_window = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

推荐答案

一个可能的解决方案是将行中的文本串联起来,并设置为要绘制的文本:

A possible solution is to concatenate the texts in the row and set as the text to be painted:

class ComboBox(QComboBox):
    def paintEvent(self, event):
        painter = QStylePainter(self)
        painter.setPen(self.palette().color(QPalette.Text))
        # draw the combobox frame, focusrect and selected etc.
        opt = QStyleOptionComboBox()
        self.initStyleOption(opt)

        values = []

        for c in range(self.model().columnCount()):
            index = self.model().index(self.currentIndex(), c, self.rootModelIndex())
            values.append(index.data())
        opt.currentText = " ".join(values)
        painter.drawComplexControl(QStyle.CC_ComboBox, opt)
        # draw the icon and text
        painter.drawControl(QStyle.CE_ComboBoxLabel, opt)

这篇关于QComboBox 显示 QTreeView 的整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆