1

i will want to create a simple QGIS plugin where the user put a number (line_edit) and if that number is something the user take back three new codes(lineEdit_2,lineEdit_3,lineEdit_4) and if user press ok(buttonBox) then plugin do something else in the some GUI.

for that i create a new additional pushButton in Qdesigner and i create additional def function(run1) for execute code for pushButton.

first i add new pushbutton in __init__ :

def __init__(self, iface):
    self.dlg = pluginDialog()
    self.dlg.pushButton.clicked.connect(self.run1)

my gui :

qdesigner

plugin work without error but dont show me the codes (lineEdit_2,lineEdit_3,lineEdit_4) in the plugin window if i press pushbutton,any idea ? my python code :

def run1(self):
    distance = str(self.dlg.lineEdit.text())
    if distance==0:
        area1='some'
        area2='hello'
        area3='world'
    self.dlg.lineEdit_2.setText(str(area1))
    self.dlg.lineEdit_3.setText(str(area2))
    self.dlg.lineEdit_4.setText(str(area3))



def run(self):
    """Run method that performs all the real work"""
    # show the dialog
    self.dlg.show()
    # Run the dialog event loop
    result = self.dlg.exec_()
    # See if OK was pressed
    if result:
        # Do something else
        pass

and i try to replace run1 with :

def run1(self):
    self.dlg.show()
    result = self.dlg.exec_()
    if result:
        distance = str(self.dlg.lineEdit.text())
        if distance==0:
            area1='some'
            area2='hello'
            area3='world'
        self.dlg.lineEdit_2.setText(str(area1))
        self.dlg.lineEdit_3.setText(str(area2))
        self.dlg.lineEdit_4.setText(str(area3))
        pass

but no change.

  • 2
    There are some errors in the first version. You get distance from a line edit widget as a string and later you compare it with a number (0). So the if block will never be executed. Also the variables area1 and so on do not exist. – Andreas Müller Mar 15 '17 at 7:23
1

Along with the other comments and @Joseph answer you will need to put the button event statements in the initGui(self) function so he click event may be listened to properly:

def initGui(self):
  self.dlg.pushButton.clicked.connect(self.run1)
0

There's a few lines in your run1() function which you do not need. You also need to set your if statement to match a string. Try something like the following:

def run1(self):
    distance = str(self.dlg.lineEdit.text())
    if distance == '0':
        area1 = 'some'
        area2 = 'hello'
        area3 = 'world'
        self.dlg.lineEdit_2.setText(str(area1))
        self.dlg.lineEdit_3.setText(str(area2))
        self.dlg.lineEdit_4.setText(str(area3))
    else:
        pass

Example:

Result

  • 1
    you are right now work fine thx @Joseph the self.dlg.pushButton.clicked.connect(self.run1) must be below in` initGui` else not working – Mar Mar 15 '17 at 15:58
  • @Mar - Most welcome, glad it worked! Yes, I just noticed artwork21's answer. I suggest you upvote and accept his answer by clicking the green-faded tick next to his answer to show that your problem has been solved :) – Joseph Mar 16 '17 at 11:38
  • one more question @Joseph,now i want to clear line edit after the running i put all clears edits in the def ... run : but if the user use only the pushbutton and then push cancel the next opening plugin have not clear how to fix that ?because if i use clears on def run1 then or dont show me again nothing or i have errors....Good job joseph you are a very significant in stack overflow – Mar Mar 16 '17 at 14:40
  • @Mar - So if the user clicks either OK or Cancel, you want the line edits to be cleared? – Joseph Mar 16 '17 at 14:52
  • yes i want every to start again plugin all line edits have clear – Mar Mar 16 '17 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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