No MainWindow after refactoring
Tony Arnold
tony.arnold at manchester.ac.uk
Sun Jun 20 15:01:21 BST 2021
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_mainWindowfrom PyQt5 import QtWidgets,
> QtCore
> ##from PyQt5.QtCore import QModelIndex, Qt
> import dns.query, dns.resolver, dns.zone, dns.rdataset,
> dns.rdatatypeimport 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__() sel
> f.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.setSe
> ctionResizeMode(QtWidgets.QHeaderView.ResizeToContents) view.setMo
> del(model) mw.show()
> sys.exit(app.exec_())
>
>
> [2]za.py:
> from za_main_ui import Ui_mainWindowfrom .model import ZoneModelfrom
> PyQt5 import QtWidgets
>
> class ZaMainWindow(QtWidgets.QMainWindow,Ui_mainWindow): def
> __init__(self): super(ZaMainWindow,self).__init__() sel
> f.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.setSe
> ctionResizeMode(QtWidgets.QHeaderView.ResizeToContents) view.setMo
> del(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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/f6dce599/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: model.py
Type: text/x-python
Size: 2203 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/f6dce599/attachment-0001.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: za.py
Type: text/x-qml
Size: 850 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/f6dce599/attachment-0002.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4054 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210620/f6dce599/attachment-0003.bin>
More information about the PyQt
mailing list