Skip to content
QtWS25 Last Chance
  • QPainter does not work

    Unsolved General and Desktop qpainter qline qimage qwidget qpaintevent
    18
    0 Votes
    18 Posts
    2k Views
    C
    @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(); }
  • 0 Votes
    3 Posts
    416 Views
    D
    @sierdzio said in QMouseEvent::pos() returns different values on different OS: No, it is not related to the style selected. I checked on Windows and it has no shift on Windows Vista, Windows and Fusion.
  • QPaintEvent ignore automatic clipping

    Unsolved General and Desktop qpaintevent
    3
    0 Votes
    3 Posts
    263 Views
    SGaistS
    Hi, Do you mean draw on a different widget ? If so, you can't. What you can do is use a larger widget so you have a larger surface to use.
  • 1 Votes
    1 Posts
    470 Views
    No one has replied
  • 0 Votes
    6 Posts
    4k Views
    SGaistS
    It uses the raster engine. See here for historical informations.
  • 0 Votes
    26 Posts
    11k Views
    W
    @SilverSurfer I don't have the time handy to do a full working example, but this is the technique in glsl: https://www.khronos.org/opengl/wiki/Data_Type_(GLSL)#Swizzling (The vec4 can hold "rgba" values if that's what you put in it - the example there just calls it "xyzw" because the data type generically handles any 4 floats.) Basically, start from an example that draws an image with a simple glsl shader as a texture. Then hack on the shader until it flips around the colors like you want when it draws the texture. Then use your code that gets the image over the network to upload the image as the OpenGL texture every frame.
  • 0 Votes
    1 Posts
    693 Views
    No one has replied
  • paintEvent is not painting complete objects

    Unsolved General and Desktop qpainter qpaintevent
    11
    0 Votes
    11 Posts
    6k Views
    J
    @VRonin Depends on what he wants to achieve. Is it a widget, than I agree with you. Is it some CAD-drawing (looks like a diagram symbol to me), then I would prefer drawing. For the latter the Graphics View Framework would be more practical though.
  • Not able to draw lines inside QPaintEvent

    Solved General and Desktop qpaintevent qpainter
    3
    0 Votes
    3 Posts
    1k Views
    NIXINN
    Thanx, it really solved my problem
  • 0 Votes
    6 Posts
    3k Views
    SGaistS
    Rather than going up-front with building your own GUI for a painting program, I'd recommend taking a look at what is currently existing like Krita or KolourPaint. Painting is a vast subject that can cover many aspects. Just take a look at Qt's examples on the subject. You should also take the time to look at QtQuick for the GUI design part.