PyQT QTabWidget currentChanged

时间:2014-02-04 20:17:37

标签: python signals qtabwidget slot

我正在尝试构建一个在QTabWidget中加载多个网页的小应用程序。这已经很好了。现在我希望在更改当前Tab时重新加载tabs / QWebViews。

我认为连接功能有问题" onChange"到currentChanged-Event。

这是我的代码:



    #!/usr/bin/env python

    import sys, os
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from PyQt4.QtWebKit import *
    from PyQt4.QtNetwork import *
    from PyQt4 import QtCore, QtGui, QtWebKit
    from PyQt4.QtWebKit import QWebView


    class BaseWindow(QtGui.QMainWindow):
        def __init__(self, parent = None):
            QtGui.QMainWindow.__init__(self, parent)
            self.centralWidget = QtGui.QWidget()
            self.resize(800, 500)
            self.setWindowTitle('Test')
            self.tabs = QTabWidget()
            #self.tabs.connect(self.tabs,SIGNAL("currentChanged(int)"),self,SLOT("onChange(int)")) #tabs,SLOT("tabChangedSlot(int)")
            #self.tabs.currentChanged.connect(self.onChange)


            self.webview = QWebView()
            self.webview.load(QUrl("http://gmx.de"))

            self.webview2 = QWebView()
            self.webview2.load(QUrl("http://web.de"))

            centralLayout = QtGui.QVBoxLayout()
            centralLayout.addWidget(self.tabs, 1)

            self.tabs.addTab(self.webview, "gmx")
            self.tabs.addTab(self.webview2, "web")
            self.centralWidget.setLayout(centralLayout)

            self.setCentralWidget(self.centralWidget)

        #@pyqtSlot()  
        def onChange(self):
            QtGui.QMessageBox.information(self,
                      "Tab Index Changed!",
                      "Current Tab Index: ");





    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        window = BaseWindow()
        window.show()
        sys.exit(app.exec_())


我希望你能帮助我解决我的问题!非常感谢!

1 个答案:

答案 0 :(得分:9)

在此处检查代码所需的更改:

import sys, os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtWebKit import QWebView


class BaseWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent)
        self.centralWidget = QtGui.QWidget()
        self.resize(800, 500)
        self.setWindowTitle('Test')
        self.tabs = QTabWidget()

        self.tabs.blockSignals(True) #just for not showing the initial message
        self.tabs.currentChanged.connect(self.onChange) #changed!


        self.webview = QWebView()
        self.webview.load(QUrl("http://gmx.de"))

        self.webview2 = QWebView()
        self.webview2.load(QUrl("http://web.de"))

        centralLayout = QtGui.QVBoxLayout()
        centralLayout.addWidget(self.tabs, 1)

        self.tabs.addTab(self.webview, "gmx")
        self.tabs.addTab(self.webview2, "web")
        self.centralWidget.setLayout(centralLayout)

        self.setCentralWidget(self.centralWidget)

        self.tabs.blockSignals(False) #now listen the currentChanged signal


    #@pyqtSlot()  
    def onChange(self,i): #changed!
        QtGui.QMessageBox.information(self,
                  "Tab Index Changed!",
                  "Current Tab Index: %d" % i ) #changed!

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = BaseWindow()
    window.show()
    sys.exit(app.exec_())