Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Display icon&text on QLabel

    General and Desktop
    5
    12
    2760
    Loading More Posts
    • 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.
    • O
      opengpu last edited by

      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...

      jsulm J 2 Replies Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @opengpu last edited by jsulm

        @opengpu I will assume that your question is whether QLabel can have text and pixmap at the same time as you actually did not ask any question.
        The documentation answers your question and you can easily check this with a small test app. Short answer: no, QLabel cannot show both at the same time.

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

        1 Reply Last reply Reply Quote 5
        • Pradeep P N
          Pradeep P N last edited by

          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.

          jsulm O 2 Replies Last reply Reply Quote 5
          • jsulm
            jsulm Lifetime Qt Champion @Pradeep P N last edited by

            @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 Reply Quote 0
            • J.Hilk
              J.Hilk Moderators last edited by

              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

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

              1 Reply Last reply Reply Quote 4
              • O
                opengpu last edited by

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

                Pradeep P N 1 Reply Last reply Reply Quote 0
                • O
                  opengpu @Pradeep P N last edited by

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

                  Pradeep P N 1 Reply Last reply Reply Quote 0
                  • Pradeep P N
                    Pradeep P N @opengpu last edited by

                    @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 Reply Quote 0
                    • Pradeep P N
                      Pradeep P N @opengpu last edited by 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.

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

                      O 1 Reply Last reply Reply Quote 0
                      • O
                        opengpu @Pradeep P N last edited by

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

                        Pradeep P N 1 Reply Last reply Reply Quote 0
                        • Pradeep P N
                          Pradeep P N @opengpu last edited by

                          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 Reply Quote 3
                          • J
                            Jerry.Wilson @opengpu last edited by

                            @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 Reply Quote 0
                            • First post
                              Last post