Drees
Drees

Reputation: 720

Get ID of dynamically created button when clicked

I have a program that dynamically creates tabs with buttons on them, when the user clicks button, I want it to give me the button_id (number that corresponds to the tab index).

I understand that you can do something like tabwidget.currentIndex() to get index of tab being used, but I don't want that as I will eventually have a method that iterates through the number of tabs and access each button without selecting the tabs as shown below.

    for i in range(1,self.tabWidget.count()):
        self.tabWidget.widget(i).stagematch.click()

For example:

If user clicks 'Clear Text' button on 'Tab 2' then I want it to give me the number 2 back.

How can I accomplish this without using the currentIndex() method for the tabs

Test code:

import sys
from PyQt5 import QtCore, QtWidgets

class TabPage(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        group = QtWidgets.QGroupBox('Monty Python')
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(group)
        grid = QtWidgets.QGridLayout(group)
        testbutton = QtWidgets.QPushButton('Clear Text')
        grid.addWidget(testbutton, 2, 2)

        testbutton.clicked.connect(self.tab_match)
        #testbutton.clicked.connect(self.button_id)

    def button_id(self):
        sender = self.sender()
        print(sender.text()) # Gives text of button, i'd like a number that corresponds to the tab# that called it

    def tab_match(self,button_id):
        #Do something with button ID here
        pass

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.tabs = QtWidgets.QTabWidget()
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.tabs)
        button = QtWidgets.QToolButton()
        button.setToolTip('Add New Tab')
        button.clicked.connect(self.addNewTab)
        button.setIcon(self.style().standardIcon(
            QtWidgets.QStyle.SP_DialogYesButton))
        self.tabs.setCornerWidget(button, QtCore.Qt.TopRightCorner)
        self.addNewTab()

    def addNewTab(self):
        text = 'Tab %d' % (self.tabs.count() + 1)
        self.tabs.addTab(TabPage(self.tabs), text)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 300, 200)
    window.show()
    sys.exit(app.exec_())

Upvotes: 1

Views: 530

Answers (1)

S. Nick
S. Nick

Reputation: 13651

Try it:

import sys
from PyQt5 import QtCore, QtWidgets

class TabPage(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.parent = parent                                                 # +
        self.button_id = 0                                                   # +

        group = QtWidgets.QGroupBox('Monty Python')
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(group)
        grid = QtWidgets.QGridLayout(group)
        testbutton = QtWidgets.QPushButton('Clear Text')
        grid.addWidget(testbutton, 2, 2)

        testbutton.clicked.connect(self.tab_match)
        self.parent.currentChanged.connect(self.qtabwidget_currentchanged)   # +

    def tab_match(self): 
        #Do something with button ID here
        print("\ndef tab_match: button_id-> {}".format(self.button_id))      # +

    @QtCore.pyqtSlot(int)
    def qtabwidget_currentchanged(self, index):                              # +
        self.button_id = index 


class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.tabs = QtWidgets.QTabWidget()
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.tabs)
        button = QtWidgets.QToolButton()
        button.setToolTip('Add New Tab')
        button.clicked.connect(self.addNewTab)
        button.setIcon(self.style().standardIcon(
            QtWidgets.QStyle.SP_DialogYesButton))
        self.tabs.setCornerWidget(button, QtCore.Qt.TopRightCorner)

        self.button_id = 0
        self.addNewTab()

    def addNewTab(self):
        text = 'Tab %d' % (self.tabs.count() + 1)
        self.tabs.addTab(TabPage(self.tabs), text)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 300, 200)
    window.show()
    sys.exit(app.exec_())

enter image description here

Upvotes: 2

Related Questions