Results 1 to 10 of 10

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 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.

  2. #2
    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 01:06.

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    518
    Thanks
    13
    Thanked 77 Times in 75 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

  4. #4
    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'

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 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.

  6. 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, 16:16
  2. Replies: 2
    Last Post: 16th July 2012, 12:40
  3. Replies: 1
    Last Post: 12th October 2010, 22:20
  4. Replies: 9
    Last Post: 26th October 2009, 00:13
  5. Detecting mouse clicks outside of widget
    By init2null in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2006, 19: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
  •  
Qt is a trademark of The Qt Company.