将模型追加到VXYModelMapper

时间:2018-12-02 04:37:53

标签: qt qml qstandarditemmodel

我在绘图图中使用“ VXYModelMapper”和“ QStandardItemModel”。我使用打击代码,但是将模型附加到VXYModelMapper的速度很慢。

VXYModelMapper {
        model: myChartClass.newMyChartModel
        series: lineSeries
        xColumn: 0
        yColumn: 1
}
void MyChartClass::setMyChartModel(QStandardItemModel *model)
{
  newMyChartModel= model;
  emit myChartModelChanged(model);
}

QStandardItemModel* lineModel=new QStandardItemModel(npcArray.size(), 2);
foreach(const QJsonValue & val, npcArray){
    double xVal=val.toObject().value("x").toDouble();
    double yVal=val.toObject().value("y").toDouble();

    QStandardItem *item1 = new QStandardItem(QString::number(xVal));
    lineModel->setItem(i, 0, item1);

    QStandardItem *item2 = new QStandardItem(QString::number(yVal));
    lineModel->setItem(i, 1, item2);
     i+=1;
}

 //To this line everything is very good and fast   

QElapsedTimer timer;
timer.start();
myChartClass->setMyChartModel(lineModel);
qDebug() << "The slow operation took" << timer.elapsed() << "millisecond";
  

输出:缓慢的操作花费了 123045 毫秒。总分   计数: 5188

这是非常慢的:(

更新: 我使用QTimer但没有好处。 :(

lineModel=new QStandardItemModel(0, 2);
myChartClass->setMyChartModel(lineModel);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(30);


void MyChartClass::update(){

int toIndex=fromIndex+100;
if(toIndex>=npcArray.size()){
    toIndex=npcArray.size()-1;
}

for(int i=fromIndex;i<toIndex;i++){
    double xVal=npcArray[i].toObject().value("x").toDouble();
    double yVal=npcArray[i].toObject().value("y").toDouble();

    QStandardItem *item1 = new QStandardItem(QString::number(xVal));
    lineModel->setItem(i, 0, item1);

    QStandardItem *item2 = new QStandardItem(QString::number(yVal));
    lineModel->setItem(i, 1, item2);
}
fromIndex=toIndex;
}

0 个答案:

没有答案