Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQT button clicked name

I have dynamically created buttons(QtoolButton) in gridLayout in pyQT. How can I get the name of the button clicked in the layout?

I can't know the name before hand. Is there is any trigger to accomplish the task?

Thanks in advance.

like image 679
Program Questions Avatar asked Oct 24 '12 13:10

Program Questions


People also ask

How to get name of button in Pyqt5?

You can call self. sender() in a function connected to your button event to get the object that triggered the event. From there you can call the object's objectName() method to get the name. Here's a quick example - the widget has 10 buttons and clicking on a button will update the label's text to show the button name.

What is the correct syntax to connect push button Name button to a function named action?

When the button is pressed it should perform some action, in order to add action to a button we will use clicked. connect method. Argument : It takes function as a argument. Action performed: When button will be clicked it will call the passes function.


2 Answers

You can call self.sender() in a function connected to your button event to get the object that triggered the event. From there you can call the object's objectName() method to get the name.

Here's a quick example - the widget has 10 buttons and clicking on a button will update the label's text to show the button name.

import sys
from PyQt4.QtGui import QApplication, QWidget, QToolButton, QLabel, QVBoxLayout, QHBoxLayout

class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        self.button_layout = QHBoxLayout()
        self.widget_layout = QVBoxLayout()

        for button_number in xrange(1, 11):
            button = QToolButton()
            button.setText(str(button_number))
            button.setObjectName('Button%d' % button_number)
            button.released.connect(self.button_released)
            self.button_layout.addWidget(button)

        self.status_label = QLabel('No button clicked')

        self.widget_layout.addItem(self.button_layout)
        self.widget_layout.addWidget(self.status_label)
        self.setLayout(self.widget_layout)

    def button_released(self):
        sending_button = self.sender()
        self.status_label.setText('%s Clicked!' % str(sending_button.objectName()))




if __name__ == '__main__':
  app = QApplication(sys.argv)

  widget = Widget()
  widget.show()

  sys.exit(app.exec_())
like image 198
Gary Hughes Avatar answered Sep 21 '22 15:09

Gary Hughes


I think you have to implement your iconlabel class derived from QToolButton:
Like This:

class IconLabel : public QToolButton
{
    Q_OBJECT
public:
    explicit IconLabel(QWidget *parent = 0);
    bool event (QEvent* e );
    QString name;
signals:
    void clicked_signal(QString);

};



bool IconLabel::event (QEvent* e ) {
   if ( e->type() == QEvent::Paint) {
      return QToolButton::event(e);


   }
   if(e->type() == QEvent::MouseButtonPress)
   {

       emit clicked_signal(name);
       return true;
   }

   return true;
}



connect(iconlabel, SIGNAL(clicked_signal(QString)), this, SLOT(getClickedButtonName(QString)));
like image 34
Al2O3 Avatar answered Sep 18 '22 15:09

Al2O3