PyQt 自定义小部件未显示 [英] PyQt custom widget not showing

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

问题描述

我是 PyQt 的新手.

I'm new to PyQt.

我正在尝试将 QTableView 放在一个类中,这样我就可以在类中定义它的行为,而不会将它与所有其他代码混合,但是当我这样做时它不会显示.

I'm trying to put a QTableView in a class, so I can define it's behaviour in the class without mixing it with all the other code, but when I do so it just won't show.

这是我正在学习的代码.它是从 [使用 QAbstractTableModel 在 pyqt 中编辑表] 中借用的.稍微重新调整以与 Qt5 一起使用并将 QTableView 移动到一个类中

Here's the code i'm learning from. It was borrowed from [ Edit table in pyqt using QAbstractTableModel ]. Readapted it slightly to use with Qt5 and moved the QTableView in a class

import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QVBoxLayout, QTableView, QWidget
from PyQt5.QtCore import *

# données à représenter
my_array = [['00','01','02'],
            ['10','11','12'],
            ['20','21','22']]

def main():
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

# création de la vue et du conteneur
class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        tablemodel = MyTableModel(my_array, self)
        table = Table(tablemodel)

        layout = QVBoxLayout(self)
        layout.addWidget(table)
        self.setLayout(layout)

# création du modèle

class Table(QWidget):
    def __init__(self, model):
        super().__init__()
        self.model = model
        self.initUI()

    def initUI(self):
        self.setMinimumSize(300,300)
        self.view = QTableView()
        self.view.setModel(self.model)


class MyTableModel(QAbstractTableModel):
    def __init__(self, datain, parent = None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.arraydata = datain

    def rowCount(self, parent):
        return len(self.arraydata)

    def columnCount(self, parent):
        return len(self.arraydata[0])

    def data(self, index, role):
        if not index.isValid():
            return None
        elif role != Qt.DisplayRole:
            return None
        return (self.arraydata[index.row()][index.column()])

    """
    def setData(self, index, value):
        self.arraydata[index.row()][index.column()] = value
        return True
    def flags(self, index):
        return Qt.ItemIsEditable
    """    

if __name__ == "__main__":
    main()

如果我删除课程并使用

table = QTableView()
table.setModel(tablemodel)

表格显示没有问题.

我错过了什么?

推荐答案

问题:table.view 没有父级.如果你添加 self.view.show() 来测试 Table.initUi(),你会得到两个小部件,MyWindow 与空表tmoreau 将 table.view 编写为独立的小部件.

the problem: table.view has no parent. If you add self.view.show() to test to Table.initUi(), you get two widgets, MyWindow with empty table as tmoreau wrote and table.view as isolated widget.

您可以在Table.initUi()

self.view = QTableView(self) 

(然后您不需要布局)或将 table.view 添加到 tmoreau 编写的布局中,然后重新设置 tableview.

(then you don't need a layout) or add table.view to a layout as written by tmoreau, Then the tableview is reparented.

去掉class也是一样的效果,然后把tableview加到layout中.

Removing the class has the same effect, then the tableview is added to layout.

这篇关于PyQt 自定义小部件未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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