简体   繁体   English

Qt:如何调整QGraphicsItem以动态更改大小

[英]Qt: How to adjust QGraphicsItem to dynamicaly change size

I've this problem for several days. 我这个问题好几天了。 I have created QGraphicsItem and I want to stretch/adjust it's size to size of my QGraphicsView. 我已经创建了QGraphicsItem,我想将其大小拉伸/调整到QGraphicsView的大小。 I was using paint() method, but got with it problems with updating. 我使用的是paint()方法,但存在更新问题。 Now I've used boundingRect() but it uses fixed size. 现在,我使用boundingRect(),但是它使用固定大小。 When I set too big size it expand my scene and scrollbars are appearing. 当我将尺寸设置得太大时,它会扩展场景并显示滚动条。 Is there way to adjust size of item to size of View? 有没有办法将项目的大小调整为View的大小?

EDIT : I want only adjust height of my object. 编辑 :我只想调整我的对象的高度。

Here's some code: 这是一些代码:

Header of my Item: 我的项目的标题:

#ifndef POINTER_H
#define POINTER_H
#include <QObject>
#include <QColor>
#include <QRect>
#include <QGraphicsLineItem>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>

class Pointer : public QGraphicsLineItem
{
public:
    Pointer();
    void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
    QRectF boundingRect() const;
    int position;
    void changePosition(int x);

};

#endif // TRACKPOINTER_H

Implementation of my Item: 我项目的实施:

#include "pointer.h"

Pointer::Pointer()
{
    //this->setFlag(QGraphicsLineItem::ItemIsMovable);
    //setFlag(QGraphicsLineItem::ItemIsFocusable);
    //setFocus();

}

void Pointer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

    QPen pen(Qt::red);
    painter->setPen(pen);
    painter->setBrush(QColor(77,77,77));
    painter->drawLine(0,0,0,2000);

}

QRectF Pointer::boundingRect() const
{
    return QRectF(0,0,2,600);
}

void Pointer::changePosition(int x)
{
    //position = x;
    setPos(x,0);
    update();
}

And my Window: 而我的窗口:

Window::Window(Timers *timer, TrackPointer *tp)
{
Timeline = new QGraphicsScene(this);
TimelineView = new QGraphicsView(Timeline);
TimelineView->setAlignment(Qt::AlignTop|Qt::AlignLeft);

QVBoxLayout *timeLineLayout = new QVBoxLayout;
timeLineLayout->addWidget(TimelineView);

Pointer *pointer = new Pointer;
Timeline->addItem(pointer);
}

I have also problems with my scene: When my object moves somewhere away - it expand scene. 我的场景也有问题:当我的物体移到某个地方时,它会扩展场景。 Later when I bring my object back to it's starting position scene still is expanded and I have scrollbars to scroll my view around the scene. 稍后,当我将对象放回其起始位置时,场景仍会展开,并且具有滚动条可在场景中滚动视图。 Is there way to decrease scene size using my object? 有没有办法使用我的物体减小场景大小?

The promised example looks something like this: 所承诺的示例如下所示:

MainWindow.h MainWindow.h

#include <QMainWindow>

class QGraphicsView;
class QGraphicsRectItem;

class MainWindow : public QMainWindow
{
    Q_OBJECT    
public:
    explicit MainWindow(QWidget *parent = nullptr);

    bool eventFilter(QObject *watched, QEvent *event) override;

private:
    QGraphicsView *m_view;
    QGraphicsRectItem *m_item;
};

MainWindow.cpp MainWindow.cpp

#include "MainWindow.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QEvent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_view(new QGraphicsView(this)),
    m_item(new QGraphicsRectItem(0, 0, 1, 1))
{
    m_view->setScene(new QGraphicsScene());
    m_view->setFrameStyle(QFrame::NoFrame);
    m_view->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    m_view->setSceneRect(0, 0, 1, 1);
    m_view->installEventFilter(this);
    m_view->scene()->addItem(m_item);

    setCentralWidget(m_view);
    resize(600, 400);
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if ((watched == m_view) && (event->type() == QEvent::Resize))
        m_item->setRect(m_view->viewport()->rect().adjusted(5, 5, -5, -5));

    return QMainWindow::eventFilter(watched, event);
}

look into :QGraphicsView::fitInView 看看:QGraphicsView :: fitInView

http://doc.qt.io/qt-4.8/qgraphicsview.html#fitInView http://doc.qt.io/qt-4.8/qgraphicsview.html#fitInView

QPixmap *pixMap= new QPixmap();;
QGraphicsScene *scene=new QGraphicsScene();;
pixMap->loadFromData(jpegData);
scene->clear();
pixMapItem = scene->addPixmap(*pixMap);
ui->graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
ui->graphicsView->show();
ui->graphicsView->viewport()->update();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM