Skip to main content

Hello,

 

Could someone tell me how, from a PythonCaller calling a dropdown list via the PyQt5 module, to use the selected string (in the example below, the "select" variable) in the list to fill in a feature attribute and then leave the dialog box once the choice is made?

# coding: utf-8
from PyQt5.QtWidgets import QApplication, QWidget , QComboBox
import sys    
 
def actionSelect():
    select = qcombo.currentText()
    print("Selected street : " + select)    
app = QApplication(sys.argv)
root = QWidget()
root.setWindowTitle("Street Selected")
root.setGeometry(100 , 100 , 500 , 300)
 
# création de la liste QCombobox
qcombo = QComboBox(root)
qcombo.setGeometry(100 , 10 , 150 , 30)
qcombo.activated.connect(actionSelect)
# création de la liste des items
L = "Street A, City A" , "Street B, City B" , "Street C, City C"]
# ajout des items à la liste QCombobox
qcombo.addItems(,"Street A, City A" , "Street B, City B" , "Street C, City C"])
root.show()
sys.exit(app.exec_())

For example, in the example below:

image.pngGive this result:

image 

Thanks a lot in advance for your help 🙏 

 

Antonin

I've got very little experience with Qt, but you could try to make the dialog box modal to prevent the "asynchronous" behavior. You could then retrieve the value of the combobox using either a class instance member, or a global variable.


Can you propose me an example in my original code ?

I'm a beginner in Python and I don't know which code and where I need to define the attribute.

I've try to put

feature.setAttribute("Street",select)
self.pyoutput(feature)

in "def actionSelect():" but it doesn't work 😓 


Try something like this to let the user select from a list of predefined items:

import fme
import fmeobjects
from PyQt5.QtWidgets import (QApplication, QWidget, QInputDialog)
 
class StreetNameDialog(QWidget):
    
    def __init__(self, items):
        super().__init__()
        self.text = ''
        self.showDialog(items)
 
    def showDialog(self, items):
        text, ok = QInputDialog.getItem(self, 'Street', 'Street name:', items, editable=False)
        if ok:
            self.text = str(text)
 
class SelectStreet(object):
    def __init__(self):
        pass
 
    def input(self, feature):
        app = QApplication(e])
        dlg = StreetNameDialog("Street A, City A" , "Street B, City B" , "Street C, City C"])
        feature.setAttribute('Street', dlg.text)
        
        self.pyoutput(feature)
                               
    def close(self):
        pass

Note that the PythonCaller must be configured to call the "SelectStreet" class:

imageExample:

imageThe resulting attribute "Street" will contain an empty string if the user selects Cancel, otherwise the text of the selected list item.


Try something like this to let the user select from a list of predefined items:

import fme
import fmeobjects
from PyQt5.QtWidgets import (QApplication, QWidget, QInputDialog)
 
class StreetNameDialog(QWidget):
    
    def __init__(self, items):
        super().__init__()
        self.text = ''
        self.showDialog(items)
 
    def showDialog(self, items):
        text, ok = QInputDialog.getItem(self, 'Street', 'Street name:', items, editable=False)
        if ok:
            self.text = str(text)
 
class SelectStreet(object):
    def __init__(self):
        pass
 
    def input(self, feature):
        app = QApplication(e])
        dlg = StreetNameDialog("Street A, City A" , "Street B, City B" , "Street C, City C"])
        feature.setAttribute('Street', dlg.text)
        
        self.pyoutput(feature)
                               
    def close(self):
        pass

Note that the PythonCaller must be configured to call the "SelectStreet" class:

imageExample:

imageThe resulting attribute "Street" will contain an empty string if the user selects Cancel, otherwise the text of the selected list item.

huge thank you David, this is exactly what I wanted 💃 🏆 🙏  and it work perfectly!!!


A huge thank you David, this is exactly what I wanted 💃 🏆 🙏 and it work perfectly!!!

I take advantage of the fact that you are a king for one last question:

How do I get all the items in an FME list into my dropdown (I know I should go through "feature.getAttribute('array{}.display_name')" but not sure how to operate it 😉?


huge thank you David, this is exactly what I wanted 💃 🏆 🙏  and it work perfectly!!!

Try replacing line 23 with this:

list_items = feature.getAttribute('array{}.display_name') or r]
dlg = StreetNameDialog(list_items)

 

 


A huge thank you David, this is exactly what I wanted 💃 🏆 🙏 and it work perfectly!!!

Quick and Efficient, I love that 😍 👏

Great Thanks.


Reply