3

There is a plugin I wrote for QGIS and I added some buttons there. From main launch Python script I need to check is specific button is clicked and according to that do some stuff.

I saw a default condition in main script:

def run(self):
    if self.first_start == True:
        self.first_start = False
        self.dlg = frame_builderDialog()
    self.dlg.show()
    result = self.dlg.exec_()
    if result:
        pass
        'or do something'

In this case I can catch event if I had an "OK\Cancel" button. But I have another simple buttons. I tried to catch them using something like

result_btn = self.dlg.pushButton.clicked

where pushButton is one of the buttons taken from script with dialog, but nothing were returned.

What should be done here?

3 Answers 3

4

In python you have "events" and "events handler". when an event occurs they usually (not always...) generate a signal that can be caught by the event handler.

for example :

curLayer.selectionChanged.connect(self.test)

the event is selectionChanged and the event handler is the function handling the signal received (in that case the handler should be a function (written by you) called test (or any other naming ofc).

your snippet :

result_btn = self.dlg.pushButton.clicked

Is meaningless (imho ...)

So ... in your case u should try an

self.dlg.pushButton.clicked.connect(self.my_event_handler)

And write an my_event_handler function of course.

3

You could connect the pushButton to a function so that when it is clicked, it will do something:

result_btn = self.dlg.pushButton.clicked.connect(self.someFunction)
0

The event handler function looks something like this:

def handlepushbutton(self,flag):
    QgsMessageLog.logMessage("clicked on a button", "DEBUG")

And in the run() method you set the handler to call the handlepushbutton() method:

self.dlg.pushButton.clicked.connect(self.handlepushbutton)

pushButton is the name of the push button in QtDesigner. Subsequent buttons in the same dialogbox are named pushButton_2, pushButton_3, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.