Py学习  »  Python

如何在python中为组合框选择正确的列

Johnny Palomino • 4 年前 • 506 次点击  

我有一个数据库(“texpalsac”)、qtdesigner(ventas.ui)、一个表(products):

 PRODUCTS 
COD   NAME
111   bag
112   shoes
121   pants
122   t-shirts

我希望这个组合框(comboard)显示第二列(“name”),但我不知道该怎么做。目前只显示第一列(“COD”:11111 2…)。 谢谢

class MiFormulario(QDialog, QComboBox):
    def __init__(self, parent=None):
        super(MiFormulario, self).__init__(parent)
        uic.loadUi('Venta.ui', self)

        self.model = QtSql.QSqlTableModel (self)
        self.model.setTable ("products")
        self.model.select ()
        self.comboArt.setModel (self.model)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38262
 
506 次点击  
文章 [ 1 ]  |  最新文章 4 年前
eyllanesc
Reply   •   1 楼
eyllanesc    5 年前

你必须使用 setModelColumn() 方法 QComboBox 要指示要显示的列:

class MiFormulario(QDialog): # <-- remove QComboBox, it is unnecessary
    def __init__(self, parent=None):
        super(MiFormulario, self).__init__(parent)
        uic.loadUi('Venta.ui', self)

        self.model = QtSql.QSqlTableModel(self)
        self.model.setTable("products")
        self.model.select()
        self.comboArt.setModel(self.model)
        self.comboArt.setModelColumn(1) # <--- select column 1

加:

如果要显示包含两列的QcomboBox,则必须创建自定义QcomboBox,如下所示:

多芯盒.py

from PyQt5 import QtCore, QtGui, QtWidgets


class CustomView(QtWidgets.QTableView):
    def __init__(self, parent=None):
        super(CustomView, self).__init__(parent)
        self.verticalHeader().hide()
        self.horizontalHeader().hide()
        self.setShowGrid(False)
        self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
        self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)

    def adjustWidth(self):
        w = sum([self.horizontalHeader().sectionSize(i) for i in range(self.horizontalHeader().count())])
        self.setFixedWidth(w)
        self.parentWidget().setFixedWidth(w)

    def showEvent(self, e):
        if self.isVisible():
            self.adjustWidth()
        super(CustomView, self).showEvent(e)

class MultiComboBox(QtWidgets.QComboBox):
    def setModel(self, model):
        super(MultiComboBox, self).setModel(model)
        view = CustomView(self)
        self.setView(view)

    def paintEvent(self, e):
        painter = QtWidgets.QStylePainter(self)
        painter.setPen(self.palette().color(QtGui.QPalette.Text))
        opt = QtWidgets.QStyleOptionComboBox()
        self.initStyleOption(opt)
        if self.model():
            p = self.rootModelIndex()
            t = ""
            for c in range(self.model().columnCount(p)):
                t += " " + self.model().index(self.currentIndex(), c).data()
            opt.currentText = t
            fm = QtGui.QFontMetrics(self.font())
            self.setMinimumWidth(fm.width(t))
        painter.drawComplexControl(QtWidgets.QStyle.CC_ComboBox, opt)
        painter.drawControl(QtWidgets.QStyle.CE_ComboBoxLabel, opt)

如果你想在qt设计器中使用它,你必须升级它,为此你可以查看 this answer .