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

QPixmap Problem

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 3 Posters 7.9k 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.
  • O ofmrew

    I have used QPixmap before successfully, I am stuck on this one. I am trying to create a PNG file that I can use in another application, but all it seems to produce is a black image. The code is:

        if (mapCreated) return;
        parcelMap = new QPixmap(20000, 10000);
        parcelMap->fill(Qt::white);
        QPainter pm(parcelMap);
    static const QPointF points1[5] = {
        QPointF(200, 200),
        QPointF(20000-200, 200),
        QPointF(20000-200, 5000-100),
        QPointF(200, 5000-100),
        QPointF(200, 200)};
        pm.drawPolyline(points1, 5);
    static const QPointF points2[5] = {
            QPointF(200, 5000+100),
            QPointF(10000-100, 5000+100),
            QPointF(10000-100, 5000-100),
            QPointF(200, 5000-100),
            QPointF(200, 200)};
    pm.drawPolyline(points2, 5);
    static const QPointF points3[5] = {
            QPointF(10000+100, 5000+100),
            QPointF(20000+200, 5000+100),
            QPointF(20000+200, 5000-100),
            QPointF(10000+100, 5000-100),
            QPointF(10000+100, 200)};
    pm.drawPolyline(points3, 5);
    
        QFile file("//home//XXX/Pictures//parcels.png");
        file.open(QIODevice::WriteOnly);
        parcelMap->save(&file, "PNG");
        mapCreated = true;
    

    The file is created, but the Linus Image Viewer shows it black. Any thoughts on what I am doing incorrectly?

    Second question: If I have a pixmap that is larger in extent than my widget, will the pixmap image be shrunk to fit the widget if I use drawPixmap(widget->rect, pixmap, pixmap->rect) (I believe that I will need to keep the aspect ratio)?

    O Offline
    O Offline
    ofmrew
    wrote on last edited by
    #3

    @ofmrewThanks, but let me explain: In the snippet above I only created the file, which I did because I got confusing results when I used paintEvent to the display the pixmap on the widget.

    I do not require a paintEvent when drawing on a pixmap. The statement about the aspect ration was an aside, I knew that I would need to use something like what you pointed out or do it myself as I have done in the past.

    My real two qestions are: 1. Does the drawPixmap scale the image. 2. And most important, why do I get just a black image.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Charlie_Hdz
      wrote on last edited by
      #4

      Ok, I have a program that create a .png correctly... For reference while I got the answer:

      QApplication app( argc, argv );
      
        QPixmap pixmap( 200, 200 );
        pixmap.fill( Qt::white );
      
        QPainterPath path;
      
        path.addEllipse( 80, 80, 80, 80 );
      
        path.moveTo( 120, 120 );
        path.lineTo( 120, 40 );
        path.arcTo( 40, 40, 160, 160, 90, 90 );
        path.lineTo( 120, 120 );
      
        QFont font = QApplication::font();
        font.setPixelSize( 40 );
      
        path.addText( 20, 180, font, "Path" );
      
        QPainter painter( &pixmap );
        painter.setRenderHint( QPainter::Antialiasing );
      
        painter.setPen( Qt::black );
        painter.setBrush( Qt::gray );
      
        painter.drawPath( path );
      
        pixmap.save( "path.png" );
      
        return 0;
      

      Kind Regards,
      Enrique Hernandez
      gearstech.com.mx
      chernandez@gearstech.com.mx

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

        Hi,

        You forgot to call pm.end().

        By the way since you don’t show that image you should rather use QImage as it doesn’t require a window server running.

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

        O 1 Reply Last reply
        0
        • O Offline
          O Offline
          ofmrew
          wrote on last edited by
          #6

          Let me pose my question another way. What I want to do is to draw three rectangles on to something that I can save to a file in read into another application as a background, much like you see an aerial photograph overlaid with a road network. I my case, I will be digitizing the road network using the aerial photograph as a reference. I am using QPixmap, but should I be using QImage?

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

            QPixmap is optimised for showing on screen. If you don't plan to do that then go on with QImage.

            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
            • SGaistS SGaist

              Hi,

              You forgot to call pm.end().

              By the way since you don’t show that image you should rather use QImage as it doesn’t require a window server running.

              O Offline
              O Offline
              ofmrew
              wrote on last edited by
              #8

              @SGaist I tried pm.end with no success and I converted to QImage with no success. Linux Image Viewer still show a black image. Is Image Viewer the problem or is it something I am doing?

              1 Reply Last reply
              0
              • C Charlie_Hdz

                Ok, I have a program that create a .png correctly... For reference while I got the answer:

                QApplication app( argc, argv );
                
                  QPixmap pixmap( 200, 200 );
                  pixmap.fill( Qt::white );
                
                  QPainterPath path;
                
                  path.addEllipse( 80, 80, 80, 80 );
                
                  path.moveTo( 120, 120 );
                  path.lineTo( 120, 40 );
                  path.arcTo( 40, 40, 160, 160, 90, 90 );
                  path.lineTo( 120, 120 );
                
                  QFont font = QApplication::font();
                  font.setPixelSize( 40 );
                
                  path.addText( 20, 180, font, "Path" );
                
                  QPainter painter( &pixmap );
                  painter.setRenderHint( QPainter::Antialiasing );
                
                  painter.setPen( Qt::black );
                  painter.setBrush( Qt::gray );
                
                  painter.drawPath( path );
                
                  pixmap.save( "path.png" );
                
                  return 0;
                
                O Offline
                O Offline
                ofmrew
                wrote on last edited by
                #9

                @Charlie_Hdz I had to convert your program into a console application, but I got nothing. How did you creat yours, I did mine with QtCreator.
                Here is mine:
                #include <QCoreApplication>
                #include <QPainter>
                #include <QPainterPath>
                #include <QFont>
                #include <QPixmap>

                int main(int argc, char *argv[])
                {
                QCoreApplication a(argc, argv);;

                  QPixmap pixmap( 200, 200 );
                  pixmap.fill( Qt::white );
                
                  QPainterPath path;
                
                  path.addEllipse( 80, 80, 80, 80 );
                
                  path.moveTo( 120, 120 );
                  path.lineTo( 120, 40 );
                  path.arcTo( 40, 40, 160, 160, 90, 90 );
                  path.lineTo( 120, 120 );
                

                // QFont font = QCoreApplication::font();
                // font.setPixelSize( 40 );

                // path.addText( 20, 180, font, "Path" );

                  QPainter painter( &pixmap );
                  painter.setRenderHint( QPainter::Antialiasing );
                
                  painter.setPen( Qt::black );
                  painter.setBrush( Qt::gray );
                
                  painter.drawPath( path );
                
                  pixmap.save( "//home//bobwilliams//Pictures//path.png" );
                
                return a.exec();
                

                }

                C 1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Charlie_Hdz
                  wrote on last edited by
                  #10

                  I did a Qt Widget Application. I commented as you did and it worked. Check whether your path is correct.

                  Kind Regards,
                  Enrique Hernandez
                  gearstech.com.mx
                  chernandez@gearstech.com.mx

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    ofmrew
                    wrote on last edited by
                    #11

                    @Charlie_Hdz I change to what created the parcels.png file and got nothing. I do not see what is wrong it is very simple. I will try reducing the size and try with both QPixmap and QImage, then I will post the results.

                    In what folder did the file from pixmap.save( "path.png" ); end up.

                    C 1 Reply Last reply
                    0
                    • O ofmrew

                      @Charlie_Hdz I change to what created the parcels.png file and got nothing. I do not see what is wrong it is very simple. I will try reducing the size and try with both QPixmap and QImage, then I will post the results.

                      In what folder did the file from pixmap.save( "path.png" ); end up.

                      C Offline
                      C Offline
                      Charlie_Hdz
                      wrote on last edited by
                      #12

                      @ofmrew said in QPixmap Problem:

                      In what folder did the file from pixmap.save( "path.png" ); end up.

                      Depends, it can be saved at Home(root directory) or in the project output folder. I tried in Ubuntu.

                      Kind Regards,
                      Enrique Hernandez
                      gearstech.com.mx
                      chernandez@gearstech.com.mx

                      O 1 Reply Last reply
                      0
                      • O ofmrew

                        @Charlie_Hdz I had to convert your program into a console application, but I got nothing. How did you creat yours, I did mine with QtCreator.
                        Here is mine:
                        #include <QCoreApplication>
                        #include <QPainter>
                        #include <QPainterPath>
                        #include <QFont>
                        #include <QPixmap>

                        int main(int argc, char *argv[])
                        {
                        QCoreApplication a(argc, argv);;

                          QPixmap pixmap( 200, 200 );
                          pixmap.fill( Qt::white );
                        
                          QPainterPath path;
                        
                          path.addEllipse( 80, 80, 80, 80 );
                        
                          path.moveTo( 120, 120 );
                          path.lineTo( 120, 40 );
                          path.arcTo( 40, 40, 160, 160, 90, 90 );
                          path.lineTo( 120, 120 );
                        

                        // QFont font = QCoreApplication::font();
                        // font.setPixelSize( 40 );

                        // path.addText( 20, 180, font, "Path" );

                          QPainter painter( &pixmap );
                          painter.setRenderHint( QPainter::Antialiasing );
                        
                          painter.setPen( Qt::black );
                          painter.setBrush( Qt::gray );
                        
                          painter.drawPath( path );
                        
                          pixmap.save( "//home//bobwilliams//Pictures//path.png" );
                        
                        return a.exec();
                        

                        }

                        C Offline
                        C Offline
                        Charlie_Hdz
                        wrote on last edited by
                        #13

                        @ofmrew said in QPixmap Problem:

                        I had to convert your program into a console application

                        So, hadn't have an error when QApplication header was not detected? In the console application, GUI is removed... So it causes errors.

                        Create a Qt Widget Application, then insert the code and compile. It should work in that way!

                        Kind Regards,
                        Enrique Hernandez
                        gearstech.com.mx
                        chernandez@gearstech.com.mx

                        1 Reply Last reply
                        0
                        • C Charlie_Hdz

                          @ofmrew said in QPixmap Problem:

                          In what folder did the file from pixmap.save( "path.png" ); end up.

                          Depends, it can be saved at Home(root directory) or in the project output folder. I tried in Ubuntu.

                          O Offline
                          O Offline
                          ofmrew
                          wrote on last edited by
                          #14

                          @Charlie_Hdz

                          What I meant was when you executed the code you posted, in what folder did you find the path.png file?

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

                            This one is working fine. I get a white image with several rectangles on it

                            #include <QCoreApplication>
                            #include <QFile>
                            #include <QImage>
                            #include <QPainter>
                            #include <QPointF>
                            #include <QtDebug>
                            
                            int main(int argc, char **argv)
                            {
                                QCoreApplication app(argc, argv);
                                QImage parcelMap(20000, 10000, QImage::Format_ARGB32);
                                parcelMap.fill(Qt::white);
                                QPainter pm(&parcelMap);
                                static const QPointF points1[5] = {
                                QPointF(200, 200),
                                QPointF(20000-200, 200),
                                QPointF(20000-200, 5000-100),
                                QPointF(200, 5000-100),
                                QPointF(200, 200)};
                                pm.drawPolyline(points1, 5);
                                static const QPointF points2[5] = {
                                    QPointF(200, 5000+100),
                                    QPointF(10000-100, 5000+100),
                                    QPointF(10000-100, 5000-100),
                                    QPointF(200, 5000-100),
                                    QPointF(200, 200)};
                                pm.drawPolyline(points2, 5);
                                static const QPointF points3[5] = {
                                    QPointF(10000+100, 5000+100),
                                    QPointF(20000+200, 5000+100),
                                    QPointF(20000+200, 5000-100),
                                    QPointF(10000+100, 5000-100),
                                    QPointF(10000+100, 200)};
                                pm.drawPolyline(points3, 5);
                                pm.end();
                            
                                QFile file("parcels.png");
                                if (!file.open(QIODevice::WriteOnly)) {
                                    qDebug() << "Failed to open file" << file.errorString();
                                }
                                parcelMap.save(&file, "PNG");
                                return 0;
                            

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

                            O 1 Reply Last reply
                            1
                            • O ofmrew

                              @Charlie_Hdz

                              What I meant was when you executed the code you posted, in what folder did you find the path.png file?

                              C Offline
                              C Offline
                              Charlie_Hdz
                              wrote on last edited by
                              #16

                              @ofmrew

                              You define the output directory, in Project, Run Working Directory.

                              In this image, you can search for the "Working DIrectory"

                              0_1515184782782_Screenshot from 2018-01-05 14-38-48.png

                              Kind Regards,
                              Enrique Hernandez
                              gearstech.com.mx
                              chernandez@gearstech.com.mx

                              1 Reply Last reply
                              0
                              • SGaistS SGaist

                                This one is working fine. I get a white image with several rectangles on it

                                #include <QCoreApplication>
                                #include <QFile>
                                #include <QImage>
                                #include <QPainter>
                                #include <QPointF>
                                #include <QtDebug>
                                
                                int main(int argc, char **argv)
                                {
                                    QCoreApplication app(argc, argv);
                                    QImage parcelMap(20000, 10000, QImage::Format_ARGB32);
                                    parcelMap.fill(Qt::white);
                                    QPainter pm(&parcelMap);
                                    static const QPointF points1[5] = {
                                    QPointF(200, 200),
                                    QPointF(20000-200, 200),
                                    QPointF(20000-200, 5000-100),
                                    QPointF(200, 5000-100),
                                    QPointF(200, 200)};
                                    pm.drawPolyline(points1, 5);
                                    static const QPointF points2[5] = {
                                        QPointF(200, 5000+100),
                                        QPointF(10000-100, 5000+100),
                                        QPointF(10000-100, 5000-100),
                                        QPointF(200, 5000-100),
                                        QPointF(200, 200)};
                                    pm.drawPolyline(points2, 5);
                                    static const QPointF points3[5] = {
                                        QPointF(10000+100, 5000+100),
                                        QPointF(20000+200, 5000+100),
                                        QPointF(20000+200, 5000-100),
                                        QPointF(10000+100, 5000-100),
                                        QPointF(10000+100, 200)};
                                    pm.drawPolyline(points3, 5);
                                    pm.end();
                                
                                    QFile file("parcels.png");
                                    if (!file.open(QIODevice::WriteOnly)) {
                                        qDebug() << "Failed to open file" << file.errorString();
                                    }
                                    parcelMap.save(&file, "PNG");
                                    return 0;
                                
                                O Offline
                                O Offline
                                ofmrew
                                wrote on last edited by
                                #17

                                @SGaist Ran it and got an image of thousands of small squares. I have a screen shot, but I so not know how to post it to this message; how do I do it?

                                Also, I moved the project to windows and got a blank white image!

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

                                  There's an upload image button just above the editor, third from the right.

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

                                  O 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    There's an upload image button just above the editor, third from the right.

                                    O Offline
                                    O Offline
                                    ofmrew
                                    wrote on last edited by
                                    #19

                                    @SGaist I have narrowed the problem to my Mint Linux computer because I moved the one above to a Win 10 computer and I got a white rectangle with just visible rectangles. I moved the .png file to the Win 10 machine and, guess what, it showed a white rectangle with just visible rectangles. So the problem appears to be Image Viewer. Which viewer do you use on Linux computers.

                                    As for the icons on the editor, to tell the truth, I never really noticed them. Is the some documentation on their meaning and how to use them. Most are obvious, but link, picture, zen mode and the one on the end, require more explanation.

                                    Seems we spent all this time running to ground a non-problem with Qt, while the real culprit was software provided by the operating system; however, just for grins I tried GIMP Image Viewer and of course it showed the image correctly. Warning to Mint Linux users, Image Viewer is not to be trusted.

                                    Thanks to all for the help.

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

                                      It depends on the desktop environment so there are several possible like Gwenview, Okular, etc.

                                      Zen mode simply puts the editor in full screen so you don't have any distraction and the last one on the right allows to easily insert code sample.

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

                                      O 1 Reply Last reply
                                      1
                                      • SGaistS SGaist

                                        It depends on the desktop environment so there are several possible like Gwenview, Okular, etc.

                                        Zen mode simply puts the editor in full screen so you don't have any distraction and the last one on the right allows to easily insert code sample.

                                        O Offline
                                        O Offline
                                        ofmrew
                                        wrote on last edited by
                                        #21

                                        @SGaist Thanks. That really helps; now I can start correctly adding code and images to my posts.

                                        1 Reply Last reply
                                        1

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved