Results 1 to 10 of 10

Thread: Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

  1. #1
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

    Is it possible to detect a mouse press or mouse click on a QLineEdit that has been set to either .NoFocus or .setReadOnly, and then call a function to do something? If so, how?

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

    Hi, you could try an event filter:
    https://doc.qt.io/qt-5/qobject.html#installEventFilter
    but I'm not sure if an object with NoFocus will receive any events.

    What do you want to do?

    Ginsengelf

  3. #3
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

    Sorry for the delay -- went out of town.

    I set several QLineEdits to .setReadOnly when I do not want the user to input any data. However, I wanted to remind the user why they cannot enter any data. I used a tooltip to accomplish what I want, but the best thing is to show a QMessageBox (or something like that) when the user "clicks" the LiineEdit to enter data (a tooltip does not show when clicking the line edit). I read about the event filters, but am such a newbie that I could not set it up to test. Could you help? Here is what I tried, but of course, it does not work:

    in my init:
    Qt Code:
    1. self.mousePressEvent = QtCore.QEvent.MouseButtonPress
    2. self.ui.BTE.installEventFilter(self.ui.BTE)
    3. self.ui.BTE.mousePressEvent(QMouseEvent=)
    To copy to clipboard, switch view to plain text mode 

    in a function:
    Qt Code:
    1. def eventFilter(self, source, event):
    2. if self.BTE.isReadOnly:
    3. if event.type() == QtCore.QEvent.MouseButtonPress and source is self.ui.BTE:
    4. message = 'Test'
    5. self.blueboxmessage(message)
    6. else:
    7. return
    To copy to clipboard, switch view to plain text mode 
    thanks for looking

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

    self.ui.BTE.mousePressEvent(QMouseEvent=)
    If you are trying to fire a mouse event to test your code, this is not the way to do it. You need to post an actual event to the Qt event loop so it can be processed and eventually sent to your event filter. See QCoreApplication::postEvent() or QCoreApplication::sendEvent() for the details. I do not know for sure whether sendEvent() will activate your event filter or not, so you would have to test that. If it doesn't work, then you'll have to use postEvent() instead.

    In C++, an eventFIlter() method must return a Boolean true or false to determine whether the event handling stops (true - the event filter "eats" the event) or continues (false - to be also handled by the object it is intended for). If you are not interested in the event at all, then you have to call the superclass eventFilter() method and return its true / false return value. If you don't follow these rules, then your line edit might fail to work at all.

    See the documentation for QObject::eventFilter() which shows how to correctly implement eventFilter() in a MainWindow class.

    If I were doing this, I would not use a dialog-based class to show the message. I would create a QToolTip instance as a member of your class, call QToolTip::showText() in the mouse pressed event, and call QToolTip::hideText() in both the mouse release and leave events for your line edit. You could also start a QTimer when you show the tooltip, and hide the tooltip when the timeout event fires.

    Doing it with a QDialog means the user has to click somewhere else to dismiss the dialog, and it removes the focus from the line edit in the process. I guarantee this will lead to more UI problems.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

    Trying to implement what you suggested in last paragraph:
    in init:
    Qt Code:
    1. self.ui.BTFE.installEventFilter(self)
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. def eventFilter(self, source, event):
    2. if event.type() == QtCore.QEvent.MouseButtonPress and source.isReadOnly:
    3. self.ui.BTFE.setToolTip('Cannot enter data here')
    4. QTimer.singleShot(5000, self.remove_tooltip)
    5. return super(UIMainWindow, self).eventFilter(source, event)
    6.  
    7. def remove_tooltip(self):
    8. self.ui.BTFE.setToolTip('')
    To copy to clipboard, switch view to plain text mode 

    I cannot figure out the pyqt code for "showing" and 'hiding" the tooltip -- I only know how to code .setTooltip, but that does not show the tip when the line edit is clicked -- only when hover. Plus, not sure if QTimer will do what I want. Ultimately, I'll have nine line edits that I need to test, all with the same tool tip. thanks

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

    I am not a Qt Python expert, but I think you need something like this:

    Qt Code:
    1. def eventFilter(self, source, event):
    2. if event.type() == QtCore.QEvent.MouseButtonPress and source.isReadOnly:
    3. QTimer.singleShot(5000, self.remove_tooltip)
    4. QtWidgets.QTooltiip.showText( source.pos(), 'Cannot enter data here')
    5. return super(UIMainWindow, self).eventFilter(source, event)
    6.  
    7. def remove_tooltip(self):
    8. QtWidgets.QTooltiip.hideText()
    To copy to clipboard, switch view to plain text mode 

    In the eventFilter() "source" is the QWidget instance that is the focus of the event, so it -is- "self.ui.BTFE" in this case, since that's where you have installed the filter. So in the event filter code, you can simply replace "self.ui.BTFE" with "source" and it will work for all of your line edits.

    I do not know for sure if QTooltip::showText() is a blocking method (ie, it suspends the rest of the code until it is hidden again), so I switched the order of starting the timer and showing the tip just to make sure the timer is running first.

    You also need to check to see if you can TAB into a read-only QLineEdit, and if so, you will probably wnat to display the tooltip for an enter event as well.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

    My PyCharm IDE is telling me it does not recognize QtWidgets, even though I have this import:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 

    I then added: (below import above):
    Qt Code:
    1. from PyQt5 import QtWidgets
    To copy to clipboard, switch view to plain text mode 

    That import got rid of the 'not recognized' issue, and no errors when run. I changed the event to MouseButtonRelease so the message would show for the 5 seconds. However, 1) the tooltip shows well away from the line edit. Is the source.pos() supposed to make the tooltip show next to the line edit that was clicked?
    thanks for your response
    Last edited by dennisvz; 28th August 2020 at 02:06.

  8. #8
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

    Hi, pos() returns the position in widget coordinates. I think globalPos() is what you need.

    Ginsengelf

  9. #9
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

    Qt Code:
    1. QtWidgets.QToolTip.showText(source.globalPos(), 'Cannot enter data here')
    To copy to clipboard, switch view to plain text mode 

    gives an AttributeError: 'QLineEdit' object has no attribute 'globalPos'

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Detecting a mouse press or click on QLineEdit set to Nofocus or .setReadOnly

    gives an AttributeError: 'QLineEdit' object has no attribute 'globalPos'
    So you fire up your browser, type "Qt globalpos" into the Google search box, and what do you think you get as the first hit? Certainly faster than asking questions here and waiting for someone to answer.

    And since you already imported QTooltip from QtWidgets, you can probably remove the QtWidgets part of QtWidgets.QTooltip.showText and just use QTooltip.showText. I said I wasn't a Python expert. Also get rid of the separate import of all of QtWidgets. It just bogs things down at runtime.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  11. The following user says thank you to d_stranz for this useful post:

    dennisvz (28th August 2020)

Similar Threads

  1. Mouse press
    By User 23 in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2016, 17:16
  2. Replies: 2
    Last Post: 16th July 2012, 13:40
  3. Replies: 1
    Last Post: 12th October 2010, 23:20
  4. Replies: 9
    Last Post: 26th October 2009, 01:13
  5. Detecting mouse clicks outside of widget
    By init2null in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2006, 20:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.