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. I am getting this error "QPainter::setPen: Painter not active"
Forum Updated to NodeBB v4.3 + New Features

I am getting this error "QPainter::setPen: Painter not active"

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

    @QPainter myPainter(ui->graphicsView);
    myPainter.setPen(QPen(QBrush("#535353"), 0.5));

    myPainter.setRenderHint(QPainter::Antialiasing);
    
    int h = height();
    int w = width();
    
    myPainter.translate(QPoint(w/2, h/2));
    
    for (qreal rot=0; rot < 360.0; rot+=5.0 )
    {
        myPainter.drawEllipse(-125, -40, 250, 80);
        myPainter.rotate(5.0);
    }@
    

    error:
    @QPainter::begin: Widget painting can only begin as a result of a paintEvent
    QPainter::setPen: Painter not active
    QPainter::setRenderHint: Painter must be active to set rendering hints
    QPainter::translate: Painter not active
    QPainter::rotate: Painter not active
    QPainter::rotate: Painter not active
    QPainter::rotate: Painter not active
    QPainter::rotate: Painter not active
    QPainter::rotate: Painter not active
    QPainter::rotate: Painter not active@

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      You should subclass QGraphicsView, then put these code to its paintEvent();

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        However, I don't know why you want to draw onto QGraphicsView, instead of QGraphicsScene.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gmaro
          wrote on last edited by
          #4

          Message is clear.

          If you want to Use QPainter to paint on any QPaintDevice you should reimplement
          @virtual void QWidget::paintEvent ( QPaintEvent * event )@
          In your case you need to subclass the QGraphicsView and reimplement the function, or subclass this widget, depending what you need.
          Inside that function put your code.
          you can also use the QPainter::begin function.

          To triger painting call the update() or redraw() in place where you need to trigger paining manually.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #5

            [quote author="gmaro" date="1333663206"]
            If you want to Use QPainter to paint on any QPaintDevice you should reimplement
            [/quote]

            Not any, just QWidget and all its subClasses. Other QPaintDevice, such as QImage, QPixmap, etc doesn't have relationship with paintEvent().

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gmaro
              wrote on last edited by
              #6

              Yes I know that, I was refering to the current case only, and it's clear it's about the widgets.

              Btw. It would be hard to reimplement the QImage or QPixmap's paintEvent function :)

              1 Reply Last reply
              0
              • A Offline
                A Offline
                adnan
                wrote on last edited by
                #7

                Thanks for your replies.

                Actually I am implementing a crop function. I want to draw a rectangle on an image displayed in qgraphicsview widget. the rectangle should be resizable on its ends using mouse. Once the user finalises the part of image, the program would use copy function of QImage to extract the cropped part.
                So, can you plz guide me with creating a resizable rectangle. If I reimplement the painter function of my mainwindow class, the rectangle gets displayed at the beginning. It should appear only when crop is invoked.

                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