开发者

How to make a QListView resize its items according to editor size

I have a qlistview whose editing is made in a qtableview, by means of a delegate. When the qtableview shows up, I'd like the qlistview to resize its row to fit all of qtableview. Here is the code:

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开发者_StackOverflow社区)
    mw = MyApp()
    mw.show()
    app.exec_()

if __name__=="__main__":
    main()


The widget you should be trying to resize is the editor, that being said

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()

Other thing is that with QMainWindow you should be using setCentralWidget

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