QCustomPlot在网格上设置pixamp

时间:2018-08-27 06:01:22

标签: qt qpixmap qcustomplot

我试图在QCustomPlot上添加pixamp,这是我的代码:

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();
}

像素图应该是介于[[2,3],[5,3],[5,0],[2,0])之间的矩形,但是 我不太清楚:

enter image description here

为什么间隔没有设置为5.0?

1 个答案:

答案 0 :(得分:1)

查看方法

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

人们可以看到aspectRatioMode的默认值为Qt::KeepAspectRatio,将其更改为Qt::IgnoreAspectRatio,以获得所需的结果。

它看起来像这样:

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