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. [SOLVED] Subclassing QWidget not working

[SOLVED] Subclassing QWidget not working

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.0k 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.
  • guidupasG Offline
    guidupasG Offline
    guidupas
    wrote on last edited by
    #1

    Hello all!

    I need to subclass QWidget, but its not working. When I call the function desenhaFluxo(), it returns the error:
    QWidget::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::setPen: Painter not active

    How can I do that?

    @
    #include "fluxocaixawidget.h"

    fluxoCaixaWidget::fluxoCaixaWidget(QWidget *parent) :
    QWidget(parent)
    {

    }

    void fluxoCaixaWidget::paintEvent(QPaintEvent *)
    {
    QPainter painter(this);

    QPen caneta(Qt::black);
    caneta.setWidth(4);
    painter.setPen(caneta);
    
    
    
    painter.drawLine(20, this->parentWidget()->height() / 2 - 20, this->parentWidget()->width()-40, this->parentWidget()->height() / 2 - 20);
    

    }

    void fluxoCaixaWidget::desenhaFluxo()
    {
    QPainter painter(this);

    QPen caneta(Qt::black);
    caneta.setWidth(4);
    painter.setPen(caneta);
    
    painter.drawLine(10,10,50,10);
    

    }

    @

    Att.
    Guilherme Cortada Dupas

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Quoting from the documentation:
      [quote]Each widget performs all painting operations from within its paintEvent() function. This is called whenever the widget needs to be redrawn, either as a result of some external change or when requested by the application.[/quote]

      I hope the reason for your warnings is clear now.

      (Z(:^

      1 Reply Last reply
      0
      • guidupasG Offline
        guidupasG Offline
        guidupas
        wrote on last edited by
        #3

        Thanks for the reply Sierdzio!

        I got it. But how could I do draw other lines calling a function like the code above?

        Att.
        Guilherme Cortada Dupas

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          Re-implement paintEvent() and perform all required drawing operations in there. As the docs say, Qt will call that method whenever your Widget needs to be (re)drawn. Of course you can call sub-functions, like desenhaFluxo(), from there. Just do not call functions which perform painting operations directly!

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • guidupasG Offline
            guidupasG Offline
            guidupas
            wrote on last edited by
            #5

            Worked fine after clean the project

            Att.
            Guilherme Cortada Dupas

            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