如何根据编辑器大小使QListView调整其项目大小

时间:2011-01-11 13:58:38

标签: python qt pyqt qlistview

我有一个qlistview,通过委托在qtableview中进行编辑。当qtableview显示时,我希望qlistview调整其行以适应所有qtableview。这是代码:

import sys
from PyQt4 import QtGui, QtCore

class MyDelegate(QtGui.QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        mtview = QtGui.QTableView(parent)
        mtview.verticalHeader().setVisible(False)
        mtview.horizontalHeader().setVisible(False)
        mtmodel = QtGui.QStandardItemModel()
        mtview.setModel(mtmodel)
        return mtview
    def setEditorData(self, editor, modelindex):
        editor.parent().setMinimumSize(400,400)
        mtmodel = editor.model()
        for x in range(5):
            for y in range(5):
                mtmodel.setItem(x,y,QtGui.QStandardItem("%s, %s" % (x, y)))

class MyApp(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.resize(200, 200)
        self.listview = QtGui.QListView(self)
        self.listview.setGeometry(QtCore.QRect(0,0,200,200))
        self.listview.setItemDelegate(MyDelegate())
        listmodel = QtGui.QStandardItemModel()
        t = '123456789'
        for x in range(10):
            itemmodel=QtGui.QStandardItem(t)
            listmodel.appendRow(itemmodel)
        self.listview.setModel(listmodel)

def main():
    app = QtGui.QApplication(sys.argv)
    mw = MyApp()
    mw.show()
    app.exec_()

if __name__=="__main__":
    main()

1 个答案:

答案 0 :(得分:0)

您应该尝试调整大小的小部件是编辑器,就是说

import sys
from PyQt4 import QtGui, QtCore

class MyDelegate(QtGui.QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        mtview = QtGui.QTableView(parent)
        mtview.verticalHeader().setVisible(False)
        mtview.horizontalHeader().setVisible(False)
        mtmodel = QtGui.QStandardItemModel()
        mtview.setModel(mtmodel)
        return mtview

    def setEditorData(self, editor, modelindex):
        editor.resize(400,400)
        mtmodel = editor.model()
        for x in range(5):
            for y in range(5):
                mtmodel.setItem(x,y,QtGui.QStandardItem("%s, %s" % (x, y)))

class MyApp(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyApp, self).__init__(parent)
        self.setMinimumSize(200, 200)
        self.listview = QtGui.QListView()
        self.listview.setItemDelegate(MyDelegate())
        listmodel = QtGui.QStandardItemModel()

        self.setCentralWidget(self.listview)
        t = '123456789'
        for x in range(10):
            itemmodel=QtGui.QStandardItem(t)
            listmodel.appendRow(itemmodel)
        self.listview.setModel(listmodel)

def main():
    app = QtGui.QApplication(sys.argv)
    mw = MyApp()
    mw.show()
    app.exec_()

if __name__=="__main__":
    main()

其他的是,使用QMainWindow你应该使用setCentralWidget