Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

c++ - Shift key click in qt?

I am working on a paint program in QT5 C++ and I am trying to modify a function that draws a line to incorporate the 45 degree, horizontal or vertical special line that should be drawn if the shift key is clicked.

Below is what I have but for some reason the key handler is not working for me.

I received an error but I don't understand what I need to do to fix it attached below is the error and the code for the paint function that I've modified after that. I have wrapped the modification I did in comments for readability

Runtime error

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error is pretty clear.

QMouseEvent *mouse; - you declare a pointer to the a QMouseEvent, but where is it instantiated? This is only a pointer which points to something.

If you want to handle mouse events you probably have to overload some kind of widget's mouse event (mouseMoveEvent, mousePressEvent, etc.). Those will provide you a valid QMouseEvent input.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...