How to Draw a point in a QPixmap Image? - c++

I have a project where i want to draw a point into a image inside a QPixmap. The point would be draw with the mouse click on the QLabel. I created a eventFilter() which corresponds to mouse click. When I click with the mouse, these eventFilter is called and draw a point in the image, but my code doesn't works. I tried many others options like subclassing the QLabel, but didn't work either.
And sometimes my compiler shows these error messages:
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setPen: Painter not active
QPainter::drawPoints: Painter not active
QPainter::end: Painter not active, aborted
but I don't understand, because the Qt documentation says that is allowed use the QPainter outside of paintEvent just using with QPixmap.
Below is my code with the method that starts the QPainter.
bool mainwindow::eventFilter(QObject* watched, QEvent* event) {
if ( watched != ui->labelScreen )
return false;
if ( event->type() != QEvent::MouseButtonPress )
return false;
const QMouseEvent* const me = static_cast<const QMouseEvent*>( event );
//might want to check the buttons here
const QPoint p = me->pos(); //...or ->globalPos();
ui->label_Xget->setNum(this->xReal);
ui->label_Yget->setNum(this->yReal);
///////////////////////////////////
QPixmap pix;
pix.fromImage(QImage::fromData("C:/Users/Syn/Pictures/imagem137.jpg"));
QPainter *painter = new QPainter(&pix);
painter->setPen(Qt::red);
painter->drawPoint(p.x(), p.y());
ui->labelScreen->setPixmap(pix);
painter->end();
///////////////////////////////////
return false;
}
Someone would can help me solve this problem? Thanks.

The error messages are not from your compiler, they happen at runtime, and you can't be sure whether the code you quote above is their cause.
There are several problem:
QPixmap::fromImage is a static method that returns a pixmap. You're ignoring its return value. The correct way to use it would be:
// C++11
auto pix = QPixmap::fromImage(QImage{"filename"});
// C++98
QPixmap pix(QPixmap::fromImage(QImage("filename")));
You can pass the filename directly to QPixmap constructor:
// C++11
QPixmap pix{"filename"};
// C++98
QPixmap pix("filename");
You're leaking the painter instance. You should avoid any dynamic memory allocation unless you truly need it. Also, store ui by value - it's cheaper that way.
It is a very bad idea to do any blocking I/O, such as reading a file, in event handlers. Preload and store the pixmap as a member of the MainWindow class.
Thus (in C++11):
Interface
template <class F>
class MainWindow : public QMainWindow {
QPixmap m_clickPixmap;
Ui::MainWindow ui;
bool eventFilter(QObject*, QEvent*) override;
public:
MainWindow(QWidget * parent = nullptr);
};
Implementation
void loadImage(const QString & fileName, QObject * context, F && functor) {
QtConcurrent::run([=]{
QImage img{fileName};
if (img.isNull()) return;
QTimer::singleShot(0, context, functor, img);
});
}
MainWindow::MainWindow(QWidget * parent) :
QMainWindow{parent}
{
loadImage("C:/Users/Syn/Pictures/imagem137.jpg", this, [this](const QImage & img){
m_clickPixmap = QPixmap::fromImage(img);
});
ui.setupUi(this);
}
bool MainWindow::eventFilter(QObject* watched, QEvent* event) {
if ( watched != ui.labelScreen )
return false;
if ( event->type() != QEvent::MouseButtonPress )
return false;
auto const me = static_cast<const QMouseEvent*>(event);
auto const p = me->pos(); //...or ->globalPos();
ui.label_Xget->setNum(this->xReal);
ui.label_Yget->setNum(this->yReal);
auto pix{m_clickPixmap};
QPainter painter(&pix);
painter.setPen(Qt::red);
painter.drawPoint(p.x(), p.y());
painter.end(); // probably not needed
ui.labelScreen->setPixmap(pix);
return false;
}

Related

Change circle colour every 5 seconds

