【发布时间】: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();
}
【问题讨论】: