0

我有这个应用程序,用户可以在 QGraphicsView 中绘制一些自定义 QGraphicsItems,我希望关于这些项目的一些数据也显示在 QTableWidget 中。

自定义 QGraphicsItem: 头文件的代码:

    类 Clothoid:公共 QGraphicsItem
    {
    上市:
        Clothoid(QPoint startPoint, QPoint endPoint);
        虚拟〜Clothoid();

        QPoint 点;
        QPoint ePoint;
        CFloat startCurvature;
        CFloat endCurvature;
        CFloat 回旋线长度;
        CFloat tangentAngle;
    ...
    }

cpp文件:

    Clothoid::Clothoid(QPoint startPoint, QPoint endPoint)
    {
        sPoint = 起点;
        ePoint = 端点;
        起始曲率 = 0.0;
        结束曲率 = 0.0;
        ClooidLength = sqrt(pow(endPoint.x() - startPoint.x(),2) +
                              pow(endPoint.y() - startPoint.y(),2));
    }

图形视图的代码:

    renderArea::renderArea(QWidget *parent):
            QGraphicsView(父)
    {
        场景 = 新 QGraphicsScene(this);
        场景->setItemIndexMethod(QGraphicsScene::NoIndex);
        场景->setSceneRect(0, 0, 850, 480);
        设置场景(场景);
        setCacheMode(CacheBackground);
        setViewportUpdateMode(BoundingRectViewportUpdate);
        setRenderHint(QPainter::抗锯齿);
        setTransformationAnchor(AnchorUnderMouse);
        规模(qreal(1.0),qreal(1.0));
        setMinimumSize(400, 400);
    }

    无效渲染区域::mousePressEvent(QMouseEvent *事件)
    {
        QPoint p = event->pos();

        更新列表(p);
    }

    void renderArea::updateList(const QPoint &p)
    {
        点点;
        点.点 = p;
        点.isSelected = false;
        list.append(point);
        如果 (list.size() > 1)
            updateClothoid(list[list.size()-2].point, list[list.size()-1].point);
    }

    void renderArea::updateClothoid(const QPoint &p1, const QPoint &p2)
    {
        Clothoid *temp = new Clothoid(p1, p2);

        clothoids.append(temp);

        场景->添加项目(临时);

        发出clothoidAdded(&clothoids);
    }

其中回旋曲线定义为:

QList 回旋曲线;

我将信号与表格小部件专用的另一个类中的插槽连接起来:

    void TableViewList::onClothoidAdded(QList *clothoids)
    {
        setRowCount(clothoids->size());

        for (int i = 0; i size(); i++){
            setItem(i+1, 0, new QTableWidgetItem(clothoids->at(i)->startCurvature));
            setItem(i+1, 1, new QTableWidgetItem(clothoids->at(i)->endCurvature));
            setItem(i+1, 2, new QTableWidgetItem(clothoids->at(i)->clothoidLength));
            setItem(i+1, 3, new QTableWidgetItem(clothoids->at(i)->sPoint.x() + ", " +
                                               回旋曲线->at(i)->sPoint.y()));
            setItem(i+1, 4, new QTableWidgetItem(clothoids->at(i)->ePoint.x() + ", " +
                                               回旋曲线->at(i)->ePoint.y()));
        }

    }

问题是数据没有插入表中。我通过调试进行了检查,发现该数组包含所需的数据。我怎样才能正确访问它?有任何想法吗?

在尝试使用 QTableView 和 QStandardItemModel 时,我遇到了这个问题:模型中的数据没有插入到表中:

    renderingWidget::renderingWidget(QWidget *parent) :
            QWidget(父),
            ui(新的 Ui::renderingWidget)
    {
        ui->setupUi(这个);

        model.setColumnCount(3);
        ui->clothoidTable->setModel(&model);

        SpinBoxDelegate 委托;
        ui->clothoidTable->setItemDelegate(&delegate);


        connect (ui->saveButton, SIGNAL(clicked()), this, SLOT(createClothoid()));
    }


    无效的renderingWidget::createClothoid()
    {
        model.setRowCount(model.rowCount()+1);

        QModelIndex index = model.index(model.rowCount(), 1, QModelIndex());
        model.setData(index, QVariant(ui->lengthSpinBox->value()));
        index = model.index(model.rowCount(), 2, QModelIndex());
        model.setData(index, QVariant(ui->sCurvSpinBox->value()));
        index = model.index(model.rowCount(), 3, QModelIndex());
        model.setData(index, QVariant(ui->eCurvSpinBox->value()));

        ui->clothoidTable->setModel(&model);
    }

我希望能够在某些文本框/旋转框中插入数据,然后单击按钮,数据应添加到表中。但仅更新行数,而不更新其中的数据。在为模型设置数据时我做错了什么吗?

4

1 回答 1

0

这将很难听到,但我会远离QTableWidget便利班。

查看Qt Model/View Programming 上的这个链接,了解 Qt 是如何真正处理像你这样的复杂表的。

我在模型/视图上的两分钱是这样的:

  1. 使用 aQTableView而不是 a QTableWidget
  2. 子类化QAbstractItemModel和实现data()(用于阅读),以及文档中您需要的所有其他功能。这是最棘手的部分,但请参阅上面的链接以了解如何执行此操作。
  3. setModel()QTableView子类模型。

如果您有更多问题,我很乐意为您解答。

于 2011-08-02T13:23:33.340 回答