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] Drawing a line on top of image
Forum Updated to NodeBB v4.3 + New Features

[Solved] Drawing a line on top of image

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

    Hello. I have a frame inside the MainWindow. Within this frame, I have a label with an image added to it. I want to draw a line that would overlay the image so that both are seen.

    I am trying to implement the following:

    [code]
    void MainWindow::on_pushButton_2_clicked()
    {
    QPixmap pixmap(650,580);
    pixmap.fill(QColor("transparent"));

       QPainter painter(&pixmap);
       painter.drawLine(0, 0, 480, 480);
    
       ui->label->setPixmap(pixmap);
    

    }

    [/code]

    However, the image in the label disappears and instead a line appears. Not what I want.

    OK, I understand, I may be replacing my label with the Pixmap transparent background. Then I also tried to do this:

    [code]
    void MainWindow::on_pushButton_2_clicked()
    {
    QPixmap pixmap("pic.png");

       QPainter painter(&pixmap);
       painter.drawLine(0, 0, 480, 480);
    
       ui->label->setPixmap(pixmap);
    

    }
    [/code]

    No image, no line, nothing.

    Thanks for your help!

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Ihe above code should work i guess.
      Is the file "pic.png" present ?
      Try saving the pixmap to a file using "QPixmap::save":http://qt-project.org/doc/qt-5.0/qtgui/qpixmap.html#save and check if that image file contains the line.

      157

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

        Hi,

        To add to p3c0, since "pic.png" is a relative path, the file should be in the same folder as your application executable which probably is in the shadow build otherwise you have to give the full path to it or use Qt's resource system to embed the file with your application

        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
        • I Offline
          I Offline
          igorland
          wrote on last edited by
          #4

          Hello. Thanks. The png file is in the same folder as the exe. Actually, I am trying both variants: one png file is in the exe folder, another file is in the Resource system. Neither works. I have a blank window. Pixmap is saved as "myfile.png" although with no content and 0 KB size. (Is this how it is supposed to work?)

          [code]
          void MainWindow::on_pushButton_2_clicked()
          {

          QPixmap pixmap("pic1.png");
          QFile file("myfile.png");
          file.open(QIODevice::WriteOnly);
          pixmap.save(&file, "PNG");
          
          QPainter painter(&pixmap);
          painter.drawLine(0, 0, 480, 480);
          
          ui->label->setPixmap(pixmap);
          

          }
          [/code]

          Must be something with the code...

          1 Reply Last reply
          0
          • I Offline
            I Offline
            igorland
            wrote on last edited by
            #5

            EDIT: Got it!

            [code]
            QPixmap pixmap(":/new/pic1.png");
            [/code]

            Silly me! Thank you very much, guys! Much appreciated!

            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