如何使用QPushButton从QGraphicsView正确擦除框

时间:2019-07-26 21:52:48

标签: qtableview qgraphicsview qgraphicsscene qpushbutton

我有一个QGraphicsView和一个QTableView。可以在QGraphicsView上绘制正方形并将裁剪后的图像保存在正方形中作为QTableView中的索引。用户可以通过使用鼠标双击它来重新激活该正方形。 现在,在QGraphicsView中淹没了多个正方形并在QTableView中淹没了索引之后,用户想要使用QPushButton删除索引和正方形之一。 我可以成功擦除QTableView中的索引,但仍然无法擦除QGraphicsView中的正方形。

下面是我正在使用的代码段: 请注意,下面的DataRegion是我用来处理盒子图形的外部类:

mainwindow.h

public:
    QList<DataRegion*> selections;
    int currentSelection;
    QSqlTableModel *mModelLeftCamera;
    dataLeftCamera *mDatabaseLeftCamera;

mainwindow.cpp

// constructor
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //..operations
    //.... Initialize and configure database for the left tableview
    mDatabaseLeftCamera = new dataLeftCamera(this);
    mModelLeftCamera = nullptr;
    mModelLeftCamera = new QSqlTableModel(this);
    ui->recordLeftTableView->setModel(mModelLeftCamera);
    currentSelection = -1;

}

用户绘制框

void MainWindow::onRubberBandUpdate(const QRect &viewportRect, const QPointF &fromScenePoint, const QPointF &toScenePoint)
{
      //..operations

            if(currentSelection >= 0)
                selections[currentSelection]->setActiveState(false);
            QRectF select;
            select.setCoords(start.x(), start.y(), end.x(), end.y());

            DataRegion *region = new DataRegion(select);
            region->index = currentSelection+1;

            // link the box to the table
            leftCamParameters *boxParam = new leftCamParameters();
            SelectionData tmpdat = boxParam->getData();

            // getting the A-B-C-D Coordinates
            tmpdat.mACoord.setX((int)select.topLeft().x());
            tmpdat.mACoord.setY((int)select.topLeft().y());
            tmpdat.mBCoord.setX((int)select.topRight().x());
            tmpdat.mBCoord.setY((int)select.topRight().y());
            tmpdat.mCCoord.setX((int)select.bottomLeft().x());
            tmpdat.mCCoord.setY((int)select.bottomLeft().y());
            tmpdat.mDCoord.setX((int)select.bottomRight().x());
            tmpdat.mDCoord.setY((int)select.bottomRight().y());

            tmpdat.mPath = currentLeftImagePath;

            boxParam->setData(tmpdat);

            mDatabaseLeftCamera->addItem(boxParam);
            mModelLeftCamera->select();
            ui->recordLeftTableView->show();

            region->setActiveState(true);
            region->updateLabelText();

            currentSelection = selections.size();
            selections.append(region);
            leftScene->addItem(region->getGraphics());
            ui->leftView->show();

    //.. additional operations
}

回叫QPushButton,以擦除QTableView上的正方形和参照元素:

void MainWindow::on_eraseLBoxesBtn_clicked()
{
    // Erase from the db (ok works)
    int itemId = currentSelection;
    mDatabaseLeftCamera->removeItem(itemId);
    mModelLeftCamera->select();

    // Erase from QGraphicsView (does not work)
    SelectionData *currentData;
    currentSelection = selections.size();
    leftScene->removeItem(currentData));
    ui->leftView->show();
}

因此,总而言之,我知道如何访问双框的索引并从QTableView删除它,但不知道如何从QGraphicsView擦除该框。 任何有关如何解决此问题的见解都将受到赞赏。

0 个答案:

没有答案