如何用qt5在按键上旋转45度线?

时间:2017-03-19 05:31:12

标签: c++ qt qt5 qpainter

当我按住shift键时,我试图将一条线旋转45度或水平或垂直。

使用github linked here上的开源简易绘画项目在ct ++上的qt5中完成。

我的问题是我无法将其旋转45度。我已经能够垂直和水平旋转。

此问题被标记为我在此处询问有关点击处理程序的另一个问题的副本:Shift key click in qt?

我的问题不再是点击处理程序,因为该问题已得到解答,接受和解决。 此问题不重复

我现在的问题是轮流逻辑为45度。

我的代码如下:

void LineInstrument::paint(ImageArea &imageArea, bool isSecondaryColor, bool)
{
    QPainter painter(imageArea.getImage());
    if(isSecondaryColor)
    {
        painter.setPen(QPen(DataSingleton::Instance()->getSecondaryColor(),
                            DataSingleton::Instance()->getPenSize() * imageArea.getZoomFactor(),
                            Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    }
    else
    {
        painter.setPen(QPen(DataSingleton::Instance()->getPrimaryColor(),
                            DataSingleton::Instance()->getPenSize() * imageArea.getZoomFactor(),
                            Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    }

    if(mStartPoint != mEndPoint) // here is where the line is drawn 
    {
        int deltaX, deltaY;
        // my modifications start here
        if (QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier)) { // check if shift key is active
            // Compute change in x and change in y with absolute value to prevent faulty logic
            deltaX = abs(mEndPoint.x() - mStartPoint.x());
            deltaY = abs(mEndPoint.y() - mStartPoint.y());

            if (deltaX > deltaY){
                // transform to a horizontal line
                mEndPoint.setY(mStartPoint.y()); // rotate 180 making a horizontal line
            }
            else if (deltaX < deltaY){
                // transform to a vertical line 
                mEndPoint.setX(mStartPoint.x()); // rotate 90 making a vertical line
            }
            else if(deltaX == deltaY){
                // transform to a 45 degree line
                double pi = acos(-1); // Initialize pi
                double angle = 45 / 180.0 * pi; // sets angle to 45 degrees but in radians
                double LineLength = sqrt((pow(deltaX, 2) + pow(deltaY, 2))); // finds the distance of the line
                mEndPoint.setX(cos(angle)*LineLength + mStartPoint.x()); 
                mEndPoint.setY(sin(angle)*LineLength + mStartPoint.y());
            }
            else{
                // Not a special line do nothing
            }
            painter.drawLine(mStartPoint, mEndPoint); // let the line be drawn
        }// and end here
        painter.drawLine(mStartPoint, mEndPoint); // draw normal line if shift is not pressed 
    }

    if(mStartPoint == mEndPoint)
    {
        painter.drawPoint(mStartPoint);
    }
    imageArea.setEdited(true);
        //int rad(DataSingleton::Instance()->getPenSize() + round(sqrt((mStartPoint.x() - mEndPoint.x()) *
         //                                                            (mStartPoint.x() - mEndPoint.x()) +
          //                                                          (mStartPoint.y() - mEndPoint.y()) *
           //                                                          (mStartPoint.y() - mEndPoint.y()))));
      //mPImageArea->update(QRect(mStartPoint, mEndPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
    painter.end();
    imageArea.update();
}

1 个答案:

答案 0 :(得分:-1)

令人惊讶的是,我的代码工作正常。只是它能够以45度正确旋转,相同的坐标必须精确且精确。

我的演示很难模拟,但它可以正常工作。