QT QGraphicsView放大,缩小问题

时间:2017-05-29 05:16:39

标签: c++ qt qt5.8

我的工作环境: Qt 5.8 MSVC2015 64位,QT GraphicsView,Windows 7 64位

我的课程如下:

class MainWindow : public QMainWindow
class mySquare : public QGraphicsRectItem
class GraphicsView : public QGraphicsView
class Scene : public QGraphicsScene

我画了Sqaure里面的文字。 放大&缩小我使用下面的代码:

void GraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    //temp raise mouse movement signal start
    QPointF movment = mapToScene (event->pos());
    emit sendMousePoint(movment);

    if (_pan) // it get enable when control button is pressed.
    {
        horizontalScrollBar()->setValue(horizontalScrollBar()->value() - (event->x() - _panStartX));
        verticalScrollBar()->setValue(verticalScrollBar()->value() - (event->y() - _panStartY));
        _panStartX = event->x();
        _panStartY = event->y();
        event->accept();
     //   qDebug() <<"Mouse X" << QString::number(event->pos().x()) << " Y:" << QString::number(event->pos().y());
        return;
    }
    //temp raise mouse movement signal end
    event->ignore();
    QGraphicsView::mousePressEvent(event);

}

但是当我放大&amp;它失去了焦点&amp;创建白色空白屏幕。

任何线索我在放大&amp;出代码?

1 个答案:

答案 0 :(得分:0)

我搞砸了mySquare :: paint code的代码。

这是我的固定代码:

void mySquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget);
    painter->save();
    painter->setPen(Qt::darkBlue);
    painter->setBrush(Qt::NoBrush);
    const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());

    if((lod>_minlod) && (lod<=_maxlod))
    {

    }
    else
    {
        QRectF rect(0,0,_width,_height);
        painter->drawRect(rect);
        return;
    }

    if((_level==0) && (_row==0) && (_col==0))
    {
        qDebug()<<" Level: "<<_level<<" lod:"<<lod;
    }
    QRectF rect(0,0,_width,_height);
    painter->drawRect(rect);
    QString text1= QString("I am at level : %1 , Row: %2, Col : %3").arg(_level).arg(_row).arg(_col);
    painter->drawText( QRectF(40, 40, _width -20, _height -20),text1);
    painter->restore();