0
votes

I tried to find some helps around internet, i had some codes to test but none works like i want.

I would draw with Qt something pixel by pixel.

I tried with a QImage within a QLabel with the protected event mousePressEvent it worked, but the pixels are too small to see them.

I tried to scale my Image, it's much better, but the position X, Y are not the same position of the pixels now or they're not synchronized with the pixels locations.

Here a screenshot of what i want do, if someone has an idea to recreate this...

enter image description here

If i can exactly done this via Qt i'll save a lots of time, just a basic drawing pixel by pixel, i created this example with Paint, Black and White format zoomed to 800%.

Thanks.

1
try to make your question more specific: what code part do you have trouble with? What is the output you want? what do you really obtain?Chris Maes

1 Answers

0
votes

This is a solution hard-coded for a specific size - just implement relative coordinate mapping between pixmap and draw widget and you are set.

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0) : QWidget(parent), pressed(0) {
        color = Qt::black;
        pixmap = new QPixmap("h:/small.png");
        resize(240, 240);
    }
    ~Widget() { if (pixmap) delete pixmap; }

protected:
    void paintEvent(QPaintEvent *) {
        QPainter painter(this);
        painter.drawPixmap(0, 0, pixmap->scaled(240, 240));
        painter.drawPixmap(0, 0, *pixmap);
    }

    void mousePressEvent(QMouseEvent *e) {
        if (e->button() == Qt::RightButton)
            color = color == Qt::black ? Qt::white : Qt::black;
        else {
            pressed = 1;
            draw(e);
        }
    }

    void mouseReleaseEvent(QMouseEvent *) { pressed = 0; }
    void mouseMoveEvent(QMouseEvent *e) { draw(e); }

private:
    void draw(QMouseEvent *e) {
        if (pressed) {
            QPainter painter(pixmap);
            painter.setPen(color);
            int x = e->pos().x() / 12;
            int y = e->pos().y() / 12;
            painter.drawPoint(x, y);
            repaint();
        }
    }

    QColor color;
    QPixmap *pixmap;
    bool pressed;
};