Search code examples
pythonpyqticonspyqt4qtoolbar

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


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?


Solution

  • 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")