PyQt: Can I make a toolbar button with an icon and text?
Asked Answered
P

1

5

I am learning PyQt by playing with examples. In this case, I'm playing with the webbrowser example that is located at \Python26\Lib\site-packages\PyQt4\examples\activeqt\webbrowser .

The demo does something really odd if you add one line to set the icon text property of a QAction.

Here's a code sample of the change I tried:

self.actionBack = QtGui.QAction(MainWindow)
self.actionBack.setIcon(QtGui.QIcon(":/icons/image1.xpm"))
self.actionBack.setObjectName("actionBack")
# added this line:
self.actionBack.setIconText("Back")

One time I tried it, and the entire toolbar went blank. I can't reproduce that, now I have no effect from that one line change.

What I'm trying to figure out is what to do to the QAction so that the toolbar has text on the button beside the image, or can it be done at all like this? Is there some other way to make the toolbar have some text plus an icon?

Perdomo answered 20/10, 2011 at 21:18 Comment(0)
C
12

Try setting the button style on the toolbar that the actions are being added to:

self.tbNavigate.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
...
self.actionBack.setIconText("Back")
Cinelli answered 20/10, 2011 at 21:40 Comment(1)
That works perfectly. In one day, I have gotten enough hackery in to get the flavor of PyQt, and I love it! :-) It's great.Perdomo

© 2022 - 2024 — McMap. All rights reserved.