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. QPainter won't draw
Forum Updated to NodeBB v4.3 + New Features

QPainter won't draw

Scheduled Pinned Locked Moved General and Desktop
qpainterqwidget
3 Posts 2 Posters 891 Views 2 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.
  • C Offline
    C Offline
    Chick3nN00dleS0up
    wrote on last edited by
    #1

    I've got a class "ComponentArea" which is derived from QScrollArea. The ComponentArea has some other QWidget-derivatives in it.
    I modify the palette and enable mouseTracking in the constructor:

    ComponentArea::ComponentArea(QWidget* parent) : QScrollArea(parent)
    {
        QPalette palette = this->palette();
    
        palette.setColor(QPalette::Base, Qt::white);
        palette.setColor(QPalette::Window, Qt::white);
        palette.setColor(QPalette::AlternateBase, Qt::white);
    
        this->setPalette(palette);
        this->setMouseTracking(true);
    }
    

    The mouseMoveEvent calls repaint(), which triggers the paintEvent:

    void ComponentArea::mouseMoveEvent(QMouseEvent *event)
    {
        if(pressedSocket_first != 0)
        {
            mousePos_global = event->globalPos();
            this->repaint();
        }
    }
    
    void ComponentArea::paintEvent(QPaintEvent *event)
    {
        Q_UNUSED(event)
    
        QPainter* painter = new QPainter(this);
        painter->eraseRect(QRect(QPoint(0,0), QPoint(this->size().width(), this->size().height())));
        painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::SquareCap));
    
        if(pressedSocket_first != 0)
        {
    
            QPoint socketPos = this->mapFromGlobal(pressedSocket_first->mapToGlobal(QPoint(0,0)));
            socketPos.setX(socketPos.x() + pressedSocket_first->size().width() /2);
            socketPos.setY(socketPos.y() + pressedSocket_first->size().height() /2);
    
            QPoint mousePos = this->mapFromGlobal(mousePos_global);
    
            painter->drawLine(socketPos, mousePos);
        }
    }
    

    The line does not appear. With debugging, I made sure that drawLine() is in fact reached and the values of socketPos and mousePos make sense. Nevertheless the line does not appear. What am I doing wrong? Thanks in advance!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      QPainter paints only when endPaint() is called (the destructor calls endPaint as well).
      I suggest to change your code using a stack variable instead a pointer QPainter painter instead of QPainte *painter also because you never delete created painter

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      C 1 Reply Last reply
      0
      • M mcosta

        Hi and welcome to devnet,

        QPainter paints only when endPaint() is called (the destructor calls endPaint as well).
        I suggest to change your code using a stack variable instead a pointer QPainter painter instead of QPainte *painter also because you never delete created painter

        C Offline
        C Offline
        Chick3nN00dleS0up
        wrote on last edited by Chick3nN00dleS0up
        #3

        @mcosta Hm, neither calling painter->end() manually nor using a "usual" variable solves it. I draw in the ComponentView's children the same way, and it works perfectly.

        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