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. Draw a PE_IndicatorBranch in a QLabel

Draw a PE_IndicatorBranch in a QLabel

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 2 Posters 4.3k Views 1 Watching
  • 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.
  • mrjjM mrjj

    @oberluz
    Hi
    well if you look at the test code, it seems that
    style()->drawPrimitive(QStyle::PE_IndicatorBranch draws the
    the indicator in the center of the rect given.
    It should not matter if inside a QListwidget but you might want to
    check the rect label.

    O Offline
    O Offline
    oberluz
    wrote on last edited by oberluz
    #5

    @mrjj Yes I noticed that. I've replicated your test example and I can now get it to display in the row of a QListWidget. Thanks

    mrjjM 1 Reply Last reply
    0
    • O oberluz

      @mrjj Yes I noticed that. I've replicated your test example and I can now get it to display in the row of a QListWidget. Thanks

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @oberluz
      You can simply calculate a new rect for the beginning of the text line and
      then ask QLabel to center text or similar.
      If you want complete control you should draw both text and PE_IndicatorBranch
      with painter.
      (and not call QLabel::paintEvent(e); )
      However, if you use rich text in Label or mutliple lines it is slightly more complicated.

      O 1 Reply Last reply
      4
      • mrjjM mrjj

        @oberluz
        You can simply calculate a new rect for the beginning of the text line and
        then ask QLabel to center text or similar.
        If you want complete control you should draw both text and PE_IndicatorBranch
        with painter.
        (and not call QLabel::paintEvent(e); )
        However, if you use rich text in Label or mutliple lines it is slightly more complicated.

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

        @mrjj thanks. I was able to get it to work if the QListWidget was the central widget in the QApplicaiton as per your example. However if I add it to a dock widget they fail to draw:

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        
        namespace Ui {
            class MainWindow;
        }
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
        
            QWidget* createListWidget();
        
        private:
            Ui::MainWindow *ui;
        };
        
        #endif // MAINWINDOW_H
        
        #include "MainWindow.h"
        #include "ui_MainWindow.h"
        #include <QListWidget>
        #include <QLabel>
        #include <QPainter>
        #include <QHBoxLayout>
        #include <QApplication>
        #include <iostream>
        #include <QDockWidget>
        
        class NodeWidget : public QLabel
        {
        public:
            NodeWidget(QWidget *parent = NULL) :
                QLabel("", parent)
            {
            }
        
        private:
             void paintEvent(QPaintEvent *);
        };
        
        void NodeWidget::paintEvent(QPaintEvent *e)
        {
            QLabel::paintEvent(e);
            QPainter painter(this);
            QStyleOption option;
            option.initFrom(this);
            option.rect = rect();
            option.state = QStyle::State_Item | QStyle::State_Children;
            style()->drawPrimitive(QStyle::PE_IndicatorBranch, &option, &painter, this);
        }
        
        class RowWidget : public QWidget
        {
        public:
            RowWidget(const QString& text, QWidget *parent = NULL);
        };
        
        
        RowWidget::RowWidget(const QString &text, QWidget *parent) :
            QWidget(parent)
        {
            QHBoxLayout* layout = new QHBoxLayout(this);
            layout->setContentsMargins(0,0,0,0);
        
            layout->addWidget(new NodeWidget(this));
            layout->addWidget(new QLabel(text, this));
            layout->addWidget(new QLabel("text", this));
        
            setLayout(layout);
        }
        
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            layout()->setContentsMargins(0,0,0,0);
        
            QString styleSheet("QDockWidget::title { font: 75 11pt \"ubuntu\"; text-align: left; background: darkgray; }");
        
            QDockWidget* m_dockWidget;
            m_dockWidget = new QDockWidget(" Dock Window", this);
            m_dockWidget->setObjectName(QString::fromUtf8("DockWidget"));
            QSizePolicy sizePolicy6(QSizePolicy::Minimum, QSizePolicy::Minimum);
            sizePolicy6.setHorizontalStretch(0);
            sizePolicy6.setVerticalStretch(0);
            sizePolicy6.setHeightForWidth(m_dockWidget->sizePolicy().hasHeightForWidth());
            m_dockWidget->setSizePolicy(sizePolicy6);
            m_dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea|Qt::TopDockWidgetArea|Qt::BottomDockWidgetArea);
        
            m_dockWidget->layout()->setContentsMargins(0,0,0,0);
            m_dockWidget->setStyleSheet(styleSheet);
        
            addDockWidget(Qt::BottomDockWidgetArea, m_dockWidget);
        
            QWidget* list = createListWidget();
            m_dockWidget->setWidget(list);
        
            {
                QWidget* list = createListWidget();
                setCentralWidget(list);
            }
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        QWidget *MainWindow::createListWidget()
        {
            QListWidget* list = new QListWidget(this);
            list->setAlternatingRowColors(true);
        
            RowWidget* widget = new RowWidget("one", list);
            QListWidgetItem* item = new QListWidgetItem(list);
            item->setSizeHint(widget->minimumSizeHint());
            list->addItem(item);
            list->setItemWidget(item, widget);
        
            widget = new RowWidget("two", list);
            item = new QListWidgetItem(list);
            item->setSizeHint(widget->minimumSizeHint());
            list->addItem(item);
            list->setItemWidget(item, widget);
        
            widget = new RowWidget("three", list);
            item = new QListWidgetItem(list);
            item->setSizeHint(widget->minimumSizeHint());
            list->addItem(item);
            list->setItemWidget(item, widget);
        
            return list;
        }
        

        0_1561451749908_dockwidget.png

        O 1 Reply Last reply
        1
        • O oberluz

          @mrjj thanks. I was able to get it to work if the QListWidget was the central widget in the QApplicaiton as per your example. However if I add it to a dock widget they fail to draw:

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          
          namespace Ui {
              class MainWindow;
          }
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
          
              QWidget* createListWidget();
          
          private:
              Ui::MainWindow *ui;
          };
          
          #endif // MAINWINDOW_H
          
          #include "MainWindow.h"
          #include "ui_MainWindow.h"
          #include <QListWidget>
          #include <QLabel>
          #include <QPainter>
          #include <QHBoxLayout>
          #include <QApplication>
          #include <iostream>
          #include <QDockWidget>
          
          class NodeWidget : public QLabel
          {
          public:
              NodeWidget(QWidget *parent = NULL) :
                  QLabel("", parent)
              {
              }
          
          private:
               void paintEvent(QPaintEvent *);
          };
          
          void NodeWidget::paintEvent(QPaintEvent *e)
          {
              QLabel::paintEvent(e);
              QPainter painter(this);
              QStyleOption option;
              option.initFrom(this);
              option.rect = rect();
              option.state = QStyle::State_Item | QStyle::State_Children;
              style()->drawPrimitive(QStyle::PE_IndicatorBranch, &option, &painter, this);
          }
          
          class RowWidget : public QWidget
          {
          public:
              RowWidget(const QString& text, QWidget *parent = NULL);
          };
          
          
          RowWidget::RowWidget(const QString &text, QWidget *parent) :
              QWidget(parent)
          {
              QHBoxLayout* layout = new QHBoxLayout(this);
              layout->setContentsMargins(0,0,0,0);
          
              layout->addWidget(new NodeWidget(this));
              layout->addWidget(new QLabel(text, this));
              layout->addWidget(new QLabel("text", this));
          
              setLayout(layout);
          }
          
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              layout()->setContentsMargins(0,0,0,0);
          
              QString styleSheet("QDockWidget::title { font: 75 11pt \"ubuntu\"; text-align: left; background: darkgray; }");
          
              QDockWidget* m_dockWidget;
              m_dockWidget = new QDockWidget(" Dock Window", this);
              m_dockWidget->setObjectName(QString::fromUtf8("DockWidget"));
              QSizePolicy sizePolicy6(QSizePolicy::Minimum, QSizePolicy::Minimum);
              sizePolicy6.setHorizontalStretch(0);
              sizePolicy6.setVerticalStretch(0);
              sizePolicy6.setHeightForWidth(m_dockWidget->sizePolicy().hasHeightForWidth());
              m_dockWidget->setSizePolicy(sizePolicy6);
              m_dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea|Qt::TopDockWidgetArea|Qt::BottomDockWidgetArea);
          
              m_dockWidget->layout()->setContentsMargins(0,0,0,0);
              m_dockWidget->setStyleSheet(styleSheet);
          
              addDockWidget(Qt::BottomDockWidgetArea, m_dockWidget);
          
              QWidget* list = createListWidget();
              m_dockWidget->setWidget(list);
          
              {
                  QWidget* list = createListWidget();
                  setCentralWidget(list);
              }
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          QWidget *MainWindow::createListWidget()
          {
              QListWidget* list = new QListWidget(this);
              list->setAlternatingRowColors(true);
          
              RowWidget* widget = new RowWidget("one", list);
              QListWidgetItem* item = new QListWidgetItem(list);
              item->setSizeHint(widget->minimumSizeHint());
              list->addItem(item);
              list->setItemWidget(item, widget);
          
              widget = new RowWidget("two", list);
              item = new QListWidgetItem(list);
              item->setSizeHint(widget->minimumSizeHint());
              list->addItem(item);
              list->setItemWidget(item, widget);
          
              widget = new RowWidget("three", list);
              item = new QListWidgetItem(list);
              item->setSizeHint(widget->minimumSizeHint());
              list->addItem(item);
              list->setItemWidget(item, widget);
          
              return list;
          }
          

          0_1561451749908_dockwidget.png

          O Offline
          O Offline
          oberluz
          wrote on last edited by oberluz
          #8

          @oberluz said in Draw a PE_IndicatorBranch in a QLabel:

          drawPrimitive

          my bad: the style sheet is causing the issue. Can't see why that could cause it?

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #9

            Hi
            What does the stylesheet target?
            margins and such can affect lots of stuff if not
            target using names or concrete types as selectors

            O 1 Reply Last reply
            1
            • mrjjM mrjj

              Hi
              What does the stylesheet target?
              margins and such can affect lots of stuff if not
              target using names or concrete types as selectors

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

              @mrjj I only intended it to darken the dock window title bar:

              QString styleSheet("QDockWidget::title { font: 75 11pt \"ubuntu\"; text-align: left; background: darkgray; }");
              
              mrjjM 1 Reply Last reply
              0
              • O oberluz

                @mrjj I only intended it to darken the dock window title bar:

                QString styleSheet("QDockWidget::title { font: 75 11pt \"ubuntu\"; text-align: left; background: darkgray; }");
                
                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #11

                @oberluz
                so if you do use styleSheet it looks as you expect?
                you are right, im not sure either how that should affect NodeWidget

                O 1 Reply Last reply
                0
                • mrjjM mrjj

                  @oberluz
                  so if you do use styleSheet it looks as you expect?
                  you are right, im not sure either how that should affect NodeWidget

                  O Offline
                  O Offline
                  oberluz
                  wrote on last edited by
                  #12

                  @mrjj yes, without the stylesheet its fine:

                  0_1561455516847_without_style.png

                  Is its possible I need to get the current style sheet, then somehow add the title, and then set it ? I'm thinking that by just setting it rather than modifying it I might be deleting something

                  mrjjM 1 Reply Last reply
                  1
                  • O oberluz

                    @mrjj yes, without the stylesheet its fine:

                    0_1561455516847_without_style.png

                    Is its possible I need to get the current style sheet, then somehow add the title, and then set it ? I'm thinking that by just setting it rather than modifying it I might be deleting something

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @oberluz
                    Hi
                    Unless you manually set a stylesheet, its empty pr default.
                    so if that is the ONLY stylesheet you set, i dont get it.

                    O 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      @oberluz
                      Hi
                      Unless you manually set a stylesheet, its empty pr default.
                      so if that is the ONLY stylesheet you set, i dont get it.

                      O Offline
                      O Offline
                      oberluz
                      wrote on last edited by
                      #14

                      @mrjj its the only one, the example I posted is complete

                      mrjjM 1 Reply Last reply
                      0
                      • O oberluz

                        @mrjj its the only one, the example I posted is complete

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        @oberluz
                        Sorry, i really cant guess.
                        It seems not very connected.
                        and should only affect Title

                        O 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          @oberluz
                          Sorry, i really cant guess.
                          It seems not very connected.
                          and should only affect Title

                          O Offline
                          O Offline
                          oberluz
                          wrote on last edited by
                          #16

                          @mrjj Maybe a bug.. I might raise one with this example

                          mrjjM 1 Reply Last reply
                          0
                          • O oberluz

                            @mrjj Maybe a bug.. I might raise one with this example

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #17

                            @oberluz
                            Could be.
                            Please make a small sample that can reproduce it.
                            Makes it far more likely we discover what it is. :)

                            O 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @oberluz
                              Could be.
                              Please make a small sample that can reproduce it.
                              Makes it far more likely we discover what it is. :)

                              O Offline
                              O Offline
                              oberluz
                              wrote on last edited by
                              #18

                              @mrjj will do and I'll run it under 5.13 before submititng

                              mrjjM 1 Reply Last reply
                              3
                              • O oberluz

                                @mrjj will do and I'll run it under 5.13 before submititng

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #19

                                @oberluz
                                Super.
                                and please post link to bug here :)

                                O 1 Reply Last reply
                                1
                                • mrjjM mrjj

                                  @oberluz
                                  Super.
                                  and please post link to bug here :)

                                  O Offline
                                  O Offline
                                  oberluz
                                  wrote on last edited by
                                  #20

                                  @mrjj https://bugreports.qt.io/browse/QTBUG-76947
                                  as promised. reproduced it with 5.13.

                                  1 Reply Last reply
                                  4

                                  • Login

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