No MainWindow after refactoring

Axel Rau Axel.Rau at Chaos1.DE
Sat Jun 19 14:25:45 BST 2021


Hi all,

I’m new to qt and pyqt.

The test app [1] in one module works fine (mainwindow opens and shows loaded data).
After putting ZoneModel and Zone classes in separate modules (za.py, mode.py)[2], no MainWindow is displayed, just the app icon.

What am I doing wrong?

Any help appreciated,
Axel

[1]

from za_main_ui import Ui_mainWindow
from PyQt5 import QtWidgets, QtCore

##from PyQt5.QtCore import QModelIndex, Qt

import dns.query, dns.resolver, dns.zone, dns.rdataset, dns.rdatatype
import sys

windowHeading = 'za - DNS zone admin'

class ZoneModel(QtCore.QAbstractTableModel):
    def __init__(self, data=[[]], parent=None):
        super().__init__(parent)
        self.zone = Zone('some.do.main‘)

    def headerData(self, section: int, orientation: QtCore.Qt.Orientation, role: int):
        if role == QtCore.Qt.DisplayRole:
            if orientation == QtCore.Qt.Horizontal:
                return ['OwnerName', 'TTL', 'Type', 'Rdata'][section]
            else:
                return None

    def columnCount(self, parent=None):
        return self.zone.columnCount()

    def rowCount(self, parent=None):
        return self.zone.rowCount()

    def data(self, index: QtCore.QModelIndex, role: int):
        if role == QtCore.Qt.DisplayRole:
            row = index.row()
            col = index.column()
            return self.zone.data(row, col)


class Zone(object):
    IP_XFR_NS = '1234:5678:9abc:def::1'

    def __init__(self, zone_name):
        self.zone_name = zone_name
        self.z = dns.zone
        self.d = [['', '', '', '']]

    def data(self, row: int, column: int) -> str:
        if not self.d:
            self.loadZone()
        v = self.d[row][column]
        if not v: v = ''
        return str(v)

    def loadZone(self):
        row = 0
        self.z = dns.zone.from_xfr(dns.query.xfr(self.IP_XFR_NS, self.zone_name))

        for name in self.z.keys():
            name = str(name)
            if name == '@': name = ''
            self.d[row][0] = name
            node = self.z[name]
            for the_rdataset in node:
                self.d[row][1] = str(the_rdataset.ttl)
                for rdata in the_rdataset:
                    self.d[row][2] = dns.rdatatype.to_text(the_rdataset.rdtype)
                    self.d[row][3] = str(rdata)
                    row = row + 1
                    self.d.append(['', '', '', ''])


    def columnCount(self):
        if len(self.d) < 2:
            self.loadZone()
        return len(self.d[0])

    def rowCount(self):
        if len(self.d) < 2:
            self.loadZone()
        return len(self.d)


class ZaMainWindow(QtWidgets.QMainWindow,Ui_mainWindow):
    def __init__(self):
        super(ZaMainWindow,self).__init__()
        self.setupUi(self)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    domainZones = {}
    ip4Zones = {}
    ip6Zones = {}

    subdomains = {}
    ip4Nets = {}
    ip6Nets = {}



    mw = ZaMainWindow()
    model = ZoneModel()
    view = mw.maintableView
    view.setColumnWidth(0, 200)
    view.setColumnWidth(1, 40)
    view.setColumnWidth(2, 40)
    view.setColumnWidth(3, 400)
    hh = view.horizontalHeader()
    hh.setStretchLastSection(True)
    hh.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
    view.setModel(model)
    mw.show()

    sys.exit(app.exec_())



[2]
za.py:

from za_main_ui import Ui_mainWindow
from .model import ZoneModel
from PyQt5 import QtWidgets


class ZaMainWindow(QtWidgets.QMainWindow,Ui_mainWindow):
    def __init__(self):
        super(ZaMainWindow,self).__init__()
        self.setupUi(self)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    domainZones = {}
    ip4Zones = {}
    ip6Zones = {}

    subdomains = {}
    ip4Nets = {}
    ip6Nets = {}



    mw = ZaMainWindow()
    model = model.ZoneModel()
    view = mw.maintableView
    view.setColumnWidth(0, 200)
    view.setColumnWidth(1, 40)
    view.setColumnWidth(2, 40)
    view.setColumnWidth(3, 400)
    hh = view.horizontalHeader()
    hh.setStretchLastSection(True)
    hh.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
    view.setModel(model)
    mw.show()

    sys.exit(app.exec_())

model.py:

import dns.query, dns.resolver, dns.zone, dns.rdataset, dns.rdatatype

windowHeading = 'za - DNS zone admin'

class ZoneModel(QtCore.QAbstractTableModel):
    def __init__(self, data=[[]], parent=None):
        super().__init__(parent)
        self.zone = Zone('some.do.main')

    def headerData(self, section: int, orientation: QtCore.Qt.Orientation, role: int):
        if role == QtCore.Qt.DisplayRole:
            if orientation == QtCore.Qt.Horizontal:
                return ['OwnerName', 'TTL', 'Type', 'Rdata'][section]
            else:
                return None

    def columnCount(self, parent=None):
        return self.zone.columnCount()

    def rowCount(self, parent=None):
        return self.zone.rowCount()

    def data(self, index: QtCore.QModelIndex, role: int):
        if role == QtCore.Qt.DisplayRole:
            row = index.row()
            col = index.column()
            return self.zone.data(row, col)


class Zone(object):
    IP_XFR_NS = '1234:5678:9abc:def::1‘

    def __init__(self, zone_name):
        self.zone_name = zone_name
        self.z = dns.zone
        self.d = [['', '', '', '']]

    def data(self, row: int, column: int) -> str:
        if not self.d:
            self.loadZone()
        v = self.d[row][column]
        if not v: v = ''
        return str(v)

    def loadZone(self):
        row = 0
        self.z = dns.zone.from_xfr(dns.query.xfr(self.IP_XFR_NS, self.zone_name))

        for name in self.z.keys():
            name = str(name)
            if name == '@': name = ''
            self.d[row][0] = name
            node = self.z[name]
            for the_rdataset in node:
                self.d[row][1] = str(the_rdataset.ttl)
                for rdata in the_rdataset:
                    self.d[row][2] = dns.rdatatype.to_text(the_rdataset.rdtype)
                    self.d[row][3] = str(rdata)
                    row = row + 1
                    self.d.append(['', '', '', ''])


    def columnCount(self):
        if len(self.d) < 2:
            self.loadZone()
        return len(self.d[0])

    def rowCount(self):
        if len(self.d) < 2:
            self.loadZone()
        return len(self.d)



---
PGP-Key: CDE74120  ☀  computing @ chaos claudius

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210619/a73c7c14/attachment.sig>


More information about the PyQt mailing list