[PyQt] A Hopefully Silly Question from a Beginner

Patrick Barrett patrick at mkii.org
Mon Sep 16 18:18:14 BST 2013


On 09/16/2013 10:58 AM, Ryan Hanson wrote:
> On Mon, Sep 16, 2013 at 10:19 AM, Patrick Barrett <patrick at mkii.org 
> <mailto:patrick at mkii.org>> wrote:
>
>     I'm trying to write a small app the generates a few QWebView
>     windows and then rotates the page that is displayed on a set interval.
>
>     Code: https://gist.github.com/Azdle/6556433
>
>     I've run into an issue that I don't understand. When I try to
>     generate a `PageWindow` (Class based on QWebView) from within my
>     `MainWindow` class nothing appears. (See the code in 29-30)
>     However, when I copy and paste that code outside of any classes
>     the window appears as it should. (See the commented code in 56-57)
>
>     Even when I can make the QWebView window appear, the QTimer object
>     that I create in the `PageWindow` never seems to timeout. I think
>     this is being caused by the same issue as the first problem.
>
>     I'm not sure if the issues are me doing the python wrong or just
>     not understanding something about how the Qt-specfic aspects work.
>     I have this application already working in C++, but want to get it
>     working in Python.
>
>     Thanks
>     --Patrick
>
>
> Here you go Patrick, you want to move your timer into your main class 
> and then call your new page function on the instance of PageWindow 
> that you create inside that class.
>
> import sys
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> from PyQt4.QtWebKit import *
> pages = [
>       "https://portals.exosite.com/views/3819798770/3579834479",
>       "https://portals.exosite.com/views/2562112089/4128069106",
>       "https://portals.exosite.com/views/2060811893/1760385000",
>       "https://exosite:AKYQhkDqj4@logs.exosite.com:444"
> ]
> class MainWindow(QMainWindow):
>     def __init__(self, *args):
>         QMainWindow.__init__(self)
>         self.quitButton = QPushButton("Quit")
>         vbox = QVBoxLayout()
>         vbox.addWidget(self.quitButton)
>         centralWidget = QWidget()
>         centralWidget.setLayout(vbox)
>         self.setCentralWidget(centralWidget)
>         self.quitButton.clicked.connect(self.close)
>         self.pageView = PageWindow()
>         self.pageView.show()
>         self.pageTimer = QTimer()
> self.pageTimer.timeout.connect(self.pageView.nextPage)
>         self.pageTimer.start(5000)
>     def closeEvent(self, event):
>         self.pageTimer.stop()
>         self.pageView.close()
>         event.accept()
> class PageWindow(QWebView):
>     def __init__(self):
>         QWebView.__init__(self)
>         self.pageIndex = 0
>         self.nextPage()
> self.setGeometry(QApplication.desktop().screenGeometry(1))
>         self.showFullScreen()
>     def nextPage(self):
>         print("Loading:"+pages[self.pageIndex])
>         self.load(QUrl(pages[self.pageIndex]))
>         self.pageIndex = self.pageIndex + 1
>         if self.pageIndex >= len(pages):
>             self.pageIndex = 0
> def main(args):
>     app = QApplication(args)
>     win = MainWindow()
>     win.show()
>     app.connect(app, SIGNAL("lastWindowClosed()"),
>                 app, SLOT("quit()"))
>     app.exec_()
> if __name__=="__main__":
>         main(sys.argv)

Thanks Ryan. I ended up moving the timer object back into the 
`PageWindow` class.

Added a new revision to the old Gist: https://gist.github.com/Azdle/6556433

I see from your code that my issue was that I was putting object in 
local scope instead of putting them in the object (using var instead of 
self.var, my terminology is probably wrong) so the objects were getting 
garbage collected right after getting created.

Thanks for your help!

--Patrick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130916/9a8e83ae/attachment.html>


More information about the PyQt mailing list