我有一个小的用户界面,使用QPushButton上传图像。我可以在上传的图像上绘制正方形,从而启用QCheckBox
。绘制正方形后,立即在QTableView上创建参考索引。因此,例如在下面的打印屏幕中,我有四个(N.4)红色方块,它们等效于QTableView的四个(N.4)索引。请参见下面的GUI:
可以通过双击内部旧方块来重新激活它。
如果用户不再需要活动正方形,则使用QPushButton
“擦除正方形”将擦除活动正方形以及QTableView
的相关行(参考索引)。
但是,一旦我尝试擦除正方形,GUI就会崩溃。我不确定如何处理此异常。删除框的功能在下方,称为clearSceneLeft()
。但是我不确定这是否也与QSQLITE
有关,因为每个框基本上都等效于QTableView中的一行。
查看我用于此小型gui的代码中最重要的部分。
在mouseDoubleClicked(QPointF)
下MainWindow
的构造函数中的rubberband
由类graphicsForClick
处理,我在下面仅报告最重要的部分:>
graphisforclick.h
class graphicsForClick : public QGraphicsScene
{
Q_OBJECT
public:
explicit graphicsForClick(QObject *parent = nullptr);
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent);
signals:
void mouseReleased(QPointF point);
void rightClick(QPointF point);
void mouseDoubleClicked(QPointF point);
};
graphisforclick.cpp
graphicsForClick::graphicsForClick(QObject *parent):
QGraphicsScene(parent)
{}
void graphicsForClick::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
mouseDoubleClicked(mouseEvent->scenePos());
QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
}
void graphicsForClick::mouseReleaseEvent(QGraphicsSceneMouseEvent * me)
{
if(me->button() == Qt::LeftButton)
mouseReleased(me->scenePos());
else rightClick(me->scenePos());
QGraphicsScene::mouseReleaseEvent(me);
}
在主窗口上,设置所有用于处理doubleClick的rubberband
控件,并处理QPushButton
on_eraseSquare_clicked()
:
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
leftScene = new graphicsForClick();
ui->graphicsView->setScene(leftScene);
ui->graphicsView->show();
currentSelection = -1;
mDatabaseLeftCamera = new dataLeftCamera(this);
mModelLeftCamera = nullptr;
mModelLeftCamera = new QSqlTableModel(this);
ui->tableView->setModel(mModelLeftCamera);
connect(leftScene, SIGNAL(mouseDoubleClicked(QPointF)),
this, SLOT(onSceneDoubleClick(QPointF)));
}
void MainWindow::on_eraseSquare_clicked()
{
clearSceneLeft();
}
void MainWindow::clearSceneLeft()
{
if (selections.size() > 0) {
qDeleteAll(selections);
selections.clear();
currentSelection = -1;
} for(int p=0;p<shape.size();p++) {
leftScene->removeItem(shape[p]);
delete shape[p];
}
shape.clear();
}
void MainWindow::onSceneDoubleClick(QPointF point)
{
qDebug() << "Click!";
QList<QGraphicsItem*> foundItems = leftScene->items(point);
if(foundItems.size() > 0 && foundItems[0]->group() != nullptr)
{
qDebug() << "Found";
int i = 0;
for(i=0;i<selections.size();i++)
{
qDebug() << "Iterate";
if(selections[i]->getGraphics() == foundItems[0]->group())
{
qDebug() << "Target";
break;
}
}
if(currentSelection >= 0)
selections[currentSelection]->setActiveState(false);
currentSelection = i;
selections[currentSelection]->setActiveState(true);
}
}
mainwindow.h
private:
QList<QGraphicsRectItem*> shape;
setActiveState
由类Square
处理。因此,无论何时用户绘制一个正方形(即活动正方形),而当用户绘制第二个正方形(即活动正方形)时,前一个正方形也会停用更改颜色。以下是与此类相关的代码中最重要的部分:
square.cpp
Square::Square(QRectF newarea)
{
colorActive = Qt::red;
colorInactive = Qt::darkRed;
mItemSquare = new QGraphicsRectItem(newarea);
mItemSquare->setPen(QPen(colorActive));
gfx = new QGraphicsItemGroup();
gfx->addToGroup(mItemSquare);
index = -1;
}
void Square::updateColor(QColor active, QColor inactive)
{
colorActive = active;
colorInactive = inactive;
}
void Square::setActiveState(bool newactive)
{
active = newactive;
if(active)
{
mItemSquare->setPen(QPen(colorActive));
}
else
{
mItemSquare->setPen(QPen(colorInactive));
}
}
void Square::setVisible(bool newstate)
{
mItemSquare->setVisible(newstate);
}
编译器错误:
ASSERT failure in QList<T>::operator[]: "index out of range"
如果更容易看一下最小可验证示例的源代码,则为:git clone https://ERaggi@bitbucket.org/ERaggi/reference.git
最近几天,我一直在努力解决这个问题,但还没有走运。非常感谢您为这个问题提供帮助。