c++qtqt5qcustomplot

QCPItemLine using layers


what is the correct way to use QCPItemLine with layers? i have a pixamp which i draw on the grid with QCustomPlot and i want to draw a line above the pixmap, so far it is being shown below the pixmap.

lets say i already set a pixmap how can i be sure where the line will be shown? according to documentaions the way to draw a line is:

QCOItemLine* line = new QCOItemLine(ui->grid);

_line->start->setCoords(x_tail, y_tail);
_line->end->setCoords(x_head, y_head);

ui->widget->replot();

unlike points the line dosent related to specific graph or layer so how do i manage it?

this is how i set the pixmap:

QCPItemPixamp* _pixmap;
Qimage image;
getImageData(image);

ui->grid->addLayer("map", ui->grid->layer("main));

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

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

_pixmap->topLeft->setCoords(left_x, bottom_y);
_pixmap->bottomRight->setCoords(right_x, top_y);

i want to draw a line above this pixmap


Solution

  • You have to set a different layer to each item, and then you can use moveLayer() to move the layers.

    // create layers
    ui->grid->addLayer("line");
    ui->grid->addLayer("pixmap");
    
    //create line
    QCPItemLine* _line = new QCPItemLine(ui->grid);
    _line->start->setCoords(1, 5);
    _line->end->setCoords(5, 3);
    //set layer
    _line->setLayer("line");
    
    //create pixmap
    QCPItemPixmap  *_pixmap = new   QCPItemPixmap(ui->grid);
    QImage image;
    getImageData(image);
    QPixmap pixels = QPixmap::fromImage(image.scaled(ui->grid->width(),
    ui->grid->height(), Qt::IgnoreAspectRatio, Qt::FastTransformation));
    _pixmap->setPixmap(pixels);
    _pixmap->setVisible(true);
    _pixmap->setScaled(true);
    _pixmap->topLeft->setCoords(1, 5);
    _pixmap->bottomRight->setCoords(5, 3);
    // set layer
    _pixmap->setLayer("pixmap");
    
    // move layers
    ui->grid->moveLayer(_line->layer(), _pixmap->layer(), QCustomPlot::limAbove );
    ui->grid->replot();