Displaying a standard icon and text in QLabel
Asked Answered
E

3

14

I want to display a standard warning icon along with some description text in QLabel in pyqt. Qlabel doesn't have setIcon function. So how could I do that?

Any help would be appreciated.

Expostulatory answered 10/5, 2012 at 12:19 Comment(0)
D
24

QLabel doesn't have a setIcon method, but it has setPixmap. But if you use that to set a QPixmap it overrides your text. but there are a few possibilities to achieve what you want:

  • use the html-capabilities of the QLabel to display text+image
  • use two labels, one with the text and one with the image
  • paint the component yourself
Delfeena answered 13/5, 2012 at 20:6 Comment(3)
Would you mind adding an example showing how to "use the html-capabilities of the QLabel to display text+image"?Annmarieannnora
That means just add an <img> tag pointing to the image (QLabel("<html><img src='/path/to/my_image.png'></html>")) or image resource defined in your .qrc (QLabel("<html><img src=':/my_image.png'></html>"))Delfeena
As displaying both a pixmap and text in the same QLabel is trivially feasible (if cumbersome) via the <img> hack above, it's disappointing (if unsurprising) that QLabel supports no such functionality out-of-the-box. Why were setPixmap() and setText() forced to be mutually exclusive, when the two could have been made to simplistically cooperate in the obvious way (e.g., by prepending or appending text with an <img>-wrapped pixmap in rich text mode)?Polonaise
J
7

I know I'm a bit late to the party, but here's my solution to this problem:

import qtawesome as qta
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class IconLabel(QWidget):

    IconSize = QSize(16, 16)
    HorizontalSpacing = 2

    def __init__(self, qta_id, text, final_stretch=True):
        super(QWidget, self).__init__()

        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(layout)

        icon = QLabel()
        icon.setPixmap(qta.icon(qta_id).pixmap(self.IconSize))

        layout.addWidget(icon)
        layout.addSpacing(self.HorizontalSpacing)
        layout.addWidget(QLabel(text))

        if final_stretch:
            layout.addStretch()

I am using QtAwesome to get the desired icon.

Now you can easily insert the icon label into your own PyQt5 layout. Example.

mylayout.addWidget(IconLabel("fa.scissors", "Slicer Limit:"))

What it looks like:

enter image description here

Note that I included an option final_stretch. In a vertical layout, you'll want to keep final_stretch = True for proper left-alignment (see image avove). In a horizontal layout (i.e., when placing multiple IconLabels next to each other in the same row), however, you'll probably want to set final_stretch = False in order to save space.

Jesus answered 9/10, 2020 at 11:42 Comment(0)
F
-2

Try this, I hope this will work fine`install qtawesome using the following command:

pip install qtawesome

you can use any kind of icon from qtawesome.

`

import qtawesome as qta

icon = qta.icon("fa.lock",color='blue')
self.ssl_label.setPixmap(icon.pixmap(QSize(16,16)))
Fulmination answered 19/3, 2022 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.