Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 861
Warren P Avatar asked Oct 20 '11 21:10

Warren P


People also ask

How to create menu bar in PyQt5?

To create a menu for a PyQt5 program we need to use a QMainWindow. This type of menu is visible in many applications and shows right below the window bar. It usually has a file and edit sub menu. The top menu can be created with the method menuBar().

How menus and its options are created in PyQt5?

To create a menu, we create a menubar we call . menuBar() on the QMainWindow. We add a menu on our menu bar by calling . addMenu() , passing in the name of the menu.

What is a widget in PyQt?

Widgets are the basic building blocks for graphical user interface (GUI) applications built with Qt. Each GUI component (e.g. buttons, labels, text editors) is a widget that is placed somewhere within a user interface window, or is displayed as an independent window.


1 Answers

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")
like image 77
ekhumoro Avatar answered Oct 13 '22 09:10

ekhumoro