Acsinte Florin
Acsinte Florin

Reputation: 11

Cannot add toolbar using PySide (Python)

I need to make a simple browser that has a back button (no adress bar), and can download stuff. atm, it can download(used the code from here:PySide QWebView and downloading unsupported content ), but i cannot add a toolbar in order to add a back button to it. Now, when i run it, it creates 2 windows: one with the browser, one with the toolbar. How can i make them be both in a single window?

import sys
import os
from time import sleep
from PySide import QtCore, QtGui, QtWebKit, QtNetwork

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):      

        hbox = QtGui.QHBoxLayout(self)
        pixmap = QtGui.QPixmap("tux.png")

        lbl = QtGui.QLabel(self)
        lbl.setPixmap(pixmap)

        hbox.addWidget(lbl)
        self.setLayout(hbox)

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Red Rock')
        self.show()

        #self.hide()        

class Browser(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.web = QtWebKit.QWebView()
        self.tb=self.addToolBar("Toolbar")
        self.tb.show()
        self.web.page().setForwardUnsupportedContent(True)
        self.web.page().unsupportedContent.connect(self.download)

        self.manager = QtNetwork.QNetworkAccessManager()
        self.manager.finished.connect(self.finished)
        self.pbar=QtGui.QProgressBar()
        self.pbar.setMaximumWidth(120)


        exitAction = QtGui.QAction(QtGui.QIcon('ceva.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(self.close)


        backAction = QtGui.QAction(QtGui.QIcon('tux.png'), 'Back', self)
        backAction.setShortcut('Ctrl+A')
        backAction.triggered.connect(self.close)


        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)
        self.toolbar.addAction(backAction)

        self.setGeometry(300, 300, 350, 250)
        #self.setWindowTitle('Toolbar')    
        self.show()

    def download(self, reply):
        # this just shows me a picture to know it's downloading
        self.loading=Example()

        self.request = reply.request()
        self.request.setUrl(reply.url())
        self.reply = self.manager.get(self.request)

    def finished(self):
        path = os.path.expanduser(
            os.path.join('~',
                     unicode(self.reply.url().path()).split('/')[-1]))
        if self.reply.hasRawHeader('Content-Disposition'):
            cnt_dis = self.reply.rawHeader('Content-Disposition').data()
            if cnt_dis.startswith('attachment'):
                path = cnt_dis.split('=')[1]

        destination = QtGui.QFileDialog.getSaveFileName(self, "Save", path)
        if destination:
            f = open(destination[0], 'wb')
            f.write(self.reply.readAll())
            f.flush()
            f.close()

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)

    browser = Browser()
    if len(sys.argv) > 1:
        browser.web.load(QtCore.QUrl('http://' + argv[1]))
    else:
        browser.web.load(QtCore.QUrl('http://www.google.com'))

    browser.web.show()

    sys.exit(app.exec_())

Upvotes: 1

Views: 307

Answers (1)

ekhumoro
ekhumoro

Reputation: 120678

You just need to make the webview a child of its parent window. Since the parent is a QMainWindow, you should use setCentralWidget for that:

    self.web = QtWebKit.QWebView()
    self.setCentralWidget(self.web)

Also, you don't need to call show() on widgets that are children of the main window, so you can remove self.tb.show() and self.show() from Browser.__init__, and then just do:

    browser.show()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions