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 doesnt start
Forum Updated to NodeBB v4.3 + New Features

QPainter doesnt start

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 3 Posters 1.9k 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.
  • J Offline
    J Offline
    jss193
    wrote on last edited by
    #5

    Thank you very much, but why QPainter does not begin? In fact, I am not able to know why if(engine).... does not run.

    Thank you four your useful help!

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

      Did you properly allocate engine at any point ?

      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
      • J Offline
        J Offline
        jss193
        wrote on last edited by
        #7

        I dont really know how to do it, can give me any hint please?

        Thanks again

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

          You don't know how to do what ? Allocate an object in C++ ?
          What is engine ?
          Where did you declare it ?

          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
          • BuckwheatB Offline
            BuckwheatB Offline
            Buckwheat
            wrote on last edited by
            #9

            Hi @jss193

            Welcome!
            Yes, @SGaist is correct. Only one painter is needed to all your work. You need to rethink your design because to be honest it is a mess!

            First of all. you paint your QSvgRenderer object by calling the render method and passing in the pointer to your painter.

            For example:
            QPainter painter (this);
            if (generator) generator->renderer (&painter);
            else {
            // Do your other painting
            }

            For your engine, it must be derived from QPaintDevice. Then just call:
            QPainter painter (engine);

            Notice you do NOT need to call begin! The constructor automatically calls begin for you. You can also only create ONE painter at a time for a QPaintDevice. Please read: http://doc.qt.io/qt-5/qpainter.html
            and read about "begin" carefully and notice the warnings. If you decide not to scope your painter and call begin, please make sure you call end when you complete the painting.

            For example:
            QPainter painter;
            if (engine) painter.begin (engine);
            else painter.begin (this);
            // Paint here!
            painter.end ();

            This allows painting to be done on the QPaintDevice at another time.

            I am not going to begin to guess why your application does not compile. Without your PRO file and other code it could be anything. I suggest you look at your QT and CONFIG variables in your PRO file to make sure you can access the QSvgRenderer (add SVG to the QT variable). Also, please read documentation. It is actually very good and can help you get started.

            Dave Fileccia

            1 Reply Last reply
            1
            • J Offline
              J Offline
              jss193
              wrote on last edited by
              #10

              Thank you for your answers, I realized (late) that qpainter really starts because it shows objects on the screen, so, I guess that Qpainter starts though I see on execution that it is not started. The problem appears when I try to save svg.

              And yes I included svg var in my pro file. Any suggestion?

              Thank you all

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

                You're not painting on your generator. Once you fix that it's going to be good.

                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
                • J Offline
                  J Offline
                  jss193
                  wrote on last edited by jss193
                  #12

                  @Buckwheat said in QPainter doesnt start:

                  On my screen appear things that Im painting (object QPainter), when I declare QSvgGenerator generator object, how can I paint on this object?

                  I tried the following code, but it has the following error: The program has unexpectedly finished.

                  void MainWindow::on_actionSave_as_SVG_triggered()
                  {
                      QString filePath = QFileDialog::getSaveFileName(this,tr("Save SVG"),filePath,tr("SVG files (*.svg"));
                  
                      if ( filePath.isEmpty()){
                           return;
                      }
                  
                      QSvgGenerator generator;
                  
                      generator.setFileName(filePath);
                      generator.setSize(QSize(this->width(),this->height()));
                      generator.setViewBox(QRect(0,0,this->width(),this->height()));
                      generator.setTitle("SVG Example");
                      generator.setDescription(tr("An SVG drawing created by the SVG Generator "
                                                      "Example provided with Qt."));
                  
                      paintAll(&generator);
                  
                  }
                  
                  void MainWindow::paintEvent(QPaintEvent *event)
                  {
                  
                  paintAll();
                  
                  }
                  
                  void MainWindow::paintAll(QSvgGenerator *generator){
                  
                      QPainter painter;
                      painter.begin(generator);
                  
                      painter.setFont(QFont("Times",14,QFont::Bold));
                      painter.drawText(QPoint(20,30),"Hello");
                  
                      painter.drawLine(QPoint(50,60),QPoint(100,100));
                  
                      painter.setBrush(Qt::BDiagPattern);
                      painter.drawRect(QRect(70,120,80,10));
                  
                      QPen ellipsePen;
                      ellipsePen.setColor(Qt::red);
                      ellipsePen.setStyle(Qt::DashLine);
                  
                      painter.setPen(ellipsePen);
                      painter.drawEllipse(QPoint(80,200),50,20);
                  
                      QPainterPath rectPath;
                      rectPath.addRect(QRect(150,20,100,50));
                  
                  
                      painter.setPen(QPen(Qt::red,1,Qt::DashLine,Qt::FlatCap,Qt::MiterJoin));
                      painter.setBrush(Qt::black);
                      painter.drawPath(rectPath);
                  
                  
                      painter.end();
                  
                  
                  }
                  

                  Thank u all!

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jss193
                    wrote on last edited by
                    #13

                    I found a solution (more or less), I included in pro file:
                    QT += core gui svg
                    QT += svg
                    and the library #include <QtSvg>

                    Then I allocated dynamically the object :

                    void MainWindow::on_actionSave_as_SVG_triggered()
                    {
                        QString filePath = QFileDialog::getSaveFileName(this,tr("Save SVG"),filePath,tr("SVG files (*.svg"));
                    
                        if ( filePath.isEmpty()){
                             return;
                        }
                    
                        QSvgGenerator* generator= new QSvgGenerator;
                    
                        generator->setFileName(filePath);
                        generator->setSize(QSize(this->width(),this->height()));
                        generator->setViewBox(QRect(0,0,this->width(),this->height()));
                        generator->setTitle("SVG Example");
                        generator->setDescription(tr("An SVG drawing created by the SVG Generator "
                                                        "Example provided with Qt."));
                    
                        paintAll(generator);
                        delete generator;
                    }
                    
                    

                    and

                    void MainWindow::paintEvent(QPaintEvent *event)
                    {
                    
                    paintAll();
                    }
                    
                    void MainWindow::paintAll(QSvgGenerator *generator){
                    
                        QPainter painter;
                        if (generator){
                            painter.begin(generator);
                        }else{
                            painter.begin(this);
                        }
                    
                    
                        painter.setFont(QFont("Times",14,QFont::Bold));
                        painter.drawText(QPoint(20,30),"Hello");
                    
                        painter.drawLine(QPoint(50,60),QPoint(100,100));
                    
                        painter.setBrush(Qt::BDiagPattern);
                        painter.drawRect(QRect(70,120,80,10));
                    
                        QPen ellipsePen;
                        ellipsePen.setColor(Qt::red);
                        ellipsePen.setStyle(Qt::DashLine);
                    
                        painter.setPen(ellipsePen);
                        painter.drawEllipse(QPoint(80,200),50,20);
                    
                        QPainterPath rectPath;
                        rectPath.addRect(QRect(150,20,100,50));
                    
                    
                        painter.setPen(QPen(Qt::red,1,Qt::DashLine,Qt::FlatCap,Qt::MiterJoin));
                        painter.setBrush(Qt::black);
                        painter.drawPath(rectPath);
                    
                    
                        painter.end();
                    
                    
                    }
                    
                    

                    Now the problem is that what I watch on my screen is not exactly saved on svg, for instance DashLine is a black object(object shape is correct).

                    Any suggestion?

                    Thank you very much!

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

                      You are setting a back brush so it seems that the output is correct, no ?

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

                      J 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        You are setting a back brush so it seems that the output is correct, no ?

                        J Offline
                        J Offline
                        jss193
                        wrote on last edited by
                        #15

                        @SGaist
                        Yes, but what I watch on my screen is not the same as in svg file...

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

                          I don't know what you have on the screen nor what you have in your file.

                          You should post a picture of each. It might give clues.

                          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
                          0
                          • J Offline
                            J Offline
                            jss193
                            wrote on last edited by
                            #17

                            @SGaist
                            First is what I see on my screen:
                            0_1537217709822_screen.jpeg

                            And this is what I see on Svg file:

                            0_1537217729755_2.jpeg

                            Thank you

                            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