I try to create a green circle which every 5 seconds disappears.
Actually, I have the green circle created with the QPainter method. I tried QTimer and others methods but I can't find the good solution.
I overrided the paintEvent function like this :
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
Qt::BrushStyle style = Qt::SolidPattern;
QBrush brush(Qt::green, style);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(brush);
painter.drawEllipse(525, 5, 50, 50);
}
MainWindow::MainWindow() : QWidget()
{
QTimer *ledtimer = new QTimer(this);
connect(ledtimer, SIGNAL(timeout()), this, SLOT(run_led()));
ledtimer->start(5000);
}
I tried to do something like this, but when i'm using run_led, it tells that painter is already removed (i tried in MainWindow class).
I understand the signal function and the timer, I used it in another files, so some tips would be appreciated. Am I supposed to use timers to make circles wink ?
Define a flag boolean that changes every 5 seconds and in paint use a brush as global variable
void MainWindow::paintEvent(QPaintEvent *)
{
....
QBrush brush(myBrush, style);
...
}
and in slot (run_led)
void MainWindow::run_led()
{
c != true;
if(c)
{
myBrush=Qt::green;
}
else
{
myBrush=Qt::gray;
}
}
Assuming your MainWindowinherits QMainWindow
MainWindow::paintEvent(QPaintEvent *) is a function that tells the systems to render your window.
So I let you guess what goes wrong when you override it like this.
But you can put the drawing in a QWidget made for this : QGraphicsView which displays the content of QGraphicsScene .
You should create a slot to do what you want, like this :
void MainWindow::on_led_timer_timeout(){
/*
Do stuff the the QGraphicsScene or QGraphicsView
*/
}
And then connect the correct signal of your QTimer to it :
connect(ledtimer, &QTimer::timeout, this, &MainWindow::on_led_timer_timeout);
class QSimpleLed : public QWidget
{
Q_OBJECT
Q_PROPERTY(QColor color READ color WRITE setColor)
public:
using QWidget::QWidget;
void setColor(const QColor& c) {
if (m_color != m) {
m_color = m;
update();
}
}
QColor color() const;
void paintEvent(QPaintEvent *) override;
private:
QColor m_color;
}
Implementation above should be obvious.
int main(int argc, char* argv[])
{
QApplication app{argc, argv};
QSimpleLed led;
auto animation = new QPropertyAnimation(&led, "color");
animation->setStartValue(Qt::red);
animation->setEndValue(Qt::green);
animation->setLoopCount(-1);
animation->setDuration(5000);
animation->start();
led.show();
return app.exec();
}

Error with drawing point on QLabel

I'm trying to draw on QLabel in Qt like this:
paintscene.h:
class PaintScene : public QWidget
{
Q_OBJECT
public:
PaintScene(QWidget* parent = NULL);
QVector<QLabel*> _layers;
QColor _color;
int _width;
void mousePressEvent(QMouseEvent* event);
private slots:
void updateWidth();
};
paintscene.cpp:
PaintScene::PaintScene(QWidget* parent) : QWidget(parent)
{
_width = 10;
_color = Qt::red;
QLabel* inital = new QLabel(this);
inital->setStyleSheet("QLabel { background-color : white; }");
_layers.push_back(inital);
QGridLayout* layout = new QGridLayout();
layout->addWidget(inital, 1, 1, 1, 1);
this->setLayout(layout);
}
void PaintScene::mousePressEvent(QMouseEvent *event)
{
QImage tmp = _layers.back()->pixmap()->toImage();
QPainter painter(&tmp);
QPen paintpen(_color);
paintpen.setWidth(_width);
painter.setPen(paintpen);
painter.drawPoint(event->x(), event->y());
_layers.back()->setPixmap(QPixmap::fromImage(tmp));
}
The list is needed because I want to implement the work with layers (QLabel - a separate layer).
However, I get an error, the program terminates. The error occurs on the line QImage tmp = _layers.back()->pixmap()->toImage();.
What makes this happen? How can this be fixed? Maybe for a layer to use something different, not QLabel?
#Jeremy Friesner is right about the reason for the error, not having a QPixmap this will be null, in my answer I will show a possible solution
void PaintScene::mousePressEvent(QMouseEvent *event)
{
QLabel *label = _layers.back();
const QPixmap *pix= label->pixmap();
QPixmap pixmap;
if(pix)
pixmap = *pix;
else{
pixmap = QPixmap(label->size());
pixmap.fill(Qt::transparent);
}
QPainter painter(&pixmap);
QPen paintpen(_color);
paintpen.setWidth(_width);
painter.setPen(paintpen);
painter.drawPoint(event->pos());
painter.end();
label->setPixmap(pixmap);
}
From the Qt docs for QLabel::pixmap():
This property holds the label's pixmap
If no pixmap has been set this will return 0.
... so when you do this:
QImage tmp = _layers.back()->pixmap()->toImage();
pixmap() is returning NULL (because the QLabel has never had any QPixmap set on it yet), and then you try to dereference that NULL pointer to call toImage() on it, hence the crash.
To avoid crashing, don't try to create a QImage from a NULL QPixmap pointer.
I suspect you wanted to be calling grab() instead of pixmap() -- grab() will create a QPixmap for you that contains the visual appearance of the QLabel. However, an even better approach would be to avoid messing about with QPixmaps at all; instead, make your own subclass of the QLabel class, and override its paintEvent(QPaintEvent *) method to first call up to QLabel::paintEvent(e) and then use a QPainter to draw the additional point afterwards. That will be easier to implement and also more efficient at runtime.

How to draw a line on a child widget instead of the parent widget?

I'm trying to draw a line (and some other primitives) on a QWidget. That QWidget is physically "on top of" another widget with a picture in it. I want to draw lines and circles over a picture.
I know how to draw a line already. I can do that with this code:
bool MySpecialWidget::eventFilter( QObject* watched, QEvent* event )
{
if (watched == this && event->type() == QEvent::Paint)
{
QPainter painter(this);
painter.translate(50, 50);
painter.setPen(QPen(Qt::blue, 12));
painter.setBrush(Qt::BrushStyle::SolidPattern);
painter.drawLine(0, 0, 200, 200);
}
return false;
}
But what I really want to do is position a widget to hold a picture, then position a widget over it to hold the lines. Like this:
MySpecialWidget::MySpecialWidget(QWidget *parent) : QWidget(parent)
{
QRect position = QRect(30, 50, 600, 600);
pictureBox = new QLabel(parent);
pictureBox->setGeometry(position);
pictureBox->setPixmap(QPixmap(QString::fromUtf8(":/main/graphics/MyPicture.png")));
pictureBox->setScaledContents(true);
drawnElements = new QWidget(parent);
drawnElements->setGeometry(position);
drawnElements->raise();
this->installEventFilter(this);
}
Then to draw the lines and primitives, I want to do it like this:
bool MySpecialWidget::eventFilter( QObject* watched, QEvent* event )
{
if (watched == this && event->type() == QEvent::Paint)
{
QPainter painter(drawnElements);
painter.translate(50, 50);
painter.setPen(QPen(Qt::blue, 12));
painter.setBrush(Qt::BrushStyle::SolidPattern);
painter.drawLine(0, 0, 200, 200);
}
return false;
}
But this doesn't work. Nothing is drawn. Blank.
The problem is the line that reads QPainter painter(drawnElements);
If I say QPainter painter(this);, it draws something, but it's not on the child widget, it's on the parent widget.
The documentation is pretty clear on the subject:
Each widget performs all painting operations from within its
paintEvent() function. This is called whenever the widget needs to be
redrawn, either as a result of some external change or when requested
by the application.
You should only draw on a widget from its paint event, and you should only draw on that particular widget.
The problem is the line that reads QPainter painter(drawnElements);
Of course. You need to be watching the target widget instead and catch its paint event. To control the order of drawing, you should explicitly deliver the event to the target as well.
bool MySpecialWidget::eventFilter( QObject* watched, QEvent* event )
{
// *** vvvvvvvvvvvvv *** (A)
if (watched == drawnElements && event->type() == QEvent::Paint)
{
// Let the target draw itself first.
watched->event(event);
// Then overlay our content on it.
// *** vvvvvvvvvvvvv *** (B) - must match (A)!
QPainter painter(drawnElements);
painter.translate(50, 50);
painter.setPen(QPen(Qt::blue, 12));
painter.setBrush(Qt::BrushStyle::SolidPattern);
painter.drawLine(0, 0, 200, 200);
return true; // The event is already handled.
}
return false;
}
MySpecialWidget::MySpecialWidget(QWidget *parent) : QWidget(parent)
{
//...
drawnElements->installEventFilter(this);
}
Feel free to also consider using an overlay widget.

Qt Overriding QLabel PaintEvent

