qtqpixmapqcustomplot

QCustomPlot set pixamp on grid


I tried to add pixamp on a QCustomPlot, this is my code:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {

    ui->setupUi(this);

    _pixmap = new QCPItemPixmap(ui->widget);
    ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked() {

    QImage image = QImage(200, 200, QImage::Format_RGB32);
    int image_size = 200 * 200;
    QRgb pixel;
    QRgb green = qRgb(0, 255, 0);
    QRgb red = qRgb(255, 0, 0);

    for(int i = 0; i < image_size; i++) {

        if(i % 2 == 0) {
            pixel = green;
        }
        else {
            pixel = red;
        }
        image.setPixel(i / 200, i % 200, pixel);
    }

    QPixmap pixels = QPixmap::fromImage(image.scaled(ui->widget->width(),ui->widget->height(),
                                                     Qt::IgnoreAspectRatio, Qt::FastTransformation));

    _pixmap->setVisible(true);
    _pixmap->setScaled(true);
    _pixmap->setPixmap(pixels);

    _pixmap->topLeft->setCoords(2.0, 3.0);
    _pixmap->bottomRight->setCoords(5.0, 0.0);

    ui->widget->replot();
}

The pixmap supposed to be rectangle between([2,3],[5,3],[5,0],[2,0]), but i'm not getting it precise:

enter image description here

Why is the gap and its not set on 5.0?


Solution

  • Looking at method

    void QCPItemPixmap::setScaled ( bool  scaled, Qt::AspectRatioMode  aspectRatioMode = Qt::KeepAspectRatio, Qt::TransformationMode  transformationMode = Qt::SmoothTransformation  )
    

    one can see that default value for aspectRatioMode is Qt::KeepAspectRatio change that to Qt::IgnoreAspectRatio to achieve result you want.

    It would look like this:

    ...
    _pixmap->setVisible(true);
    _pixmap->setScaled(true, Qt::IgnoreAspectRatio);
    _pixmap->setPixmap(pixels);
    ...