Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QGraphicsPolygonItem拖动时不更新QPolygonF坐标_C++_Qt - Fatal编程技术网

C++ QGraphicsPolygonItem拖动时不更新QPolygonF坐标

C++ QGraphicsPolygonItem拖动时不更新QPolygonF坐标,c++,qt,C++,Qt,我有一个自定义版本的QGraphicsPolygonItem,定义如下: #ifndef CUSTOMGPOLYGON_H #define CUSTOMGPOLYGON_H #include <QObject> #include <QGraphicsPolygonItem> #include <string> #include <QGraphicsSceneMouseEvent> #include <QMenu> #include &

我有一个自定义版本的QGraphicsPolygonItem,定义如下:

#ifndef CUSTOMGPOLYGON_H
#define CUSTOMGPOLYGON_H

#include <QObject>
#include <QGraphicsPolygonItem>
#include <string>
#include <QGraphicsSceneMouseEvent>
#include <QMenu>
#include <QGraphicsTextItem>

class CustomGPolygon : public QObject, public QGraphicsPolygonItem
{
    Q_OBJECT
public:
    CustomGPolygon(QPolygonF poly, QObject *parent);
    ~CustomGPolygon();
    using QGraphicsPolygonItem::boundingRect;
    using QGraphicsPolygonItem::paint;

    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

    QGraphicsTextItem *className;

private slots:
    void deletePolygon();
    void copyPolygon();


signals:
    void duplicatePoly(QPolygonF);

private:
    QMenu menu;

};

#endif // CUSTOMGPOLYGON_H
当我拖动这个绘制的多边形时,我需要更新boundingRect中向量的坐标,而此时,它们没有这样做

我已尝试添加这些标志来解决此问题:

objectPt->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
objectPt->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);

但是问题仍然存在

项目中设置的QPolygonF与场景的坐标无关,而是与项目的坐标有关,因此移动项目不会更改QPolygonF。这与我们脸上的位置相似:如果道路是相对于世界而不是相对于我们自己移动的。因此,如果要获得与场景相关的多边形,必须使用mapToScene()方法进行转换。另一方面,如果要跟踪项目的位置,则不应使用mouseMoveEvent(),而应使用itemChange()

另一方面,您对点的计算是不正确的,您应该比较的是基于某些度量的距离,例如欧几里德距离,因为例如,根据您的逻辑,如果多边形位于负坐标位置,则边点将始终为(0,0)

考虑到上述情况,解决方案是:

#包括
类CustomGPolygon:PublicQObject,PublicQGraphicsPolyGonItem{
Q_对象
公众:
CustomGPolygon(QPolygonF多边形,QObject*parent=nullptr):
QObject(父对象)、QGraphicsPolygonItem(多边形)、类名(nullptr){
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemSendsGeometryChanges);
menu.addAction(“复制”、this和CustomGPolygon::copyPolygon);
addAction(“Delete”、this和CustomGPolygon::deletePolygon);
//如果(家长)
//连接(this,&CustomGPolygon::SIGNAL(duplicatePoly(QPolygonF))、父级、插槽(drawPolygonf));
}
~CustomGPolygon(){}
QGraphicsTextItem*getClassName()常量{return className;}
void setClassName(QGraphicsTextItem*值){className=value;}
受保护的:
QVariant项更改(GraphicsItemChange更改、常量QVariant和值){
if(change==GraphicsItemChange::ItemPositionChange&&!polygon().isEmpty()){
QPolygonF p=maptosene(polygon());
QPointF edgePoint=*std::max_元素(p.begin(),p.end(),
[](常数点F&x、常数点F&y)->bool
{
返回QVector2D(x).length()>QVector2D(y).length();
});
if(类名)
className->setPos(edgePoint);
}
返回QGraphicsPolygonItem::itemChange(更改,值);
}
专用Q_插槽:
void deletePolygon(){删除此;}
void copyPolygon(){
QPolygonF poly=maptosene(polygon());
Q_发射重复多边形(poly);
}
Q_信号:
多孔聚乙烯(QPolygonF);
私人:
QGraphicsTextItem*类名;
QMenu菜单;
};
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
QsCENE场景;
QGraphicsView视图(和场景);
QPolygonF-poly;

我知道原因但我无法提出解决方案,因为您没有提供解决方案MRE@eyllanesc我添加了一些详细信息。如果您还需要,请告诉我!谢谢您的帮助。您可以显示QPolygonF在拖动后将使用的代码吗?@EylanEsc已将其更新为完整函数。基本上,它从0,0获取最远的顶点并更新CustomGPoylgon拥有的QGraphicsTextItem的位置。然而,当我std::cout x和y作为坐标时,它们仍然是同一个人!非常感谢你的帮助。我真的非常感谢。我有两个问题要问你,尽管我已经完成并理解你实现了什么。首先,你为什么在t中做了所有的定义头文件?我被告知这是一种不好的做法,只有声明应该在其中。其次,你能用更简单的术语解释这行代码吗?
QPointF edgePoint=*std::max_元素(p.begin(),p.end(),[](const-QPointF&x,const-QPointF&y)->bool
。再次感谢!@wtreston 1)是的,这是一个很糟糕的做法,但我的想法是,我的答案是紧凑的,所以我选择后者。2)我建议你阅读C++文档:和lambda方法1。好的,我会尝试将它改为标题和主文件分开。2。谢谢你,我会去看看!
void MainWindow::drawPolygon(const QPolygonF &poly)
{
    CustomGPolygon *objectPt = new CustomGPolygon(poly, this);
    objectPt->setPen(pen);
    objectPt->setFlag(QGraphicsItem::ItemIsMovable);
    scene->addItem(objectPt);
    objectPt->className = textItem;
    map->drawing = false;
}
objectPt->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
objectPt->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);