Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Display icon&text on QLabel
Forum Updated to NodeBB v4.3 + New Features

Display icon&text on QLabel

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 5.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Pradeep P NP Offline
    Pradeep P NP Offline
    Pradeep P N
    wrote on last edited by
    #3

    Hi @opengpu

    One workaround would be using 2 QLable (1 for image & 1 for text) in Layout and use it.
    Need to be tested

    Pradeep Nimbalkar.
    Upvote the answer(s) that helped you to solve the issue...
    Keep code clean.

    jsulmJ O 2 Replies Last reply
    5
    • Pradeep P NP Pradeep P N

      Hi @opengpu

      One workaround would be using 2 QLable (1 for image & 1 for text) in Layout and use it.
      Need to be tested

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Pradeep-P-N @opengpu Agree, use a widget with layout and put these two labels in this widget.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #5

        There are at least 2 more options as well.

        • Subclassing and overriding paintEvent to draw an image and a text
        • setting a stylesheet with the target image as background, than text should still be applicable in the normal way

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        4
        • O Offline
          O Offline
          opengpu
          wrote on last edited by
          #6

          thank you.
          if i want to set a color-border to the container-Widget, should i use stylesheet?

          Pradeep P NP 1 Reply Last reply
          0
          • Pradeep P NP Pradeep P N

            Hi @opengpu

            One workaround would be using 2 QLable (1 for image & 1 for text) in Layout and use it.
            Need to be tested

            O Offline
            O Offline
            opengpu
            wrote on last edited by
            #7

            @Pradeep-P-N worked. but the dockButton and closeButton disapear....i want the buttons

            Pradeep P NP 1 Reply Last reply
            0
            • O opengpu

              thank you.
              if i want to set a color-border to the container-Widget, should i use stylesheet?

              Pradeep P NP Offline
              Pradeep P NP Offline
              Pradeep P N
              wrote on last edited by
              #8

              @opengpu
              Yes you can try setting the StyleSheet for the styles/design needed.

              All the best.

              Pradeep Nimbalkar.
              Upvote the answer(s) that helped you to solve the issue...
              Keep code clean.

              1 Reply Last reply
              0
              • O opengpu

                @Pradeep-P-N worked. but the dockButton and closeButton disapear....i want the buttons

                Pradeep P NP Offline
                Pradeep P NP Offline
                Pradeep P N
                wrote on last edited by Pradeep P N
                #9

                @opengpu

                Did you try setting the Qt::WindowFlags ?
                And is it possible to share the sample code ? So i can check it.

                All the best.

                Pradeep Nimbalkar.
                Upvote the answer(s) that helped you to solve the issue...
                Keep code clean.

                O 1 Reply Last reply
                0
                • Pradeep P NP Pradeep P N

                  @opengpu

                  Did you try setting the Qt::WindowFlags ?
                  And is it possible to share the sample code ? So i can check it.

                  All the best.

                  O Offline
                  O Offline
                  opengpu
                  wrote on last edited by
                  #10

                  @Pradeep-P-N
                  setTitleBarWidget with a QWidget which include a HLayout of two QLabel.

                  Pradeep P NP 1 Reply Last reply
                  0
                  • O opengpu

                    @Pradeep-P-N
                    setTitleBarWidget with a QWidget which include a HLayout of two QLabel.

                    Pradeep P NP Offline
                    Pradeep P NP Offline
                    Pradeep P N
                    wrote on last edited by
                    #11

                    Hi @opengpu
                    Can you please once check the QDockWidget::setTitleBarWidget(QWidget *widget) documentation there some tips for implementing custom title bars.

                    • If a title bar widget is set, QDockWidget will not use native window decorations when it is floated.

                    Pradeep Nimbalkar.
                    Upvote the answer(s) that helped you to solve the issue...
                    Keep code clean.

                    1 Reply Last reply
                    3
                    • O opengpu

                      Display icon&text on QLabel .
                      i want to show icon & title on the DockWidget header.
                      i use setTitleBarWidget, and it works.
                      but i use QLabel as the param of setTitleBarWidget, QLabel can show both the setPixmap & setText at the same time...

                      J Offline
                      J Offline
                      Jerry.Wilson
                      wrote on last edited by
                      #12

                      @opengpu

                      Or you can use your own QProxyStyle to make it:

                      class DrawIconDockWidgetStyle: public QProxyStyle
                      {
                      Q_OBJECT

                      public:
                      DrawIconDockWidgetStyle(const QIcon& icon,
                      QStyle* style = nullptr)
                      : QProxyStyle(style)
                      , mIcon(icon){}
                      virtual ~DrawIconDockWidgetStyle(){}

                      virtual void drawControl(ControlElement element,
                                                  const QStyleOption* option,
                                                  QPainter* painter,
                                                  const QWidget* widget = nullptr) const
                      {
                          if(QStyle::CE_DockWidgetTitle == element)
                          {
                              int IconWidth = pixelMetric(QStyle::PM_ToolBarIconSize)-3;
                              int Margin = baseStyle()->pixelMetric(QStyle::PM_DockWidgetTitleMargin);
                              QPoint IconPoint(Margin + option->rect.left(), Margin + option->rect.center().y() - IconWidth/2);
                              painter->drawPixmap(IconPoint, mIcon.pixmap(IconWidth, IconWidth));
                              const_cast<QStyleOption*>(option)->rect = option->rect.adjusted(IconWidth, 0, 0, 0);
                          }
                          QProxyStyle::drawControl(element, option, painter, widget);
                      }
                      

                      private:
                      QIcon mIcon;
                      };
                      in .cpp:
                      ui->dockWidget_3->setStyle(new DrawIconDockWidgetStyle( QIcon(":/1_toolbar_normal.png"), ui->dockWidget_3->style()));
                      ui->dockWidget_3->setWindowTitle("hey~~");

                      0_1560412533730_无标题.png

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved