PDA

View Full Version : Newbie Trying to Create a Menu ToolTip, but setToolTip() Doesn't Seem to Work



sinefine
27th May 2015, 04:39
Hello. I was trying to set the tooltip for a menu item, but setToolTip() doesn't seem to work. According to the documentation:

"The simplest and most common way to set a widget's tool tip is by calling its QWidget::setToolTip() function."

What am I doing wrong here?

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *

app=QApplication(sys.argv)
w=QMainWindow()
w.resize(600,400)
w.setWindowTitle("Hello World")

mainMenu=w.menuBar()
fileMenu=mainMenu.addMenu('&File')

exitButton=QAction(QIcon('exit24.png'),'Exit',w)
exitButton.setShortcut('Ctrl+Q')
exitButton.setToolTip('Exit')
exitButton.triggered.connect(w.close)
fileMenu.addAction(exitButton)

fileMenu=mainMenu.addMenu('&Edit')

w.show()
sys.exit(app.exec_())

wysota
27th May 2015, 07:48
How does "doesn't seem to work" manifest itself?

chris.kzn
27th May 2015, 10:04
Hello sinefine,

What I think your problem might be is that although your file "exit24.png" might be located in the folder that the source code is in, when you run the program in "Debug" or "Release" mode, your file does not exist in the folder that it is being compiled to. You need to use a Qt Resource file, add this file into the Qt Resource file and then make reference to the path of the Qt Resource file.

QAction(QIcon(':/pathwheremyresourcefilestoresthisfile/exit24.png'),'Exit',w)

sinefine
28th May 2015, 00:28
How does "doesn't seem to work" manifest itself?

It manifests itself because when I hover mouse over the menu item, nothing shows up.



Hello sinefine,

What I think your problem might be is that although your file "exit24.png" might be located in the folder that the source code is in, when you run the program in "Debug" or "Release" mode, your file does not exist in the folder that it is being compiled to. You need to use a Qt Resource file, add this file into the Qt Resource file and then make reference to the path of the Qt Resource file.

QAction(QIcon(':/pathwheremyresourcefilestoresthisfile/exit24.png'),'Exit',w)

I will try that. Thank you, sir!

wysota
28th May 2015, 06:13
It manifests itself because when I hover mouse over the menu item, nothing shows up.
Do they show up when you hover your mouse over menu entries in other programs? I don't think tooltips are used in menu entries anywhere. You would probably have to implement such behavior yourself.

sinefine
28th May 2015, 23:15
Do they show up when you hover your mouse over menu entries in other programs? I don't think tooltips are used in menu entries anywhere. You would probably have to implement such behavior yourself.

Okay. Thank you!