PDA

View Full Version : Using click() on buttonWidget to change the current tab on tabWidget



zingzingint
23rd February 2019, 08:24
?Good afternoon from Thailand

I would like to have some help which it's about the socket in Qt Designer for python.

So my question is, is it possible to use click() on buttonWidget to change the current tab on tabWidget ?
(I'm trying to find some solution in the forum but not found yet)

Thank you in advance for helps

Zingzing

13029
13030

anda_skoa
23rd February 2019, 13:01
In C++ it is possible to connect to a lambda function which would then call the setCurrentIndex() method.

Maybe that is also possible in Python.

Cheers,
_

ChrisW67
24th February 2019, 04:32
If it is only one or two buttons then use one of the two methods, explicit slot method or lambda function, shown in this example:


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTabWidget, QPushButton
from PyQt5.QtGui import QIcon

class App(QWidget):

def __init__(self):
super().__init__()
self.title = 'Test'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.layout = QVBoxLayout(self)
self.tabWidget = QTabWidget(self)
self.tabWidget.addTab(QWidget(), "Page 1")
self.tabWidget.addTab(QWidget(), "Page 2")
self.button1 = QPushButton("Button 1", self)
self.button1.clicked.connect(lambda: self.tabWidget.setCurrentIndex(0))
self.button2 = QPushButton("Button 2", self)
self.button2.clicked.connect(self.button2_clicked)
self.layout.addWidget(self.tabWidget)
self.layout.addWidget(self.button1)
self.layout.addWidget(self.button2)
self.setLayout(self.layout)
self.show()

def button2_clicked(self):
self.tabWidget.setCurrentIndex(1)

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())


If you have several buttons that select different tabs in the tab widget then you could connect the buttons clicked() signals into a QSignalMapper, give each input an integer mapping, and connect the mapper's output signal to the tab widget's setCurrentIndex() slot.