【问题标题】:Shift key click in qt?在qt中点击Shift键?
【发布时间】:2017-03-19 02:01:07
【问题描述】:

我正在使用 QT5 C++ 编写一个绘图程序,并且我正在尝试修改一个绘制线条的函数,以合并在单击 shift 键时应绘制的 45 度、水平或垂直特殊线。

以下是我所拥有的,但由于某种原因,密钥处理程序不适合我。

我收到一个错误,但我不明白我需要做什么来修复它下面附上的是错误和我之后修改的绘制函数的代码。为了便于阅读,我已将我所做的修改包装在 cmets 中

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 
    {
        painter.drawLine(mStartPoint, mEndPoint); // let the line be drawn
        // my modifications start here
        if (QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) == true) { // check if shift key is active
            QMouseEvent *mouse;
            if (mouse->pos().x() > mouse->pos().y()){
                // transform to a horizontal line
                painter.save(); // save current painter state
                painter.rotate(180);
                painter.restore(); // restores painter state 

            }
            else if (mouse->pos().x() < mouse->pos().y()){
                // transfomr to a vertical line 
                painter.save();
                painter.rotate(90);
                painter.restore();
            }
            else{
                // transform to a 45 degree line
                painter.save();
                painter.rotate(45);
                painter.restore();
            }
        }// and end here

    }

    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();
}

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    错误很明显。

    QMouseEvent *mouse; - 你声明了一个指向 QMouseEvent 的指针,但它在哪里实例化?这只是一个指向某物的指针。

    如果您想处理鼠标事件,您可能必须重载某种小部件的鼠标事件(mouseMoveEventmousePressEvent 等)。这些将为您提供有效的 QMouseEvent 输入。

    【讨论】:

    • 我将如何重载它,使其在端点的时间点等于鼠标?
    • 好吧,我不知道你的代码库,所以我无法回答这个问题。
    • 一切的基础都可以在 git 上找到 github.com/Gr1N/EasyPaint/tree/master/sources/instruments
    • 由于 qpointer 的功能,我找到了一种解决方法。你确实指出了我的错误,所以我会接受它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 2012-12-02
    相关资源
    最近更新 更多