[Solved] Drawing a line on top of image



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


  • Moderators

    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.


  • Lifetime Qt Champion

    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



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



  • EDIT: Got it!

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

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.