1

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

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Zander
  • 11
  • 2
  • There i no mouse 'click' event in Qt, you can use `QEvent::MouseButtonPress` or `QEvent::MouseButtonRelease` depending on your needs. Just check the equality of `QEvent::type` value to one of these enum values in `eventFilter`. – Alexey Jul 11 '22 at 15:46
  • I've tried to add to this function: `if event.type() == QtCore.QEvent.Type.MouseButtonPress: print('hello')` But I doesn't work :( – Zander Jul 11 '22 at 17:27
  • Did you call `self.btn.installEventFilter(self)`? – Alexey Jul 11 '22 at 20:43
  • Should I setup this for each button ? Can I do it in a loop? Or I can write it for the GridLayout and this automatically will add to the Childrens (buttons)? – Zander Jul 12 '22 at 05:18
  • You should set the event filter to each button, if you need to handle it's 'click' event in a similar way. You can set it in a loop or sequentially. Than the `source` argument in `eventFilter` function (according to your sample code) would be a pointer to the button that is clicked. Refer to https://doc.qt.io/qt-6/qobject.html#installEventFilter – Alexey Jul 12 '22 at 06:18
  • But you can achieve the same behavior if you connect all buttons 'clicked' signal to the same slot function – Alexey Jul 12 '22 at 06:23
  • I think I met [a similar question](https://stackoverflow.com/q/72335222/9758790) before. I use `eval` to create a series of similar slots. I think perhaps you can also utilize `eval` to `connect` those hundreds of signal and slots? However, it seems that `eval` is not recommended by some programmers, but I can not come up with other workaround. – hellohawaii Jul 12 '22 at 06:25
  • I have function "add_functions()" where I collect all actions with buttons: I've tried to add here a loop where I connect each button click with function: `for button in self.gridLayoutWidget.findChildren(QtWidgets.QAbstractButton): button.clicked.connect(lambda: self.tmp_func(self.button.text())) ` but it works only 1 time when I run my app. I need to check every time when button status is chanched – Zander Jul 12 '22 at 08:44
  • @Zander `button.clicked.connect(lambda *args, text=button.text(): self.tmp_func(text))`. – ekhumoro Jul 12 '22 at 09:08
  • Yap, thx. I solved it. All I just need is to add click event in the `__init__` function and use `sender` in the second one... – Zander Jul 12 '22 at 09:35

0 Answers0