Insert image on other QImage in QLabel



  • Hi everyone, can you help me please?

    I would like display qimage on other qimage in qlabel, but i want to size of qimage not resize in qlabel, and insert qimage in special coords of qlabel example top or bot.

    thanks for helping


  • Moderators

    @kevin32 Hi! You could create a custom widget, derived from QWidget, and place two QLabels on it.



  • @Wieland thanks, i display my image like that

     // create Qpixmap from Qimage
        QPixmap pix = QPixmap::fromImage(img);
        // display the pixmap on the label
        ui->label->setPixmap(pix);
    

    it's ok, my label and qimage is already in custom widget but, how to insert special coords its possible with setpixamp?

    example:
    my label : width 800 height 800
    my first qimage : width 800 height 800
    my second qimage insert dynamically with code : x: 0 , y 50 i would like change coords and size.

    http://img11.hostingpics.net/pics/581570example.png

    Thanks for you helping :)


  • Moderators

    Changing position and size can be done with void QWidget::setGeometry(int x, int y, int w, int h).



  • @Wieland thanks you very much ```
    QPixmap px("C://Users//user//Pictures//a.png");
    ui->label->setPixmap(px);
    ui->label->setGeometry(0,0,100,100);
    if(!px.isNull())
    {
    ui->label->setPixmap(px);
    ui->label->show();
    }

    
    Sorry but finally how to resize image for exemple here image of 200:200 display in 100:100 thanks you.

  • Moderators

    You can resize the pixmap before handing it to the label with on of QPixmap's scaled() methods, e.g. QPixmap QPixmap::scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation) const

    QPixmap px("C://Users//user//Pictures//a.png");
    px = px.scaled( QSize(100,100) );
    ui->label->setPixmap(px);
    


  • I find other method with ```
    QPixmap *pixmap=new QPixmap(ui->label->width(), ui->label->height());
    QPainter *painter=new QPainter(pixmap);
    painter->drawPixmap(100, 0, 50, 50, QPixmap("C://Users//user//Pictures//a.png"));
    painter->drawPixmap(0, 0, 100, 100, QPixmap("C://Users//user//Pictures//1.png"));
    painter->end();
    ui->label->setPixmap(*pixmap);

    
    ithank you for helping

  • Lifetime Qt Champion

    Hi,

    You have two memory leaks now. There's no need to allocate pixmap nor painter on the heap.

    i.e:

    QPixmap pixmap(ui->label->width(), ui->label->height());
    QPainter painter=(&pixmap);
    painter.drawPixmap(100, 0, 50, 50, QPixmap("C:/Users/user/Pictures/a.png"));
    painter.drawPixmap(0, 0, 100, 100, QPixmap("C:/Users/user/Pictures/1.png"));
    painter.end();
    ui->label->setPixmap(pixmap);
    

Log in to reply
 

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