同事警告我说我错了。这就是为什么我完全重新提出了我以前的问题。
a)最佳创建新行
b)如何可靠地选择20个其他人之间的特定行
c)如何最好地移动一条线的起点
d)如何最好地移动一条线的终点
e)如何最好地移动线
f)如何删除一行
文档和示例中描述了所有内容,但每个示例都为操作选择了不同的位置。有人使用视图,一些场景,另一个使用大部分内容。有人使用bouding rect,另一个没有,有人使用编辑模式而另一个没有等...
myscene.cpp
#include "myscene.h"
#include "myview.h"
#include "mymovingpoint.h"
#include <qgraphicsitem.h>
#include <qgraphicsview.h>
#include <qobject.h>
#include <qgraphicsview.h>
#include <qpoint.h>
#include <qmath.h>
/*
*
*
*/
myScene::myScene(QObject *parent)
: QGraphicsScene(parent)
{
myMode = myMode::InsertItem;
}
/*
*
*
*/
void myScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
# remove item added by clickevent, if doubleclicked
if(line)
{
removeItem(line);
}
# if doubleclick and left button, select existing item
if(mouseEvent->buttons().testFlag(Qt::LeftButton))
{
//QGraphicsItem *item = itemAt(mouseEvent->scenePos(), QTransform());
# itemAt() returns only the object exactly under the mouse
# function getNearObject return object 3 points around
QGraphicsItem *item = getNearObject(mouseEvent);
if(item)
{
myMode = myMode::EditItem;
myLineItem *tempLine = dynamic_cast<myLineItem*>(item);
oldLine = line;
line = tempLine;
QColor myclr;
myclr.setRgb(255,0,0,255);
line->setPen(QPen(myclr, 2));
line->addMovingPoints();
}
else
{
myMode = myMode::InsertItem;
}
}
}
/*
*
*
*/
#return nearest objects
QGraphicsItem* myScene::getNearObject(QGraphicsSceneMouseEvent *mouseEvent)
{
int roundvalue = -3;
int roundx;
int roundy;
QPointF pointf = mouseEvent->scenePos();
QPointF pointtmp;
roundx = roundvalue;
roundy = roundvalue;
while(roundy <= roundvalue*-1)
{
while(roundx <= roundvalue*-1)
{
pointtmp.setX(pointf.x()-roundx);
pointtmp.setY(pointf.y()-roundy);
QGraphicsItem *item = itemAt(pointtmp, QTransform());
if(item)
{
return item;
}
roundx += 1;
}
roundx += roundvalue;
roundy += 1;
}
return NULL;
}
/*
*
*
*/
void myScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if(mouseEvent->buttons().testFlag(Qt::LeftButton))
{
QGraphicsItem *item = itemAt(mouseEvent->scenePos(), QTransform());
if(myMode == myMode::InsertItem)
{
if(oldLine)
{
myLineColor.setRgb(213, 182, 10, 255);
oldLine->setPen(QPen(myLineColor, 2));
}
}
if(myMode == myMode::EditItem)
{
myLineItem *tempLine = dynamic_cast<myLineItem*>(item);
if(tempLine)
{
oldLine = line;
line = tempLine;
QColor myclr;
myclr.setRgb(255,0,0,255);
line->setPen(QPen(myclr, 2));
myMovingPoint myPoint(line);
}
}
else if(myMode == myMode::InsertItem)
{
oldLine = line;
if(mouseEvent->modifiers() & Qt::ControlModifier)
{
line = new myLineItem(QLineF(toNearest5(mouseEvent->scenePos()), toNearest5(mouseEvent->scenePos())));
}
else
{
line = new myLineItem(QLineF(mouseEvent->scenePos(), mouseEvent->scenePos()));
}
myLineColor.setRgb(213, 182, 10, 255);
line->setPen(QPen(myLineColor, 2));
}
else
{
QGraphicsScene::mousePressEvent(mouseEvent);
}
}
}
/*
*
*
*/
void myScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if(!items().contains(line))
{
addItem(line);
}
if(mouseEvent->buttons().testFlag(Qt::LeftButton))
{
QLineF newLine;
if(myMode == myMode::InsertItem)
{
if(mouseEvent->modifiers() & Qt::ControlModifier)
{
newLine = QLineF(toNearest5(line->line().p1()), toNearest5(mouseEvent->scenePos()));
}
else
{
newLine = QLineF(line->line().p1(), mouseEvent->scenePos());
}
myLineColor.setRgb(213, 182, 10, 255);
line->setLine(newLine);
update();
}
else if(myMode == myMode::EditItem)
{
QLineF newLine = QLineF(line->line().p1(), mouseEvent->scenePos());
line->setLine(newLine);
update();
}
}
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
/*
*
*
*/
void myScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
myMode = myMode::InsertItem;
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
/*
*
*
*/
void myScene::keyPressEvent(QKeyEvent * keyEvent)
{
if(keyEvent->key() == Qt::Key_Delete)
{
if(line)
{
removeItem(line);
}
}
}
/*
*
*
*/
QPointF myScene::toNearest5(QPointF i)
{
int shift = 10;
int r = i.x();
int s = i.y();
r = (i.x()+5.0)/10;
s = (i.y()+5.0)/10;
r = r*10;
s = s*10;
QPointF output = QPointF(r, s);
return (output);
}
mysceneview.cpp
#include "myview.h"
#include <qmath.h>
#include <qmatrix.h>
myView::myView(QGraphicsScene *scene, QWidget *parent)
: QGraphicsView(scene, parent)
{
}
void myView::keyPressEvent(QKeyEvent *event)
{
QGraphicsView::keyPressEvent(event);
}
void myView::keyReleaseEvent(QKeyEvent *event)
{
QGraphicsView::keyReleaseEvent(event);
}
void myView::enterEvent(QEvent *event)
{
viewport()->setCursor(Qt::ArrowCursor);
QGraphicsView::enterEvent(event);
}
void myView::mousePressEvent(QMouseEvent* event)
{
if (event->modifiers() & Qt::ControlModifier)
{
viewport()->setCursor(Qt::ArrowCursor);
_lastPos = QPoint(event->pos());
//_lastPos = QPoint(event->pos().x()-event->pos().x()%5, event->pos().y()-event->pos().y()%5);
}
else
{
viewport()->setCursor(Qt::ArrowCursor);
_lastPos = event->pos();
}
QGraphicsView::mousePressEvent(event);
}
void myView::mouseMoveEvent(QMouseEvent* event)
{
viewport()->setCursor(Qt::ArrowCursor);
if (event->buttons().testFlag(Qt::RightButton))
{
QScrollBar *hBar = horizontalScrollBar();
QScrollBar *vBar = verticalScrollBar();
QPoint delta = event->pos() - _lastPos;
_lastPos = event->pos();
hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
vBar->setValue(vBar->value() - delta.y());
//event->ignore();
}
QGraphicsView::mouseMoveEvent(event);
}
void myView::mouseReleaseEvent(QMouseEvent* event)
{
viewport()->setCursor(Qt::ArrowCursor);
QGraphicsView::mouseReleaseEvent(event);
}
#if QT_CONFIG(wheelevent)
void myView::wheelEvent(QWheelEvent *e)
{
if (e->delta() > 0)
{
x=x+0.1;
qreal scale = x;
QMatrix matrix;
matrix.scale(scale, scale);
setMatrix(matrix);
//zoomIn(6);
}
else
{
x=x-0.1;
qreal scale = x;
QMatrix matrix;
matrix.scale(scale, scale);
setMatrix(matrix);
//zoomOut(6);
}
e->accept();
}
#endif