Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPainter doesnt start



  • Hello all, Im trying to create a program where I use Qpainter object to paint some objects and then save it as a SVG, the problem is that QPainter doesnt initiate, I show you my code:

    mainwindow.h

    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        virtual void paintEvent(QPaintEvent *event);
        void paintAll(QSvgGenerator *generator = 0 );
    
    

    mainwindow.cpp

    void MainWindow::on_actionSave_as_SVG_triggered()
    {
        QString filePath = QFileDialog::getSaveFileName(this,"Save SVG","","SVG files (*.svg");
        if ( filePath == ""){
             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");
    
        paintAll(&generator);
    }
    
    void MainWindow::paintEvent(QPaintEvent *event)
    {
    
    paintAll();
        
    }
    
    void MainWindow::paintAll(QSvgGenerator *generator){
        QPainter painter(this);
    
        painter.setFont(QFont("Times",14,QFont::Bold));
        painter.drawText(QPoint(20,30),"Hello WOrld");
    
        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);
    
        QPainter ellipsePainter(this);
        ellipsePainter.setPen(ellipsePen);
        ellipsePainter.drawEllipse(QPoint(80,200),50,20);
    
        QPainterPath rectPath;
        rectPath.addRect(QRect(150,20,100,50));
    
        QPainter pathPainter(this);
        pathPainter.setPen(QPen(Qt::red,1,Qt::DashLine,Qt::FlatCap,Qt::MiterJoin));
        pathPainter.setBrush(Qt::black);
        pathPainter.drawPath(rectPath);
    
    }
    
    

    The problem appears when I try to save svg archive, is that Qpainter never starts, I also tried the following:

    void MainWindow::paintAll(QSvgGenerator *generator)
    {
    QPainter painter;
    if (engine)
    painter.begin(engine);
    else
    painter.begin(this);
    .......The previos code follows...
    

    But it doesnt compiles,

    Can someone help me plse??

    Thank you!


  • Lifetime Qt Champion

    Hi,

    Why are you creating that many different painters in paintAll ?



  • I want to create several objects in qpainter (show them in window), whats wrong?

    Thank you very much


  • Lifetime Qt Champion

    Well, there's no need to create all these painters. You can do everything with only one.



  • 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!


  • Lifetime Qt Champion

    Did you properly allocate engine at any point ?



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

    Thanks again


  • Lifetime Qt Champion

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



  • 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.



  • 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


  • Lifetime Qt Champion

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



  • @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!



  • 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!


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    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.



  • @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


Log in to reply