[PyQt] Embedding QWidgets within a parent

Chris Wood c.c.wood at gmail.com
Fri Apr 25 13:23:42 BST 2014


Hmm, I've been through that tutorial several times, and I thought I was
getting the hang of stuff, but that tutorial doesn't seem to mention
anywhere anything about how to set parents or children. However, it seems
it might be easier than I thought it was - I hadn't realised that I could
do something like:

class webwidget(QtGui.QWidget):

  def __init__(self):
    super(webwidget, self).__init__()

    self.b = Browser()
    self.b.load(QUrl('http://www.google.com'))

    self.grid = QtGui.QGridLayout()
    self.grid.addWidget(self.b,0,1)

    self.setLayout(self.grid)

class Browser(QWebView):

    def __init__(self):
        QWebView.__init__(self)
        self.loadFinished.connect(self._result_available)

    def _result_available(self, ok):
        frame = self.page().mainFrame()

and that Google now does appear in my gui! :) (n.b. is there a better way
to do it - should I call load() somewhere else?) but, how does the comment
about children / parents relate to this?




On 24 April 2014 20:16, Florian Bruhin <me at the-compiler.org> wrote:

> * Chris Wood <c.c.wood at gmail.com> [2014-04-24 19:29:47 +0100]:
> > If I have a standalone widget, what's the best way to integrate (/embed)
> it
> > within a QMainWindow / parent QWidget? I know that a QScrollArea has a
> > setWidget method - but is there a better / lower level / more abstract
> way
> > of achieving it?
> >
> > (In my particular case, I'm using QtWebKit; using this answer (
> > http://stackoverflow.com/a/13386109/889604) as a starting point, I'd
> like
> > to embed 'view' (which is an instance of Browser) into a parent QWidget.
> > I'd like to think that because QWebView inherits QWidget, this should be
> > easy(!))
>
> The answer almost seems too simple... but you're aware of the
> parent-argument of QObjects (and thus also QWidgets)?
>
> Basically, if you don't give a parent (wv = QWebView()), it's a
> standalone/toplevel widget, and if you do (wv = QWebView(mainwindow)),
> it's inside that widget.
>
> You might really want to take a look at a basic PyQt tutorial though,
> if that really was the problem. I can recommend [1] - even when you
> want to use PyQt5, the examples are trivial to port (just import some
> things from QtWidgets instead of QtGui usually)
>
> [1] http://zetcode.com/gui/pyqt4/
>
> --
> () ascii ribbon campaign - stop html mail    www.asciiribbon.org
> /\ www.the-compiler.org  | I love long mails http://email.is-not-s.ms/
> Joe Cool always spends the first two weeks at college sailing his frisbee.
> --
> Snoopy
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140425/c86c4e91/attachment.html>


More information about the PyQt mailing list