No MainWindow after refactoring

Axel Rau Axel.Rau at Chaos1.DE
Sun Jun 20 18:41:10 BST 2021


Hi, David and Tony,

you both are right.

I made some mistakes while stripping down my codebase and while preparing it for the ML.
The posted code does not really show the problem, if corrected as you pointed out, Tony, it runs well.
And yes, I'm using PyCharm as IDE and I have a better structured layout [1].

To show the problem, I attach new versions of both files.
The problem happens, if I start creating and loading more zones by calling
model.loadZones() from the __main__ code in za.py (commented out in the attached version).
The debugger shows return from model.loadZones(), but nothing is displayed then.

I guess, model.loadZones() would better be called from some event code, once I’m using events.

Thanks for your help,
Axel

PS: [1]



> Am 20.06.2021 um 16:01 schrieb Tony Arnold <tony.arnold at manchester.ac.uk>:
> 
> Hi Alex,
> 
> So the problem is how you reference ZoneModel in za.py. You're referencing it as model.ZoneModel on line 24. You should reference as just ZoneModel.
> 
> You also need to import sys in za.py, but that's not too critical unless you want to pass command line parameters into your application.
> 
> You also need to import QtCore in model.py as someone else has pointed out.
> 
> I've attached the corrected files.
> 
> I highly recommend using a suitable development environment which will highlight syntax and other errors dynamically. I would suggest using eric or pycharm.
> 
> Regards,
> Tony.
> 
> On Sat, 2021-06-19 at 15:25 +0200, Axel Rau wrote:
>> 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
>> 
> --
> Tony Arnold MBCS, CITP | Senior IT Security Analyst | Directorate of IT Services | Office 1, Kilburn Building | The University of Manchester | Manchester M13 9PL | T: +44 161 275 6093 | M: +44 773 330 0039
> <model.py><za.py>

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/eeb1c7fe/attachment-0004.htm>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: zad_tree.txt
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/eeb1c7fe/attachment-0001.txt>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/eeb1c7fe/attachment-0005.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: za.py
Type: text/x-python-script
Size: 902 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/eeb1c7fe/attachment-0002.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/eeb1c7fe/attachment-0006.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: model.py
Type: text/x-python-script
Size: 4653 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/eeb1c7fe/attachment-0003.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/eeb1c7fe/attachment-0007.htm>
-------------- 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/20210620/eeb1c7fe/attachment-0001.sig>


More information about the PyQt mailing list