如何滚动自定义QGraphicsWidgets的所有内容?

时间:2018-08-27 21:01:36

标签: c++ qt qt5 qgraphicsscene qgraphicswidget

我有一个名为“ TestContentArea”的自定义QGraphicsWidget,已添加到QGraphicsScene中。在我的自定义QGraphicsWidget中,我在其布局中添加了许多子QGraphicsWidgets。我遇到的问题是自定义QGraphicsWidget的内容超出了用户可以滚动的区域。下面随附的是显示此内容的最小示例。在100个中,只有11个矩形可见。在此示例中,如何滚动所有100个子窗口小部件?

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>

#include "TestContentArea.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    //Constructor
    explicit MainWindow(QWidget *parent = nullptr);
    //Deconstructor
    ~MainWindow();

private:
    //Private members
    Ui::MainWindow *ui;
    QGraphicsScene * m_scene;
    QGraphicsView * m_view;
    TestContentArea * m_testContentArea;
};

#endif // MAINWINDOW_H

MainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_scene = new QGraphicsScene(0, 0, 880, 630, this);

    m_view = new QGraphicsView(m_scene, this);
    m_view->setAlignment(Qt::AlignLeft | Qt::AlignTop);

    m_testContentArea = new TestContentArea();
    m_scene->addItem(m_testContentArea);

    setCentralWidget(m_view);
}

MainWindow::~MainWindow()
{
    delete ui;
}

TestContentArea.h:

#ifndef TESTCONTENTAREA_H
#define TESTCONTENTAREA_H

#include <QGraphicsWidget>
#include <QGraphicsLinearLayout>


class TestContentArea: public QGraphicsWidget
{
public:
    //Constructor
    TestContentArea(QGraphicsWidget* parent = nullptr);

private:
    //Private members
    QGraphicsLinearLayout * m_layout;

};

#endif // TESTCONTENTAREA_H


TestContentArea.cpp:

#include "TestContentArea.h"

#include <QGraphicsLinearLayout>
#include <QGraphicsRectItem>
#include "RectangleWidget.h"

TestContentArea::TestContentArea(QGraphicsWidget* parent)
{
    Q_UNUSED(parent);

    setAcceptHoverEvents(true);

    m_layout = new QGraphicsLinearLayout(Qt::Vertical, this);
    m_layout->setContentsMargins(30, 30, 0, 0);

    for(int i= 0; i< 100; i++)
    {
        RectangleWidget* rect = new RectangleWidget(this);
        m_layout->addItem(rect);

    }

    setLayout(m_layout);

}

RectangleWidget.h:

#ifndef Rectangle_H
#define Rectangle_H

#include <QGraphicsWidget>
#include <QJsonObject>
#include <QGraphicsLinearLayout>
#include <QGraphicsProxyWidget>
#include <QPushButton>

class RectangleWidget: public QGraphicsWidget
{
public:
    RectangleWidget(QGraphicsWidget* parent = nullptr);

    // Overriding functions
    QRectF boundingRect() const override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;

private:
    //Private member variables
    QGraphicsLinearLayout * m_layout;
    QPushButton * m_button;


};

#endif // Rectangle_H

RectangleWidget.cpp:

#include "RectangleWidget.h"
#include <QPainter>

RectangleWidget::RectangleWidget(QGraphicsWidget *parent)
{

}

void RectangleWidget::paint(QPainter *painter,
    const QStyleOptionGraphicsItem *option, QWidget *widget /*= 0*/)
{
    Q_UNUSED(widget);
    Q_UNUSED(option);

    //Draw border
    painter->drawRoundedRect(boundingRect(), 0.0, 0.0);
}

QRectF RectangleWidget::boundingRect() const
{

    return QRectF(QPointF(0,0), geometry().size());
}

Only 11 rectangles visible out of 100

1 个答案:

答案 0 :(得分:2)

引起此问题的原因是,您定义的sceneRect与其应有的值相比非常小,也就是说QScrollBarSceneRect作为参考来建立其范围。因此,解决方案很简单,请不要设置任何sceneRect以便Qt自行计算,因为它会更改:

m_scene = new QGraphicsScene(0, 0, 880, 630, this);

收件人:

m_scene = new QGraphicsScene(this);