Important: Please read the Qt Code of Conduct -

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

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

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

    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.

    Thanks for you helping :)

  • 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");

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

  • 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) );

  • 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"));

    ithank you for helping

  • Lifetime Qt Champion


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


    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"));

Log in to reply