Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Popup and focus Issue



  • hi
    Following is my code
    in clsFarmers, I popup a window of type clsPopUpVillageList
    with QtCore.Qt.ApplicationModal on focus of lineEdit control (txtVillage)

    1. I am not getting modal window
    2. When I close the pop with escape Key The focus doesnt come to txtVillage, instead it goes some where else.

    kindly help

    -----------------------------------------------------------------------------
    class clsPopUpVillageList(QtWidgets.QWidget, Ui_Widget1):
        def __init__(self,parent=None):
                super().__init__(parent)          
                self.setupUi(self)               
                self.installEventFilter(self)            
                self.move(300, 100)
                self.txtSearch.setFocus()
                self.windowFlags = QtCore.Qt.ApplicationModal
        def eventFilter(self, obj, event):   
            if event.type() ==QtCore.QEvent.KeyPress:
                print('clsPopUpVillageList')
                if (event.key() == QtCore.Qt.Key_Escape):                    
                    self.close()                    
                    return False
            print('return--pop')                
            return super(clsPopUpVillageList, self).eventFilter(obj, event)
    
    -------------------------------------------------------------------------
    -------------------------------------------------------------------------
    class clsFarmers(QtWidgets.QWidget, Ui_Widget):
        def __init__(self,parent=None):
            super().__init__(parent)       
            self.setupUi(self)      
            self.txtName.installEventFilter(self)
            self.txtAdd1.installEventFilter(self)
            self.txtAdd2.installEventFilter(self)
            self.txtVillage.installEventFilter(self)
            self.txtMobile.installEventFilter(self)
    
            
        def eventFilter(self, obj, event):  
            if (event.type() ==QtCore.QEvent.FocusIn):
                print('focus')
                if (obj == self.txtVillage):                       
                    o = clsPopUpVillageList(self)
                    o.setWindowModality(QtCore.Qt.ApplicationModal)
                    o.show()
                    print('focus-----------')
                    return False            
            elif (event.type() == QtCore.QEvent.KeyPress) :
                print('key press:', (event.key(), event.text()))
                if ((event.key() == QtCore.Qt.Key_Enter) or (event.key() == QtCore.Qt.Key_Down)):
                    if ((obj == self.txtName) or (obj == self.txtAdd1) or (obj == self.txtAdd2) or (obj == self.txtVillage) or (obj == self.txtMobile)):
                        print("txtVillage key")
                        k = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_Tab, QtCore.Qt.NoModifier)                     
                        QtCore.QCoreApplication.sendEvent(obj, k)              
                        return False
                elif ((event.key() == QtCore.Qt.Key_Enter) or (event.key() == QtCore.Qt.Key_Up)):
                    if ((obj == self.txtName) or (obj == self.txtAdd1) or (obj == self.txtAdd2) or (obj == self.txtVillage) or (obj == self.txtMobile)):
                        print("txtVillage key")
                        k = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_Tab, QtCore.Qt.ShiftModifier)                     
                        QtCore.QCoreApplication.sendEvent(obj, k)              
                        return False
            print('return--main')
            return super(clsFarmers, self).eventFilter(obj, event)
    
    

  • Lifetime Qt Champion

    Hi,

    Shouldn't you rather use a QDialog for your popup ?

    In any case, if you want to set a window flag, use the setWindowFlag method.


Log in to reply