Results 1 to 3 of 3

Thread: Using click() on buttonWidget to change the current tab on tabWidget

  1. #1
    Join Date
    Feb 2019
    Posts
    1
    Qt products
    Platforms
    Windows

    Default Using click() on buttonWidget to change the current tab on tabWidget

    ?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

    1550909695791.jpg
    1550909674982.jpg

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using click() on buttonWidget to change the current tab on tabWidget

    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,
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using click() on buttonWidget to change the current tab on tabWidget

    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:
    Qt Code:
    1. import sys
    2. from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTabWidget, QPushButton
    3. from PyQt5.QtGui import QIcon
    4.  
    5. class App(QWidget):
    6.  
    7. def __init__(self):
    8. super().__init__()
    9. self.title = 'Test'
    10. self.left = 10
    11. self.top = 10
    12. self.width = 640
    13. self.height = 480
    14. self.initUI()
    15.  
    16. def initUI(self):
    17. self.setWindowTitle(self.title)
    18. self.setGeometry(self.left, self.top, self.width, self.height)
    19. self.layout = QVBoxLayout(self)
    20. self.tabWidget = QTabWidget(self)
    21. self.tabWidget.addTab(QWidget(), "Page 1")
    22. self.tabWidget.addTab(QWidget(), "Page 2")
    23. self.button1 = QPushButton("Button 1", self)
    24. self.button1.clicked.connect(lambda: self.tabWidget.setCurrentIndex(0))
    25. self.button2 = QPushButton("Button 2", self)
    26. self.button2.clicked.connect(self.button2_clicked)
    27. self.layout.addWidget(self.tabWidget)
    28. self.layout.addWidget(self.button1)
    29. self.layout.addWidget(self.button2)
    30. self.setLayout(self.layout)
    31. self.show()
    32.  
    33. def button2_clicked(self):
    34. self.tabWidget.setCurrentIndex(1)
    35.  
    36. if __name__ == '__main__':
    37. app = QApplication(sys.argv)
    38. ex = App()
    39. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    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.

  4. The following user says thank you to ChrisW67 for this useful post:

    anda_skoa (24th February 2019)

Similar Threads

  1. How to ignore current item change in QListWidget?
    By GNU/Varan in forum Qt Programming
    Replies: 9
    Last Post: 19th July 2011, 20:42
  2. How to change current line format in QTextEdit?
    By FinderCheng in forum Qt Programming
    Replies: 5
    Last Post: 25th March 2011, 10:41
  3. QAbstractItemModel double click on current row
    By core_st in forum Qt Programming
    Replies: 8
    Last Post: 27th January 2011, 17:39
  4. Replies: 2
    Last Post: 9th August 2010, 16:35
  5. Working out table's current row from pushbutton click
    By shooogun in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2008, 00:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.