2013-05-18 48 views
0

這是一個pyside GUI,我在2個不同的.py文件中創建了2個面板,main.py和sub.py每個面板都會顯示我們瀏覽器的'QWebView'。目前,當用戶按下main.py上的按鈕時,它會將用戶重定向到一個頁面,例如「www.google」,用戶將不得不點擊sub.py上的按鈕重定向到他們工作的「www.facebook.com」作爲一項獨立的功能。1 QPushButton控制2個不同類的動作

我想問一下,有沒有一種方法可以將用戶按下main.py上的按鈕連接在一起,並且兩個瀏覽器都會一起更改?

回答

1

是的,您可以有多個項目由同一個連接觸發。

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

編輯:繼關於PyQt的使用信號和槽部分另一種答案......

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

這裏是一個辦法做到這一點在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_()) 

希望有所幫助。

+0

感謝您的信息,我想知道如果我有2個不同的網頁瀏覽器窗口的設計,我必須從2個不同的腳本導入? – Wilsonyl

相關問題