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

Using mousePressEvent in PyQt5



  • Hi,

    I‘m trying to a use virtual keyboard for my gui in PyQt5. I already found one (https://github.com/yurisnm/pyqt5/tree/master/keyboard_full). My goal is to implement my Gui.py, build with QtCreator. I already imported the file and changed the MainWindow class for the new gui.

    First the working example from GitHub, that calls the Keyboard if you click into TextEdit:

    class MainWindow(QWidget):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.layout = QVBoxLayout()
            self.setLayout(self.layout)
            self.text_edit = TextEdit(self)
            self.layout.addWidget(self.text_edit)
            self.kb = WKeyboard()
            self.kb.set_receiver(self.text_edit)
    
        def closeEvent(self, QCloseEvent):
            self.kb.close()
            super(MainWindow, self).closeEvent(QCloseEvent)
    
    class TextEdit(QTextEdit):
    
        def __init__(self, parent):
            super(TextEdit, self).__init__()
            self.parent = parent
    
        def mousePressEvent(self, QMouseEvent):
            self.parent.kb.show()
            super(TextEdit, self).mousePressEvent(QMouseEvent)
    
    

    And here's how I edited it so far:

    
    class MainWindow(QMainWindow, gui.Ui_Gui):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)
            self.edit = Text(self)
            self.kb = Tastatur()
            self.kb.set_receiver(self.edit)
    
        def closeEvent(self, QCloseEvent):
            self.kb.close()
            super(MainWindow, self).closeEvent(QCloseEvent)
    
    class Text(QLineEdit):
        def __init__(self, parent):
            super(Text, self).__init__()
            self.parent = parent
    
        def mousePressEvent(self, QMouseEvent):
            self.parent.kb.show()
            super(Text, self).mousePressEvent(QMouseEvent)
    
    

    I named the LineEdit in QtCreator "edit".

    I included Gui.py and replaced self.text_edit by self.edit.

    The gui shows up, but if I click on the LineEdit nothing happens.

    Are there further changes to make?

    Thanks!


  • Banned

    Okay this snippet of code can only get you snippet answer which could be completely wrong you need to supply an MRE (Minimal Reproducible Example) -- which means take what you are trying to do and drill it down to its bare essentials anything that is not important to the issue you put in dummy placeholders or remove completely. Post this MRE so that all we have to do is copy/paste/run and we get the same error as you are getting -- then we can more easily do you the favor you are requesting. Basically help us help you with your issue

    As a side note it appears that this Class Tastatur( ) which you do not include plays some major role in what it is you are attempting to do

    However I have addressed a few issues - which I doubt are the problem but can cause problems within your code snippet if you have any questions just ask be more than happy to explain them in detail

    class Text(QLineEdit):
        def __init__(self, parent):
          # First you should not use super( ) in Python as it introduces 4 known major 
          # issues that must be handled properly. Further there were still actual bugs 
          # within the usage of super( ) when used in Python. Yes while super( ) works 
          # fine within C++ it does not work as seamlessly within Python due to the 
          # major differences between these 2 languages. Next the reason it was created 
          # was to handle a rather rare issue and unless you are doing some complicated 
          # inheritance you will most likely never run into this extremely rare issue
          # However the 4 major issues that get included by using super( ) you are much 
          # more likely to occur than that rare issue its meant for to solve. Of course
          # using the basic explicit method, as follows, does not cause these issues and 
          # is as just as simple as using `super( )` further you do not actually gain 
          # anything useful by using `super( )` in Python that could not be done in a 
          # much safer manner.
          # super(Text, self).__init__()
            QLineEdit.__init__(self)
            self.parent = parent
    
        def mousePressEvent(self, QMouseEvent):
            self.parent.kb.show()
          # See comment on why not to use super( ) above
          # super(Text, self).mousePressEvent(QMouseEvent)
            QLineEdit.mousePressEvent(QMouseEvent)
    
    class MainWindow(QMainWindow, gui.Ui_Gui):
        def __init__(self):
          # See comment on why not to use super( ) above
          # super(MainWindow, self).__init__()
            QMainWindow.__init__(self)
            self.setupUi(self)
            self.edit = Text(self)
            self.kb = Tastatur()
            self.kb.set_receiver(self.edit)
    
        def closeEvent(self, QCloseEvent):
            self.kb.close()
          # See comment on why not to use super( ) above
          # super(MainWindow, self).closeEvent(QCloseEvent)
          #
          # Further if you do not intercept the Close event which
          # you are not doing then it should continue on an do
          # what it would normally do so no need to explicitly
          # call it again
    

Log in to reply