0

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

主窗口.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());
}

100 个矩形中只有 11 个可见

4

1 回答 1

2

造成问题的原因是您定义的sceneRect值与应有的值相比非常小,那就是QScrollBar作为引用SceneRect来确定其范围。所以解决方案很简单,不要设置任何sceneRect让 Qt 自己计算它,因为它会改变:

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

至:

m_scene = new QGraphicsScene(this);
于 2018-08-28T03:44:09.850 回答