0

I see some code write in c++ style, and I try to write it in python style.
I write a table model in python file, and then pass it to qml file.
But when I run my main.py file, The windows show nothing.
And my program don't show any error and I don't know what's wrong here? Can someone know the reason?

main.py

import os
from pathlib import Path
import sys

from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine

from PyQt5.QtCore import QAbstractTableModel, Qt


class TableModel(QAbstractTableModel):
    def __init__(self):
        super().__init__()

    def rowCount(self, parent):
        return 10

    def columnCount(self, parent) -> int:
        return 10

    def data(self, index, role: int):
        if index.isValid() and role == Qt.DisplayRole:
            return f"{index.row()},{index.column()}"

        return None

if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.rootContext().setContextProperty('myModel', TableModel())
    engine.load(os.fspath(Path(__file__).resolve().parent / "table.qml"))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

table.qml

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 320
    height: 320
    visible: true

    TableView {
        anchors.fill: parent
        rowSpacing: 5
        columnSpacing: 5
        model: myModel
        delegate: myDele
    }

    Component {
        id: myDele
        Rectangle {
            implicitHeight: 50
            implicitWidth: 50
            width: 50
            height: 50
            color: "#abc"
            Text {
                anchors.centerIn: parent
                text: display
            }
        }
    }

}

Result
enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
jett chen
  • 1,067
  • 16
  • 33

1 Answers1

1

The problem is caused by memory management, it is better to assign to a variable than to pass the object directly to a method since it should be assumed that the object will be used but its memory will not be managed, unless the docs indicate.

In this case you must save the object in a variable and then use it:

myModel = TableModel()
engine.rootContext().setContextProperty('myModel', myModel)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • It's just like I had pass a temporary var to it, and qt had not hold my var?This writing style is redundant for ptyhon user. – jett chen Aug 05 '21 at 01:25
  • @jett No, Qt does not indicate that it will have ownership of the object that is passed to the setContextProperty method. – eyllanesc Aug 05 '21 at 01:28
  • @jett redundant? Non-redundant code is not the same as less code, in python the explicit is preferred over the implicit, so declaring an object is the best as it makes it readable. I don't see which python rule has been broken, could you tell me where you got that information from? Is it part of any PEP? – eyllanesc Aug 05 '21 at 01:31
  • No any infomation point to this,It's my personal opinion. – jett chen Aug 05 '21 at 02:42
  • 1
    @jett You should then say: *In my opinion ...* , since when you use "python user" it seems that you indicate that it is a community agreement (or at least a very widespread practice). – eyllanesc Aug 05 '21 at 02:59
  • Fine, I will remember it in later. – jett chen Aug 05 '21 at 03:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235641/discussion-between-jett-and-eyllanesc). – jett chen Aug 05 '21 at 03:35