Qt 捕捉到网格不会';行不通
我正在尝试在网格上进行捕捉,这样无论我画什么,它都只需要网格点,而不需要其他点。我在cadgraphicscene.cpp中创建了网格,并创建了不同的捕捉类。 我的网格如下所示: cadgraphicsene.cppQt 捕捉到网格不会';行不通,qt,qgraphicsview,Qt,Qgraphicsview,我正在尝试在网格上进行捕捉,这样无论我画什么,它都只需要网格点,而不需要其他点。我在cadgraphicscene.cpp中创建了网格,并创建了不同的捕捉类。 我的网格如下所示: cadgraphicsene.cpp void CadGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect) { const int gridSize = 50; const int realLeft = stat
void CadGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
{
const int gridSize = 50;
const int realLeft = static_cast<int>(std::floor(rect.left()));
const int realRight = static_cast<int>(std::ceil(rect.right()));
const int realTop = static_cast<int>(std::floor(rect.top()));
const int realBottom = static_cast<int>(std::ceil(rect.bottom()));
// Draw grid.
const int firstLeftGridLine = realLeft - (realLeft % gridSize);
const int firstTopGridLine = realTop - (realTop % gridSize);
QVarLengthArray<QLine, 100> lines;
for (qreal x = firstLeftGridLine; x <= realRight; x += gridSize)
lines.append(QLine(x, realTop, x, realBottom));
for (qreal y = firstTopGridLine; y <= realBottom; y += gridSize)
lines.append(QLine(realLeft, y, realRight, y));
painter->setPen(QPen(QColor(220, 220, 220), 0.0));
painter->drawLines(lines.data(), lines.size());
// Draw axes.
painter->setPen(QPen(Qt::lightGray, 0.0));
painter->drawLine(0, realTop, 0, realBottom);
painter->drawLine(realLeft, 0, realRight, 0);
}
void cadgraphicscene::牵引地面(油漆工*油漆工、施工工和校正工)
{
const int gridSize=50;
const int reallleft=static_cast(std::floor(rect.left());
const int realRight=static_cast(std::ceil(rect.right());
const int realTop=static_cast(std::floor(rect.top());
const int realBottom=static_cast(std::ceil(rect.bottom());
//绘制网格。
const int firstLeftGridLine=realLeft-(realLeft%gridSize);
const int firstTopGridLine=realTop-(realTop%gridSize);
QVarLengthArray线;
对于(qreal x=firstLeftGridLine;x绘制线(lines.data(),lines.size());
//画轴。
painter->setPen(QPen(Qt::浅灰色,0.0));
画师->绘图线(0,realTop,0,realBottom);
画师->绘图线(realLeft,0,realRight,0);
}
我的snap类如下所示:
snap.cpp
#include "snap.h"
#include <QApplication>
Snap::Snap(const QRect& rect, QGraphicsItem* parent,
QGraphicsScene* scene):
QGraphicsRectItem(QRectF())
{
setFlags(QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemSendsGeometryChanges);
}
void Snap::mousePressEvent(QGraphicsSceneMouseEvent *event){
offset = pos() - computeTopLeftGridPoint(pos());
QGraphicsRectItem::mousePressEvent(event);
}
QVariant Snap::itemChange(GraphicsItemChange change,
const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
QPointF newPos = value.toPointF();
if(QApplication::mouseButtons() == Qt::LeftButton &&
qobject_cast<CadGraphicsScene*> (scene())){
QPointF closestPoint = computeTopLeftGridPoint(newPos);
return closestPoint+=offset;
}
else
return newPos;
}
else
return QGraphicsItem::itemChange(change, value);
}
QPointF Snap::computeTopLeftGridPoint(const QPointF& pointP){
CadGraphicsScene* customScene = qobject_cast<CadGraphicsScene*> (scene());
int gridSize = customScene->getGridSize();
qreal xV = floor(pointP.x()/gridSize)*gridSize;
qreal yV = floor(pointP.y()/gridSize)*gridSize;
return QPointF(xV, yV);
}
#包括“snap.h”
#包括
快照::快照(常量QRect和rect,QGraphicsItem*父对象,
QGraphicscene*场景):
qgraphicsrecitem(QRectF())
{
setFlags(QGraphicsItem::ItemIsSelectable|
QGraphicsItem::ItemIsMovable|
QGraphicsItem::ItemSendsGeometryChanges);
}
void Snap::MousePresseEvent(QGraphicsSceneMouseEvent*事件){
偏移量=pos()-computeTopLeftGridPoint(pos());
QGraphicsRectItem::MousePresseEvent(事件);
}
QVariant Snap::itemChange(GraphicsSitemChange更改,
常数(变量和值)
{
if(change==ItemPositionChange&&scene()){
QPointF newPos=value.toPointF();
如果(QApplication::mouseButtons()==Qt::LeftButton&&
qobject_cast(场景()){
QPointF closestPoint=computeTopLeftGridPoint(新位置);
返回闭合点+=偏移量;
}
其他的
返回newPos;
}
其他的
返回QGraphicsItem::itemChange(change,value);
}
QPointF快照::computeTopLeftGridPoint(常量QPointF和pointP){
CadGraphicscene*customScene=qobject_cast(scene());
int gridSize=customScene->getGridSize();
qreal xV=地板(pointP.x()/gridSize)*gridSize;
qreal yV=地板(pointP.y()/gridSize)*gridSize;
返回QPointF(xV,yV);
}
snap.h
#ifndef SNAP_H
#define SNAP_H
#include <QGraphicsRectItem>
#include "cadgraphicsscene.h"
class Snap : public QGraphicsRectItem
{
public:
Snap(const QRect& rect, QGraphicsItem* parent,
QGraphicsScene* scene);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
QVariant itemChange(GraphicsItemChange change,
const QVariant &value);
private:
QPointF offset;
QPointF computeTopLeftGridPoint(const QPointF &pointP);
};
#endif // SNAP_H
\ifndef SNAP\u H
#定义SNAP_H
#包括
#包括“CadGraphicscene.h”
类快照:公共QGraphicsRecitem
{
公众:
快照(常量QRect和rect,QGraphicsItem*父对象,
(现场);;
受保护的:
无效鼠标压力事件(QGraphicsSceneMouseEvent*事件);
QVariant itemChange(图形站点更改),
常数(变量和值);
私人:
QPointF偏移量;
QPointF计算点(常量QPointF&pointP);
};
#endif//SNAP_H
但是什么也没发生,没有捕捉。你能在上面帮我吗?问题之一是你通过了
QRect()
传递到Snap
类的初始化列表中的QGraphicsRectItem
构造函数。这意味着它的宽度和高度将为0。相反,传递给Snap构造函数的对象与传递给Snap构造函数的对象相同:
Snap::Snap(const QRect& rect, QGraphicsItem* parent, QGraphicsScene* scene) :
QGraphicsRectItem(rect)
Snap::Snap(const QRect& rect, QGraphicsItem* parent) :
QGraphicsRectItem(rect, parent)
您似乎也没有使用父参数和场景参数,因此您不妨将它们省略:
Snap::Snap(const QRect& rect) :
QGraphicsRectItem(rect)
或者,如果您计划将父对象用于某个对象,则可以在声明中将默认值设置为0:
Snap(const QRect& rect, QGraphicsItem* parent = 0);
然后将它们都传递给基类构造函数:
Snap::Snap(const QRect& rect, QGraphicsItem* parent, QGraphicsScene* scene) :
QGraphicsRectItem(rect)
Snap::Snap(const QRect& rect, QGraphicsItem* parent) :
QGraphicsRectItem(rect, parent)
snap.h
#ifndef SNAP_H
#define SNAP_H
#include <QGraphicsRectItem>
#include "cadgraphicsscene.h"
class Snap : public QGraphicsRectItem
{
public:
Snap(const QRect& rect, QGraphicsItem* parent,
QGraphicsScene* scene);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
QVariant itemChange(GraphicsItemChange change,
const QVariant &value);
private:
QPointF offset;
QPointF computeTopLeftGridPoint(const QPointF &pointP);
};
#endif // SNAP_H
\ifndef SNAP\u H
#定义SNAP_H
#包括
类快照:公共QGraphicsRecitem
{
公众:
捕捉(常量QRect&rect,QGraphicsItem*parent=0);
受保护的:
无效鼠标压力事件(QGraphicsSceneMouseEvent*事件);
QVariant itemChange(图形站点更改),
常数(变量和值);
私人:
QPointF偏移量;
QPointF计算点(常量QPointF&pointP);
};
#endif//SNAP_H
snap.cpp
#include "snap.h"
#include <QApplication>
Snap::Snap(const QRect& rect, QGraphicsItem* parent,
QGraphicsScene* scene):
QGraphicsRectItem(QRectF())
{
setFlags(QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemSendsGeometryChanges);
}
void Snap::mousePressEvent(QGraphicsSceneMouseEvent *event){
offset = pos() - computeTopLeftGridPoint(pos());
QGraphicsRectItem::mousePressEvent(event);
}
QVariant Snap::itemChange(GraphicsItemChange change,
const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
QPointF newPos = value.toPointF();
if(QApplication::mouseButtons() == Qt::LeftButton &&
qobject_cast<CadGraphicsScene*> (scene())){
QPointF closestPoint = computeTopLeftGridPoint(newPos);
return closestPoint+=offset;
}
else
return newPos;
}
else
return QGraphicsItem::itemChange(change, value);
}
QPointF Snap::computeTopLeftGridPoint(const QPointF& pointP){
CadGraphicsScene* customScene = qobject_cast<CadGraphicsScene*> (scene());
int gridSize = customScene->getGridSize();
qreal xV = floor(pointP.x()/gridSize)*gridSize;
qreal yV = floor(pointP.y()/gridSize)*gridSize;
return QPointF(xV, yV);
}
#包括“snap.h”
#包括
#包括
快照::快照(常量QRect和rect,QGraphicsItem*父项):
QGraphicsRecItem(矩形,父级)
{
setFlags(QGraphicsItem::ItemIsSelectable|
QGraphicsItem::ItemIsMovable|
QGraphicsItem::ItemSendsGeometryChanges);
}
void Snap::MousePresseEvent(QGraphicsSceneMouseEvent*事件){
偏移量=pos()-computeTopLeftGridPoint(pos());
QGraphicsRectItem::MousePresseEvent(事件);
}
QVariant Snap::itemChange(GraphicsSitemChange更改,
常数(变量和值)
{
qDebug()setLayout(新的QVBoxLayout);
QGraphicsView*视图=新的QGraphicsView(此视图);
centralWidget()->layout()->addWidget(视图);
捕捉*捕捉=新捕捉(QRect(0,0100));
查看->设置场景(新的QGraphicscene);
查看->场景()->添加项(捕捉);
}
MainWindow::~MainWindow()
{
删除用户界面;
}
尝试一些调试。查看它在itemChange
函数中的作用有多大。当我运行调试器时,它不会在任何地方停止。我无法找到问题所在。请帮助我。添加断点并开始逐步执行。很抱歉,我没有理解你,你能告诉我如何做吗?请帮助我。。。