QPainter does not work
-
Simple example:
// widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); // QWidget interface protected: void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void paintEvent(QPaintEvent *event); private: QImage mBackground; QPointF mFrom; QPointF mTo; }; #endif // WIDGET_H
// widget.cpp #include "widget.h" #include <QPaintEvent> #include <QPainter> Widget::Widget(QWidget *parent) : QWidget(parent) { mBackground.load("/tmp/paint/background.jpeg"); } Widget::~Widget() { } void Widget::mousePressEvent(QMouseEvent *event) { mFrom = event->position(); mTo = mFrom; } void Widget::mouseReleaseEvent(QMouseEvent *event) { mTo = event->position(); update(); } void Widget::paintEvent(QPaintEvent *event) { QPainter p(this); p.drawImage(event->rect(), mBackground); if (mFrom != mTo) { QPen pen(Qt::red); pen.setWidth(3); p.setPen(pen); p.drawLine(mFrom, mTo); } p.end(); }
-
@ChrisW67 said in QPainter does not work:
void Widget::paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.drawImage(event->rect(), mBackground);
if (mFrom != mTo) {
QPen pen(Qt::red);
pen.setWidth(3);
p.setPen(pen);
p.drawLine(mFrom, mTo);
}
p.end();
}I will try, Thank you :)
@ChrisW67 In my code, QImage is constantly refreshing and that is delete my lines.
-
@Joe-von-Habsburg said in QPainter does not work:
QImage is constantly refreshing and that is delete my lines
What does this mean?
-
@Joe-von-Habsburg
If the image changes, for whatever reason, you have to redraw the lines when you redraw the background. Which yourpaintEvent()
seems to do. So unless you are changing the image somewhere else I don't see how you get a new image but no lines. -
@Joe-von-Habsburg said in QPainter does not work:
@ChrisW67 In my code, QImage is constantly refreshing and that is delete my lines.
Then you are saying that mBackground in my example is changing. When you change it, call update().
-
@jsulm said in QPainter does not work:
What does this mean?
I take data each 100ms maybe, and I draw QImage with that data. and my QIMage refreshing always
@JonB said in QPainter does not work:
you have to redraw the lines when you redraw the background.
I draw but it likes be a gif. I want to see a const line not a gif.
-
@Joe-von-Habsburg
I don't know what you are saying.The only code of yours I see which draws an image is the
drawImage()
in yourpaintEvent()
, and that also draws the line on top of it. So I don't see where you are changing the image without drawing the line.If you have different code from what you have shown we cannot guess.
-
@JonB said in QPainter does not work:
The only code of yours I see which draws an image is the drawImage() in your paintEvent(), and that also draws the line on top of it. So I don't see where you are changing the image without drawing the line.
If you have different code from what you have shown we cannot guess.MyClass (){ widget= new Widget(containerImage->parentWidget()); widget->setFixedSize(containerImage->parentWidget()->size()); _image = new QImage(_containerImage->width(), _containerImage->height(), QImage::Format_RGB32); } void draw(...) { //_containerImage is a QLabel for(int x = 0; x < _image->width(); x++){ for(int y = _image->height() - 1; y > 0; y--){ _image->setPixel(x, y , _image->pixel(x, y-1)); } } double step = static_cast<double>(sample) / (_containerImage->width()); QList<double> list; for(int i = 0; i < _containerImage->width(); i++){ list.append(step * i); } for(int i = 0; i < list.size(); i++){ int ii = round(list.at(i)); _image->setPixel(i, 0, getColor(ssd.samples[ii + startPoint].toDouble()).rgb()); } _containerImage->setPixmap(QPixmap::fromImage(*_image)); emit setPoints(_first, _last); }
-
@Joe-von-Habsburg
I have no idea what_containerImage
is or how this has any relationship at all to yourWidget::paintEvent()
, when that is called, or whatmBackground
is compared to_containerImage
....If you want help with code you have you have to actually show it. Revealing extra bits of code you have over time does not help.
-
@Joe-von-Habsburg My example recast with a changing background:
// widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); // QWidget interface protected: void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void paintEvent(QPaintEvent *event); private slots: void setBackground(); private: QImage mBackground; QPointF mFrom; QPointF mTo; }; #endif // WIDGET_H
// widget.cpp #include "widget.h" #include <QPaintEvent> #include <QPainter> #include <QLinearGradient> #include <QRandomGenerator> #include <QTimer> #include <QDebug> Widget::Widget(QWidget *parent) : QWidget(parent) , mBackground(500, 500, QImage::Format_RGB32) { setBackground(); QTimer *t = new QTimer(this); connect(t, &QTimer::timeout, this, &Widget::setBackground); t->start(1000); } Widget::~Widget() { } void Widget::mousePressEvent(QMouseEvent *event) { mFrom = event->position(); mTo = mFrom; } void Widget::mouseReleaseEvent(QMouseEvent *event) { mTo = event->position(); update(); } void Widget::paintEvent(QPaintEvent *event) { QPainter p(this); p.drawImage(event->rect(), mBackground); if (mFrom != mTo) { QPen pen(Qt::red); pen.setWidth(3); p.setPen(pen); p.drawLine(mFrom, mTo); } p.end(); } void Widget::setBackground() { QPainter p(&mBackground); const QRectF rectf(mBackground.rect()); QLinearGradient grad(rectf.topLeft(), rectf.bottomRight()); grad.setColorAt(0, QColor::fromRgb(QRandomGenerator::global()->generate())); grad.setColorAt(1, QColor::fromRgb(QRandomGenerator::global()->generate())); p.fillRect(mBackground.rect(), QBrush(grad)); p.end(); update(); }