pyqt Event when button clicked

1k Views Asked by At

I am creating a Poker Preflop Trainer as a pet project using QT Designer.

I have a set of buttons in my main window. I want to change label text and Textedit when I click on button. It will be the same action with some difference depending on button text. But I don't want to write self.btn.clicked.connect(...) for all buttons.

I just need to use event. But I don't now what event I need and how it realize.

I was trying to use EventFilter. Here is my examples, not for this task.

class Ui_MainWindow(QtWidgets.QMainWindow):

def __init__(self):
    super().__init__()
    uic.loadUi("pokerRange.ui", self)

    self.add_functions()
    self.dict_range = {}

    self.range_listWidget.installEventFilter(self)
    self.range_listWidget.currentRowChanged.connect(self.display_range_by_item)

    self.tEdit_range.installEventFilter(self)


def eventFilter(self, source, event):
    """
    All keyboard actions
    """
    # right click mouse action for listWidget
    if (event.type() == QtCore.QEvent.ContextMenu and source is self.range_listWidget):
        menu = QtWidgets.QMenu()
        menu.addAction('Open Window')
        if menu.exec_(event.globalPos()):
            item = source.itemAt(event.pos())
            print(item.text())
        return True
    # keyboard enter action for text edit
    if event.type() == QtCore.QEvent.KeyPress and source is self.tEdit_range:
        if event.key() == QtCore.Qt.Key_Return and self.tEdit_range.hasFocus():
            print('Enter pressed')

    return super(Ui_MainWindow, self).eventFilter(source, event) 

enter image description here

0

There are 0 best solutions below