2

I want to ask a question about qcustomplot. How can i change itemLine position according to Slider? (like x = a)

#include "itemline.h"
#include "ui_itemline.h"
#include "qcustomplot.h"

itemLine::itemLine(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::itemLine)
{
    ui->setupUi(this);

    QCPItemLine *item = new QCPItemLine(ui->customPlot);
    ui->customPlot->addItem(item);
    item->setPen(QPen(Qt::red));
    item->start->setCoords(1,0);
    item->end->setCoords(1,5);

    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(horzSliderChanged(int)));
}

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

void itemLine::horzSliderChanged(int value)
{
     // how can i change item position acording to horizontalSlider, like "x = a" line ?
}
Nejat
  • 31,784
  • 12
  • 106
  • 138
Emre
  • 43
  • 1
  • 6

1 Answers1

3

First you should keep the pointer to QCPItemLine as a class member. After that horzSliderChanged slot could be like :

void itemLine::horzSliderChanged(int value)
{
    item->start->setCoords(value,0);
    item->end->setCoords(value,5);
    ui->customPlot->replot();
}
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • How can I keep the pointer to QCPItemLine as a class member? Sorry I am new to Qt. – Emre Dec 08 '14 at 17:39
  • It's not about Qt. You should learn C++. Just declare the pointer in your class header file. – Nejat Dec 08 '14 at 17:43
  • Ok thanks. You are right i must study c++. I did that you said and it woks. Thank you Nejat. – Emre Dec 08 '14 at 23:13