Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQT QTabWidget currentChanged

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!

like image 211
user3272502 Avatar asked Feb 04 '14 20:02

user3272502


1 Answers

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_())
like image 98
Alvaro Fuentes Avatar answered Sep 23 '22 02:09

Alvaro Fuentes