Wilsonyl
Wilsonyl

Reputation: 1

1 QPushButton control 2 action from 2 different class

This is a pyside GUI, I have created 2 panel in 2 different .py files, main.py and sub.py each panel will display a we browser 'QWebView'. Currently when user press the button on main.py it will redirect the user to a page e.g "www.google" and user will have to click the button on sub.py to be redirected to e.g"www.facebook.com" they work as a indipendent function.

I would like to ask is there a way to link both together where user press the button on main.py and both webbrower will change together?

Upvotes: 0

Views: 337

Answers (1)

phyatt
phyatt

Reputation: 19152

Yes, you can have multiple items triggered by the same connection.

QObject::connect(myButton, SIGNAL(clicked()), 
                     this,  SLOT(launchGoogleSiteOnBrowserA());
QObject::connect(myButton, SIGNAL(clicked()), 
                     pointerToOtherClass, SLOT(launchFacebookSiteOnBrowserB());

http://qt-project.org/doc/qt-4.8/signalsandslots.html

EDIT: Following some another answer about using signals and slots in PyQt...

https://stackoverflow.com/a/7618282/999943

Here is a way to do it in PyQt:

widget.pyw

from PyQt4 import QtCore, QtGui
from mybrowser import Browser

class Widget(QtGui.QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        self.myButton = QtGui.QPushButton('Open Facebook and Google')
        self.myHLayout = QtGui.QHBoxLayout()

        self.myVLayout = QtGui.QVBoxLayout()
        self.myVLayout.addWidget(self.myButton)

        url = QtCore.QUrl('http://www.yahoo.com')
        self.browserLHS = Browser(url)
        self.browserRHS = Browser(url)

        self.myHLayout.addWidget(self.browserLHS)
        self.myHLayout.addWidget(self.browserRHS)

        QtCore.QObject.connect(self.myButton, QtCore.SIGNAL("clicked()"), self.changePageOnBothBrowsers )

        self.myVLayout.addLayout(self.myHLayout)

        self.setLayout(self.myVLayout)

    def changePageOnBothBrowsers(self):
        self.browserLHS.load(QtCore.QUrl.fromUserInput('google.com'))
        self.browserRHS.load(QtCore.QUrl.fromUserInput('facebook.com'))

if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)

    widget = Widget()
    widget.show()

    sys.exit(app.exec_())

mybrowser.pyw

from PyQt4 import QtCore, QtGui, QtNetwork, QtWebKit

import jquery_rc

class Browser(QtWebKit.QWebView):
    def __init__(self, url):
        super(Browser, self).__init__()

        self.progress = 0

        fd = QtCore.QFile(":/jquery.min.js")

        if fd.open(QtCore.QIODevice.ReadOnly | QtCore.QFile.Text):
            self.jQuery = QtCore.QTextStream(fd).readAll()
            fd.close()
        else:
            self.jQuery = ''

        QtNetwork.QNetworkProxyFactory.setUseSystemConfiguration(True)


        self.load(url)
        self.loadFinished.connect(self.adjustLocation)
        self.titleChanged.connect(self.adjustTitle)
        self.loadProgress.connect(self.setProgress)
        self.loadFinished.connect(self.finishLoading)

        self.locationEdit = QtGui.QLineEdit(self)
        self.locationEdit.setSizePolicy(QtGui.QSizePolicy.Expanding,
                self.locationEdit.sizePolicy().verticalPolicy())
        self.locationEdit.returnPressed.connect(self.changeLocation)

    def adjustLocation(self):
        self.locationEdit.setText(self.url().toString())

    def changeLocation(self):
        url = QtCore.QUrl.fromUserInput(self.locationEdit.text())
        self.load(url)
        self.setFocus()

    def adjustTitle(self):
        if 0 < self.progress < 100:
            self.setWindowTitle("%s (%s%%)" % (self.title(), self.progress))
        else:
            self.setWindowTitle(self.title())

    def setProgress(self, p):
        self.progress = p
        self.adjustTitle()

    def finishLoading(self):
        self.progress = 100
        self.adjustTitle()
        self.page().mainFrame().evaluateJavaScript(self.jQuery)

#if __name__ == '__main__':
#
#    import sys
#
#    app = QtGui.QApplication(sys.argv)
#
#    if len(sys.argv) > 1:
#        url = QtCore.QUrl(sys.argv[1])
#    else:
#        url = QtCore.QUrl('http://www.google.com/ncr')
#
#    browser = Browser(url)
#    browser.show()
#
#    sys.exit(app.exec_())

Hope that helps.

Upvotes: 1

Related Questions