2

I am making a custom maptool for QGIS I am overriding canvasReleaseEvent. when the user clicks on part of map canvas I call another function which identifies and creates a list of identified feature (there is some logic to create the list). Then this list is used to create the selection interface like the one below:

enter image description here

So far everything works well. The problem is,my program doesn't wait for the result from Selection Dialog box :

    def canvasReleaseEvent(self, event):
        parrent=self.select_parent_feature('structurepoint',event)
        #here the program should wait till the result from parrent is ready
        self.main_dlg.ParrentID_txt.setText(parrent)

Here is the function which creates the selection list, shows dlg and lets user to select

    def select_parent_feature(self,layer_name,e):
    '''This Functtion will show a list of all features in clicked point and returns user selection
    :param Layer_name: Qgis Layer name should be Already open.
    :type Layer_name: string

    :param e: is mouse event
    :type e: event
    '''

    #Start of creating list and identifieng the features
    global selection_list_header #this is the list of headers of table 
    selection=""
    selection_done_flag=False
    identified_parent_list=[]
    for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
        if lyr.name() == layer_name:LayerID = lyr
    features = QgsMapToolIdentify(self.canvas).identify(e.x(), e.y(), [LayerID], QgsMapToolIdentify.TopDownAll)

    if len(features) > 0:#to make sure there is feature in clicked area
        if len(features)==1:#if there is only one item there is no need for the selection list
            feature = features[0].mFeature# the list only has one element
            selection=feature['id']
            selection_done_flag=True

        else: # show list to the user to select from
            for obj in features:
                feature = obj.mFeature
                info=[feature['id'],feature['size']]
                identified_parent_list.append(info)
                info=""

            table=self.selectionList_dlg.Selectionlist_tbl
            table.setColumnCount(len(selection_list_header))
            for i in range(len(identified_parent_list)):
                table.insertRow(i)


            column_index=0#the following line sets the headers
            for item in selection_list_header:
                table.setHorizontalHeaderItem(column_index, QTableWidgetItem(item))
                column_index+=1

            row_number=0#the following loop populates the value in table and lock the cells
            for item in identified_parent_list:
                for column_index in range(len(selection_list_header)):
                    cell_value=QTableWidgetItem(item[column_index])
                    cell_value.setFlags(QtCore.Qt.ItemIsEnabled)#lock the cell
                    table.setItem(row_number,column_index,cell_value)
                row_number+=1
            #end of logic for createn selection list DLG
            #this it where my program doesnt work
            self.selectionList_dlg.show()
            result = self.main_dlg.exec_()
           # we need to wait for the result to be ready 
            if result:
                current_row_number=table.currentRow()
                if current_row_number<>-1:
                    selection=table.item(current_row_number,0).text()#we asssumed the id is located on column 0
                    selection_done_flag=True

    #I tried to  use wait until and it explodes the program
     #while not selection_done_flag:#Wait till the user select from the list
         #time.sleep(0.0001)

    return selection      

As the program shows, I defined a flag to check and wait until the flag is true but that explodes the program.

the problem is the last part of the program where we show the selection list and the user has to select before the program can continue

3

It's difficult to tell exactly without the full code, but I noticed you're calling

self.selectionList_dlg.show()

I suspect this should be

self.selectionList_dlg.exec_()

As exec_ will wait till the dialog is closed, but show will not.

  • there were several problems with code but the main problem was the difference between .show() and exec_() – Ehsan Aliverdi Sep 26 '18 at 2:10

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.