Y
Y
Yapparov_Ilyas2017-01-04 11:39:47
Qt
Yapparov_Ilyas, 2017-01-04 11:39:47

How to draw on QPixmap?

Hello everyone. There is a class that inherits from QLabel. It is necessary to install a picture from 2 QPixmap into it. I decided to create a third QPixmap and redraw the image in paintEvent

void MainWindow::paintEvent(QPaintEvent*)
{
    QPixmap *pixmap=new QPixmap(this->size().width(), this->size().height());
    QPainter *painter=new QPainter(pixmap);
    painter->drawPixmap(0, 0, this->size().width(), this->size().height(), QPixmap(":/rectB.png"));
    painter->drawPixmap(0, 0, this->size().width(), this->size().height(), QPixmap(":/CheckFF.png"));
    painter->end();
    delete painter;

    pix= *pixmap;    //pix- private поле класса
    setPixmap(pix);
    delete pixmap;
}

But for some reason, nothing is drawn in the widget. I tried to redefine resizeEvent by inserting repaint() and update() into it, but the situation is the same.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2017-01-05
@Mercury13

your mistakes.
1. QPixmap is itself a shared ownership pointer; if you need to create it for a short time - do it on the stack, no new!
1.1. So is QPainter, only it's a sole proprietorship pointer. By the way, this once played a cruel joke with one of my programs when it ate all the memory.
2. Why do we need Pixmap generation in the form redrawing procedure? If your descendant draws a picture correctly on its own, generate it on the “something has changed” event.
Further, if there can be a finite but sufficiently large number of pictures (for example, thousands), you can set up a cache where the last twenty are stored prepared, for example. If there are few pictures (up to a hundred), it is better to prepare them all when loading the program.

S
Stanislav Makarov, 2017-01-04
@Nipheris

Lord, what are you doing?
What is this anyway? What for?
So do you want to label the QPixmap once or redraw it yourself each time? If redrawing, then why create QPixmap on the heap and also reassign it to the field?
Resizing something to do with it? Are you trying to solve the problem at any cost?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question