2

I want to show tooltip while not focusing.

I made the code by referring to this PyQt Window Focus

But, it works after click window just one. Works fine, but window always blink at taskbar.

And I think this method is inefficient.

I think it's as if os are not resting while waiting for task to come, but checking every moment for task to come.

This is a simple window window, so it won't use up the cpu much, but I want to code it more efficiently.

Is there any way to improve this?

Or this method right because focusoutEvent excuted only one? ( Cpu resource 0% )

If right, how can I remove blink at taskbar?

I check reference focusPolicy-prop

import sys, os
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.initUI()

    def initUI(self):
        vbox = QVBoxLayout()
        vbox.addStretch(2)
        btn = QPushButton("Test")
        btn.setToolTip("This tooltip")
        vbox.addWidget(btn)
        vbox.addStretch(1)

        self.setLayout(vbox)
        self.setGeometry(300, 300, 300, 200)
        self.show()

    def focusOutEvent(self, event):
        self.setFocus(True)
        self.activateWindow()
        self.raise_()
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())
4rigener
  • 346
  • 3
  • 18

1 Answers1

2

You are having an XY problem: trying to find a solution (usually unorthodox and overly complicated) for a problem that is originated elsewhere.

What you want to do is to show tooltips even if the window is not focused, not to restore the focus of the window; to achieve this you must not reactivate the window when it loses focus (which not only is WRONG, but is both a wrong way and reason for doing so).

You just have to set the WA_AlwaysShowToolTips widget attribute on the top level window (and remove the unnecessary focusOutEvent override, obviously).

class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.initUI()
        self.setAttribute(QtCore.Qt.WA_AlwaysShowToolTips, True)

Note that the attribute must be set on a widget that is a top level window, so, unless you're using a QMainWindow or you are absolutely sure that the QWidget will always be a window, it's usually better to do this instead:


    self.window().setAttribute(QtCore.Qt.WA_AlwaysShowToolTips, True)


Besides that, the blinking is normal on windows, and has nothing to do with CPU usage:

activateWindow():
[...] On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the taskbar entry to indicate that the window has changed in some way. This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • How did you find `WA_AlwaysShowToolTips`? I search with **tooltip, focus, window** at google but nothing comes out... If there's any method to find func at reference, I want to learn. – 4rigener Mar 06 '20 at 00:12
  • Read the *widget attribute* link in the answer, I don't put them there just for fun... ;) – musicamante Mar 06 '20 at 00:18
  • I searched like [this](https://doc.qt.io/qt-5/search-results.html?q=tooltip%20while%20not%20focusing) and it didn't come out for 5 hours, so I was wondering if there's a way to look up reference. Anyway thanks for your help. – 4rigener Mar 06 '20 at 00:23
  • I never use the qt site search, especially for "semantic" searches like yours (although "smart", current search algorithms are still based on literal words, not their meaning, especially when limited to a single website). The only solution is to patiently study the documentation and become more accustomed with the classes and their functions (the mandatory classes are without any doubt QObject and QWidget, but reading all QtCore.Qt enum/flags is also a good thing). It takes time, but Qt is a *big*, complex and comprehensive framework, you can't expect to use it just with "random" searches. – musicamante Mar 06 '20 at 00:51
  • 2
    Consider this: it took you 5 hours to find *nothing*. In the same amount of time, you could have read (at least) both QWidget and QtCore.Qt pages completely, you'd have probably found what you were looking for in half of that time, while still learning a lot of other things you didn't know about... :) – musicamante Mar 06 '20 at 00:53
  • @4rigener see https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum – S. Nick Mar 06 '20 at 12:26