I've been struggling with this problem for the past couple of days. I want to be able to grow and shrink whatever the assigned Pixmap is in a QLabel as the user resizes the window. The issue is preserving the aspect ratio and image quality. Another user on here suggested that I reimplement the paint event for the label - but I'm still very lost. I'm not even sure if I have overridden the paintEvent correctly. I would kill for a bit of sample code here.
This is where I'm at:
void MyLabel::paintEvent(QPaintEvent * event)
{
//if this widget is assigned a pixmap
//paint that pixmap at the size of the parent, aspect ratio preserved
//otherwise, nothing
}
Here is a possible implementation of a QLabel subclass that scales its pixmap content while keeping its aspect ratio. It is implemented based on the way QLabel::paintEvent is implemented.
If you are using it in a layout you can also set its size policy to QSizePolicy::Expanding, so that additional space in the layout is taken up by the QLabel for a larger display of the pixmap content.
#include <QApplication>
#include <QtWidgets>
class PixmapLabel : public QLabel{
public:
explicit PixmapLabel(QWidget* parent=nullptr):QLabel(parent){
//By default, this class scales the pixmap according to the label's size
setScaledContents(true);
}
~PixmapLabel(){}
protected:
void paintEvent(QPaintEvent* event);
private:
//QImage to cache the pixmap()
//to avoid constructing a new QImage on every scale operation
QImage cachedImage;
//used to cache the last scaled pixmap
//to avoid calling scale again when the size is still at the same
QPixmap scaledPixmap;
//key for the currently cached QImage and QPixmap
//used to make sure the label was not set to another QPixmap
qint64 cacheKey{0};
};
//based on the implementation of QLabel::paintEvent
void PixmapLabel::paintEvent(QPaintEvent *event){
//if this is assigned to a pixmap
if(pixmap() && !pixmap()->isNull()){
QStyle* style= PixmapLabel::style();
QPainter painter(this);
drawFrame(&painter);
QRect cr = contentsRect();
cr.adjust(margin(), margin(), -margin(), -margin());
int align= QStyle::visualAlignment(layoutDirection(), alignment());
QPixmap pix;
if(hasScaledContents()){ //if scaling is enabled
QSize scaledSize= cr.size() * devicePixelRatioF();
//if scaledPixmap is invalid
if(scaledPixmap.isNull() || scaledPixmap.size()!=scaledSize
|| pixmap()->cacheKey()!=cacheKey){
//if cachedImage is also invalid
if(pixmap()->cacheKey() != cacheKey){
//reconstruct cachedImage
cachedImage= pixmap()->toImage();
}
QImage scaledImage= cachedImage.scaled(
scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
scaledPixmap= QPixmap::fromImage(scaledImage);
scaledPixmap.setDevicePixelRatio(devicePixelRatioF());
}
pix= scaledPixmap;
} else { // no scaling, Just use pixmap()
pix= *pixmap();
}
QStyleOption opt;
opt.initFrom(this);
if(!isEnabled())
pix= style->generatedIconPixmap(QIcon::Disabled, pix, &opt);
style->drawItemPixmap(&painter, cr, align, pix);
} else { //otherwise (if the label is not assigned to a pixmap)
//call base paintEvent
QLabel::paintEvent(event);
}
}
//DEMO program
QPixmap generatePixmap(QSize size) {
QPixmap pixmap(size);
pixmap.fill(Qt::white);
QPainter p(&pixmap);
p.setRenderHint(QPainter::Antialiasing);
p.setPen(QPen(Qt::black, 10));
p.drawEllipse(pixmap.rect());
p.setPen(QPen(Qt::red, 2));
p.drawLine(pixmap.rect().topLeft(), pixmap.rect().bottomRight());
p.drawLine(pixmap.rect().topRight(), pixmap.rect().bottomLeft());
return pixmap;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap= generatePixmap(QSize(1280, 960));
PixmapLabel label;
label.setPixmap(pixmap);
label.setAlignment(Qt::AlignCenter);
label.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
label.setMinimumSize(320, 240);
label.show();
return a.exec();
}
I think this is better than the solution in this answer, as the QLabel here takes care of resizing its pixmap. So, there is no need to resize it manually every time the parent widget is resized and everytime a new pixmap is set on it.
First of all I wanted to say thanks to Mike.
Additionally I would like to add to his answer from 21.10.2016 - however I am not able to add a comment as of yet - thus here is a full fledged answer. [If anyone is able to move this to the comment section, feel free.]
I also added an overwrite of the other constructor of QLabel and the window flags [with default arguments as in QLabel] and added the Q_OBJECT macro, as to be compatible to the Qt framework:
Header:
class PixmapLabel : public QLabel
{
Q_OBJECT
public:
explicit PixmapLabel(QWidget* parent=nullptr, Qt::WindowFlags f = Qt::WindowFlags());
explicit PixmapLabel(const QString &text, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
...
}
Module:
PixmapLabel::PixmapLabel(QString const & text, QWidget * parent, Qt::WindowFlags f) :
QLabel(text, parent, f)
{
//By default, this class scales the pixmap according to the label's size
setScaledContents(true);
}

Dark transparent layer over a QMainWindow in Qt

I need to implement a "Loading..." window in my application but I do prefer to cover the whole QMainWindow with a dark transparent layer with a text above. Does anybody know how to do that? I am not sure how to overlap widgets/layouts in Qt. Any help will be appreciated.
This answer is in a series of my overlay-related answers: first, second, third.
The most trivial solution is to simply add a child transparent widget to QMainWindow. That widget must merely track the size of its parent window. It is important to properly handle changes of widget parentage, and the z-order with siblings. Below is a correct example of how to do it.
If you want to stack overlays, subsequent overlays should be the children of OverlayWidget, in the z-order. If they were to be siblings of the OverlayWidget, their stacking order is undefined.
This solution has the benefit of providing minimal coupling to other code. It doesn't require any knowledge from the widget you apply the overlay to. You can apply the overlay to a QMainWindow or any other widget, the widget can also be in a layout.
Reimplementing QMainWindow's paint event would not be considered the best design. It makes it tied to a particular class. If you really think that a QWidget instance is too much overhead, you better had measurements to show that being the case.
It is possible, of course, to make the overlay merely a QObject and to put the painting code into an event filter. That'd be an alternative solution. It's harder to do since you have to also properly deal with the parent widget's Qt::WA_StaticContents attribute, and with the widget potentially calling its scroll() method. Dealing with a separate widget is the simplest.
// https://github.com/KubaO/stackoverflown/tree/master/questions/overlay-widget-19362455
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
class OverlayWidget : public QWidget
{
void newParent() {
if (!parent()) return;
parent()->installEventFilter(this);
raise();
}
public:
explicit OverlayWidget(QWidget * parent = {}) : QWidget{parent} {
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);
newParent();
}
protected:
//! Catches resize and child events from the parent widget
bool eventFilter(QObject * obj, QEvent * ev) override {
if (obj == parent()) {
if (ev->type() == QEvent::Resize)
resize(static_cast<QResizeEvent*>(ev)->size());
else if (ev->type() == QEvent::ChildAdded)
raise();
}
return QWidget::eventFilter(obj, ev);
}
//! Tracks parent widget changes
bool event(QEvent* ev) override {
if (ev->type() == QEvent::ParentAboutToChange) {
if (parent()) parent()->removeEventFilter(this);
}
else if (ev->type() == QEvent::ParentChange)
newParent();
return QWidget::event(ev);
}
};
class LoadingOverlay : public OverlayWidget
{
public:
LoadingOverlay(QWidget * parent = {}) : OverlayWidget{parent} {
setAttribute(Qt::WA_TranslucentBackground);
}
protected:
void paintEvent(QPaintEvent *) override {
QPainter p{this};
p.fillRect(rect(), {100, 100, 100, 128});
p.setPen({200, 200, 255});
p.setFont({"arial,helvetica", 48});
p.drawText(rect(), "Loading...", Qt::AlignHCenter | Qt::AlignVCenter);
}
};
int main(int argc, char * argv[])
{
QApplication a{argc, argv};
QMainWindow window;
QLabel central{"Hello"};
central.setAlignment(Qt::AlignHCenter | Qt::AlignTop);
central.setMinimumSize(400, 300);
LoadingOverlay overlay{&central};
QTimer::singleShot(5000, &overlay, SLOT(hide()));
window.setCentralWidget(&central);
window.show();
return a.exec();
}
I would suggest execute a modal, frameless dialog on top and add a graphics effect on the background widget.
This is IMHO a very flexible and short solution without touching the event system directly.
The performance might be bad - one could improve that by calling drawSource(), but I haven't a reliable solution here yet.
class DarkenEffect : public QGraphicsEffect
{
public:
void draw( QPainter* painter ) override
{
QPixmap pixmap;
QPoint offset;
if( sourceIsPixmap() ) // No point in drawing in device coordinates (pixmap will be scaled anyways)
pixmap = sourcePixmap( Qt::LogicalCoordinates, &offset );
else // Draw pixmap in device coordinates to avoid pixmap scaling;
{
pixmap = sourcePixmap( Qt::DeviceCoordinates, &offset );
painter->setWorldTransform( QTransform() );
}
painter->setBrush( QColor( 0, 0, 0, 255 ) ); // black bg
painter->drawRect( pixmap.rect() );
painter->setOpacity( 0.5 );
painter->drawPixmap( offset, pixmap );
}
};
// prepare overlay widget
overlayWidget->setWindowFlags( Qt::FramelessWindowHint | Qt::Dialog | Qt::WindowStaysOnTopHint );
// usage
parentWidget->setGraphicsEffect( new DarkenEffect );
overlayWidget->exec();
parentWidget->setGraphicsEffect( nullptr );
If you are talking about using a separate layout/widget over your 'Main Window', you could just make the "Loading..." window modal either through the UI editor or in the constructor for your UI.