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. Painting in same widget above child objects of Itself
QtWS25 Last Chance

Painting in same widget above child objects of Itself

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.9k 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #1

    Sample code below.

    1. I have widget with child buttons.
    2. paintEvent(..) reimplemented. I'm drawing the line in paintEvent.

    Issue - Line always below the buttons. I would like to see the line above the buttons. Is there a way to achieve this ?

    MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    {
    createUI();
    }

    void MyWidget::createUI()
    {
    b1 = new QPushButton("Monitor");
    b2 = new QPushButton("Help");

    hLyt = new QHBoxLayout;
    hLyt->addWidget(b1);
    hLyt->addWidget(b2);
    
    this->setLayout(hLyt);
    

    }

    void MyWidget::paintEvent(QPaintEvent *event)
    {
    QPainter paint(this);
    paint.setBrush(Qt::blue);
    QPen pen;
    pen.setWidth(4);
    pen.setBrush(Qt::red);
    paint.setPen(pen);
    paint.drawLine(30,0,30,this->rect().height());
    }
    0_1495032721254_Scree_Line_Below.png

    Dheerendra
    @Community Service
    Certified Qt Specialist
    http://www.pthinks.com

    A 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      IIRC, in that kind of situation, you would rather have an overlay widget on top of the other and paint that one.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • dheerendraD dheerendra

        Sample code below.

        1. I have widget with child buttons.
        2. paintEvent(..) reimplemented. I'm drawing the line in paintEvent.

        Issue - Line always below the buttons. I would like to see the line above the buttons. Is there a way to achieve this ?

        MyWidget::MyWidget(QWidget *parent)
        : QWidget(parent)
        {
        createUI();
        }

        void MyWidget::createUI()
        {
        b1 = new QPushButton("Monitor");
        b2 = new QPushButton("Help");

        hLyt = new QHBoxLayout;
        hLyt->addWidget(b1);
        hLyt->addWidget(b2);
        
        this->setLayout(hLyt);
        

        }

        void MyWidget::paintEvent(QPaintEvent *event)
        {
        QPainter paint(this);
        paint.setBrush(Qt::blue);
        QPen pen;
        pen.setWidth(4);
        pen.setBrush(Qt::red);
        paint.setPen(pen);
        paint.drawLine(30,0,30,this->rect().height());
        }
        0_1495032721254_Scree_Line_Below.png

        A Offline
        A Offline
        ambershark
        wrote on last edited by
        #3

        @dheerendra The children are always drawn on top of the widget. So I don't think there's a way to do that other than putting another widget on top like @SGaist suggested.

        I helped someone on the forums here recently with a tour my app type widget that did something similar. And I had to make it part of the widget I wanted to highlight. I couldn't just draw over other widgets. There may be a way but nothing popped into my mind while I was doing that.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        1
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Thank you @SGaist @ambershark. I have achieved my requirements through overlay. Just wanted to check possibility. I think it does not make sense to draw parent over children. So my question itself is not valid :)

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          1

          • Login

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