简体   繁体   中英

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. I was using paint() method, but got with it problems with updating. Now I've used boundingRect() but it uses fixed size. 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?

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

#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

#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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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