显示单个QStandardItem

时间:2019-05-28 14:28:00

标签: python pyside

我有一个使用QStandardItemModel的模型,它由单列QStandardItem组成。我想知道Pyside中是否有任何小部件可用于在QLineEdit之类的东西中显示单个QStandardItem?

我想像这样显示单个QStandardItem:

enter image description here

如果用户希望查看所有项目,则可以稍后使用我已经拥有的QListView打开一个单独的对话框来显示所有此类项目

enter image description here

1 个答案:

答案 0 :(得分:1)

没有可显示模型项信息的小部件,但是您可以基于委托和模型来构建小部件:

from PySide import QtCore, QtGui


class ItemView(QtGui.QWidget):
    def __init__(self, parent=None):
        super(ItemView, self).__init__(parent)
        self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
        self.m_index = QtCore.QModelIndex()
        self.m_model = None
        self.m_icon_size = QtCore.QSize()
        self.m_text_elide_mode = QtCore.Qt.ElideRight
        self.m_wrap_item_text = False

        self.m_delegate = QtGui.QStyledItemDelegate(self)

    def model(self):
        return self.m_model

    def setModel(self, model):
        self.m_model = model
        self.update()

    def setItemDelegate(self, delegate):
        if delegate is None:
            return
        if self.m_delegate.parent() == self:
            self.m_delegate.deleteLater()
        self.m_delegate = delegate
        self.m_delegate.sizeHintChanged.connect(self.update)

    def delegate(self):
        return self.m_delegate

    def index(self):
        return self.m_index

    def setIndex(self, index):
        if index.isValid() and index.model() == self.model():
            self.m_index = index
            self.update()

    def textElideMode(self):
        return self.m_text_elide_mode

    def setTextElideMode(self, mode):
        self.m_text_elide_mode = mode
        self.update()

    def wordWrap(self):
        return self.m_wrap_item_text

    def setWordWrap(self, on):
        self.m_wrap_item_text = on
        self.update()

    def iconSize(self):
        return self.m_icon_size

    def setIconSize(self, size):
        self.m_icon_size = size
        self.update()

    def viewOptions(self):
        option = QtGui.QStyleOptionViewItem()
        option.init(self)
        option.state &= ~QtGui.QStyle.State_MouseOver
        option.font = self.font()
        if not self.hasFocus():
            option.state &= ~QtGui.QStyle.State_Active
        option.state &= ~QtGui.QStyle.State_HasFocus
        if self.m_icon_size.isValid():
            option.decorationSize = self.m_icon_size
        else:
            pm = self.style().pixelMetric(
                QtGui.QStyle.PM_SmallIconSize, None, self
            )
            option.decorationSize = QtCore.QSize(pm, pm)
        option.decorationPosition = QtGui.QStyleOptionViewItem.Left
        option.decorationAlignment = QtCore.Qt.AlignVCenter
        option.textElideMode = self.m_text_elide_mode
        option.rect = QtCore.QRect()
        option.showDecorationSelected = self.style().styleHint(
            QtGui.QStyle.SH_ItemView_ShowDecorationSelected, None, self
        )
        if self.m_wrap_item_text:
            option.features = QtGui.QStyleOptionViewItem.WrapText
        option.locale = self.locale()
        option.locale.setNumberOptions(QtCore.QLocale.OmitGroupSeparator)
        option.Widget = self
        return option

    def paintEvent(self, event):
        if self.m_model is None or not self.m_index.isValid():
            return

        painter = QtGui.QPainter(self)
        option = self.viewOptions()
        option.rect = self.rect()
        cg = QtGui.QPalette.ColorGroup()
        if (
            self.m_model.flags(self.m_index) & QtCore.Qt.ItemIsEnabled
            == QtCore.Qt.NoItemFlags
        ):
            option.state &= ~QtGui.QStyle.State_Enabled
            cg = QtGui.QPalette.Disabled
        else:
            cg = QtGui.QPalette.Normal
        option.palette.setCurrentColorGroup(cg)
        if self.hasFocus():
            option.state |= QtGui.QStyle.State_HasFocus

        self.m_delegate.paint(painter, option, self.m_index)

    def sizeHint(self):
        option = self.viewOptions()
        size = self.m_delegate.sizeHint(option, self.m_index)
        return size


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)

    view = QtGui.QListView()
    model = QtGui.QStandardItemModel(view)
    view.setModel(model)

    icons = [
        "SP_TitleBarMinButton",
        "SP_TitleBarMenuButton",
        "SP_TitleBarMaxButton",
        "SP_TitleBarCloseButton",
        "SP_TitleBarNormalButton",
        "SP_TitleBarShadeButton",
        "SP_TitleBarUnshadeButton",
        "SP_TitleBarContextHelpButton",
        "SP_MessageBoxInformation",
        "SP_MessageBoxWarning",
        "SP_MessageBoxCritical",
        "SP_MessageBoxQuestion",
        "SP_DesktopIcon",
        "SP_TrashIcon",
        "SP_ComputerIcon",
        "SP_DriveFDIcon",
        "SP_DriveHDIcon",
        "SP_DriveCDIcon",
        "SP_DriveDVDIcon",
        "SP_DriveNetIcon",
        "SP_DirHomeIcon",
        "SP_DirOpenIcon",
        "SP_DirClosedIcon",
        "SP_DirIcon",
        "SP_DirLinkIcon",
        "SP_FileIcon",
        "SP_FileLinkIcon",
        "SP_FileDialogStart",
        "SP_FileDialogEnd",
        "SP_FileDialogToParent",
        "SP_FileDialogNewFolder",
        "SP_FileDialogDetailedView",
        "SP_FileDialogInfoView",
        "SP_FileDialogContentsView",
        "SP_FileDialogListView",
        "SP_FileDialogBack",
        "SP_DockWidgetCloseButton",
        "SP_ToolBarHorizontalExtensionButton",
        "SP_ToolBarVerticalExtensionButton",
        "SP_DialogOkButton",
        "SP_DialogCancelButton",
        "SP_DialogHelpButton",
        "SP_DialogOpenButton",
        "SP_DialogSaveButton",
        "SP_DialogCloseButton",
        "SP_DialogApplyButton",
        "SP_DialogResetButton",
        "SP_DialogDiscardButton",
        "SP_DialogYesButton",
        "SP_DialogNoButton",
        "SP_ArrowUp",
        "SP_ArrowDown",
        "SP_ArrowLeft",
        "SP_ArrowRight",
        "SP_ArrowBack",
        "SP_ArrowForward",
        "SP_CommandLink",
        "SP_VistaShield",
        "SP_BrowserReload",
        "SP_BrowserStop",
        "SP_MediaPlay",
        "SP_MediaStop",
        "SP_MediaPause",
        "SP_MediaSkipForward",
        "SP_MediaSkipBackward",
        "SP_MediaSeekForward",
        "SP_MediaSeekBackward",
        "SP_MediaVolume",
        "SP_MediaVolumeMuted",
        "SP_CustomBase",
    ]
    for icon_name in icons:
        icon = QtGui.QApplication.style().standardIcon(
            getattr(QtGui.QStyle, icon_name)
        )
        it = QtGui.QStandardItem(icon, icon_name)
        model.appendRow(it)

    item_view = ItemView()
    item_view.setModel(model)

    view.selectionModel().currentChanged.connect(item_view.setIndex)
    view.setCurrentIndex(model.index(0, 0))

    w = QtGui.QWidget()
    lay = QtGui.QVBoxLayout(w)
    lay.addWidget(view)
    lay.addWidget(item_view)
    w.show()
    sys.exit(app.exec_())

enter image description here