Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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
Qt 如何将旋转/调整大小的行项目坐标(点P1)转换为本地坐标系?_Qt_Qgraphicsview_Qgraphicsitem - Fatal编程技术网

Qt 如何将旋转/调整大小的行项目坐标(点P1)转换为本地坐标系?

Qt 如何将旋转/调整大小的行项目坐标(点P1)转换为本地坐标系?,qt,qgraphicsview,qgraphicsitem,Qt,Qgraphicsview,Qgraphicsitem,我有一个自定义行项目,它是QGraphicsLineItem的子类。当行项目边缘发生鼠标移动事件时,我会相应地旋转或调整行的大小。我的问题是,在执行旋转或调整大小之前,行项目存在于具有P1(0,0)和P2(x,y)的局部坐标系中 当P2用作调整大小和旋转的锚点,并且将鼠标放置在P1上时,P1接受鼠标事件->pos()坐标。完成调整大小后,我需要将行项目转换为局部坐标系。我该怎么做?我尝试使用setTransformOriginPoint(event->pos()),但它无法将P1转换为原点 用于

我有一个自定义行项目,它是QGraphicsLineItem的子类。当行项目边缘发生鼠标移动事件时,我会相应地旋转或调整行的大小。我的问题是,在执行旋转或调整大小之前,行项目存在于具有P1(0,0)和P2(x,y)的局部坐标系中

当P2用作调整大小和旋转的锚点,并且将鼠标放置在P1上时,P1接受鼠标事件->pos()坐标。完成调整大小后,我需要将行项目转换为局部坐标系。我该怎么做?我尝试使用setTransformOriginPoint(event->pos()),但它无法将P1转换为原点

用于旋转/调整大小的代码如下所示:

void CustomGraphicsLineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug()<<"Line mouse move event";
    if( dragIndex != -1 )
    {
        const QPointF anchor = dragIndex == 0 ? this->line().p1() : this->line().p2();
        this->setLine(dragIndex == 0 ? QLineF(anchor, event->pos()) : QLineF(event->pos(),anchor));
    }
    if(dragIndex == 1)
    {
        this->setTransformOriginPoint(event->pos());
    }
    this->update();
}



 void CustomGraphicsLineItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        qDebug()<<"Line mouse press event";
        //  initialPos = mapToScene(event->pos());
        dragIndex = -1;
        const QPointF event_pos = event->pos();
        const qreal l1 = QLineF( event_pos, this->line().p1() ).length();
        const qreal l2 = QLineF( event_pos, this->line().p2() ).length();
        //threshold indicates the area of influence of the mouse click event, which is set to  5.0 pixels
        const qreal threshold = 15.0;

        if(l1 < threshold || l2 < threshold)
        {
            if( l1 < l2 && l1 < threshold )
            {
                dragIndex = 1;
            }
            else if ( l2 < l1 && l2 < threshold )
            {
                dragIndex = 0;
            }
            else
            {
                dragIndex = -1;
            }
            event->setAccepted( dragIndex != -1 );
        }

        //if we click anywhere other than the end points, then consider it to be a drag
        if(l1 > threshold && l2 > threshold)
        {
            QMimeData * mimeData = new QMimeData;
            CustomGraphicsLineItem * item = this;
            QByteArray byteArray(reinterpret_cast<char*>(&item),sizeof(CustomGraphicsLineItem*));
            mimeData->setData("Item",byteArray);

            // start the event
            QDrag * drag = new QDrag(event->widget());
            drag->setMimeData(mimeData);
            drag->exec();
            dragStart = event->pos();
            event->accept();
        } 

    }

void CustomGraphicsLineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    dragIndex = -1;
    QGraphicsLineItem::mouseReleaseEvent(event);
}

void CustomGraphicsLineItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{   
 // qDebug() << "CustomGraphicsLineItem::paint called ";
    QPen pen=QPen();
    pen.setColor(Qt::red);
    pen.setWidth(5);
    painter->setPen(pen);
    painter->setBrush(Qt::red);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->drawLine(line());
void CustomGraphicsLineItem::mouseMoveEvent(QGraphicsSceneMouseEvent*事件)
{
qDebug()行().p2();
this->setLine(dragIndex==0?QLineF(锚定,事件->定位()):QLineF(事件->定位(),定位));
}
if(dragIndex==1)
{
此->设置TransformOriginPoint(事件->位置());
}
此->更新();
}
void CustomGraphicsLineItem::MousePresseEvent(QGraphicsSceneMouseEvent*事件)
{
qDebug()位置();
const qreal l1=QLineF(事件位置,this->line().p1()).length();
const qreal l2=QLineF(事件位置,this->line().p2()).length();
//阈值表示鼠标单击事件的影响区域,设置为5.0像素
恒定阈值=15.0;
如果(l1<阈值| | l2<阈值)
{
如果(l1设置已接受(dragIndex!=-1);
}
/如果我们点击除终点以外的任何地方,然后认为它是一个阻力。
如果(l1>阈值和&l2>阈值)
{
QMimeData*mimeData=新QMimeData;
CustomGraphicsLineItem*项=此项;
QByteArray byteArray(重新解释铸件和项目),尺寸(自定义图形项目*);
mimeData->setData(“项目”,byteArray);
//启动活动
QDrag*drag=newqdrag(事件->小部件());
拖动->设置mimeData(mimeData);
拖动->exec();
dragStart=事件->位置();
事件->接受();
} 
}
void CustomGraphicsLineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent*事件)
{
dragIndex=-1;
QGraphicsLineItem::mouseReleaseEvent(事件);
}
void CustomGraphicsLineItem::paint(QPaint*油漆工、const QStyleOptionGraphicsItem*、QWidget*)
{   
//qDebug()设置笔(笔);
画家->挫折(Qt::红色);
painter->setRenderHint(QPainter::抗锯齿);
油漆工->拉丝(线());

}

什么是dragIndex?它在哪里更改其值?@mhcuervo-dragIndex是一个标志,用于标识鼠标事件发生的特定端点(选择),根据拖动索引,我们决定线旋转或调整大小的锚点。@mhcuervo:dragIndex=-1;const QPointF event_pos=事件->pos();const qreal l1=QLineF(事件位置,this->line().p1()).length();const qreal l2=QLineF(事件位置,this->line().p2()).length();//阈值表示鼠标点击事件的影响区域,设置为5.0像素const qreal threshold=15.0;if(l1设置接受(dragIndex!=-1)}你能编辑这个问题并包含
CustomGraphicsLineItem
类的全部代码吗?