PyQT QTabWidget currentChanged
Asked Answered
L

1

7

I'm trying to build a little app that loads several webpages in an QTabWidget. This already works well. Now I want the tabs / QWebViews to be reloaded when the current Tab is changed.

I think that there is a problem connection the function "onChange" to the currentChanged-Event.

This is my Code:



    #!/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_())


I hope that you can help me solving my problem! Thanks a lot!

Lengel answered 4/2, 2014 at 20:17 Comment(0)
T
16

Check the changes needed on your code here:

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_())
Telephony answered 4/2, 2014 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.