gpt4 book ai didi

c++ - 需要帮助来理解 Qt 中的 sceneRect 是如何工作的

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:30 26 4
gpt4 key购买 nike

我是 Qt 新手。我阅读了教程,但仍然不明白 sceneRect 的工作原理。

当我运行这段代码时,图像(绿色)显示在中间(可能是默认位置,对吧?)

MyApplication.cpp

ui.setupUi(this);    

ui.graphicsView->setBackgroundBrush(Qt::red);
_graphicsScene = make_unique<QGraphicsScene>(this);
ui.graphicsView->setScene(_graphicsScene.get());

_graphicsScene->setBackgroundBrush(Qt::blue);

QImage img("C:\\Users\\user\\Downloads\\img.png");
auto pixImg = QPixmap::fromImage(img);
pixImg = pixImg.scaled(QSize(50, 50), Qt::KeepAspectRatio);

QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixImg);
_graphicsScene->addPixmap(pixImg);

enter image description here

ui_MyApplication.h

void setupUi(QMainWindow *MyApplicationToolClass)
{
if (MyApplicationToolClass->objectName().isEmpty())
MyApplicationToolClass->setObjectName(QString::fromUtf8("MyApplicationToolClass"));
MyApplicationToolClass->resize(465, 372);
centralWidget = new QWidget(MyApplicationToolClass);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
entitiesView = new QGraphicsView(centralWidget);
entitiesView->setObjectName(QString::fromUtf8("entitiesView"));
entitiesView->setGeometry(QRect(30, 20, 401, 281));
MyApplicationToolClass->setCentralWidget(centralWidget);
menuBar = new QMenuBar(MyApplicationToolClass);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 465, 21));
MyApplicationToolClass->setMenuBar(menuBar);
mainToolBar = new QToolBar(MyApplicationToolClass);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
MyApplicationToolClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(MyApplicationToolClass);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
MyApplicationToolClass->setStatusBar(statusBar);

retranslateUi(MyApplicationToolClass);

QMetaObject::connectSlotsByName(MyApplicationToolClass);
} // setupUi

我想如果我通过 _entitiesScene->setSceneRect(0, 0, 100, 100); 将 sceneRect 设置为 (0,0,100,100); 图像将显示在左上角,但它只是向上移动了一点。

enter image description here

我将 sceneRect 设置为 (50,50,100,100) 它只是向上和向左移动

enter image description here

最佳答案

简述scene的sceneRect,items的boundingRect:

QGraphicsItem::boundingRect :

This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be restricted to inside an item's bounding rect. QGraphicsView uses this to determine whether the item requires redrawing.

Although the item's shape can be arbitrary, the bounding rect is always rectangular, and it is unaffected by the items' transformation.

QGraphicsScene::sceneRect

This property holds the scene rectangle; the bounding rectangle of the scene

The scene rectangle defines the extent of the scene. It is primarily used by QGraphicsView to determine the view's default scrollable area, and by QGraphicsScene to manage item indexing.

If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).

场景矩形可能被显式设置。这将影响 View 中的渲染。如果场景声称不同于所有项目的边界矩形, View 将分别考虑这一点。

OP 的缺失部分是恕我直言,在渲染中还考虑了其​​他一些东西:

QGraphicsView::alignment

This property holds the alignment of the scene in the view when the whole scene is visible.

If the whole scene is visible in the view, (i.e., there are no visible scroll bars,) the view's alignment will decide where the scene will be rendered in the view. For example, if the alignment is Qt::AlignCenter, which is default, the scene will be centered in the view, and if the alignment is (Qt::AlignLeft | Qt::AlignTop), the scene will be rendered in the top-left corner of the view.)

为此,QGraphicsView 会考虑自己的 View 大小和场景矩形,并计算相应的平移以实现所需的对齐。翻译用于更新 QGraphicsView::transform它将场景坐标映射到视口(viewport)坐标。

一个小样本来说明这一点(testQGraphicsSceneSceneRect.cc):

#include <QtWidgets>

// returns a random floating point number in [min, max).
qreal randF(qreal min, qreal max)
{
return qrand() / (RAND_MAX + 1.0) * (max - min) + min;
}

// returns a random color.
QColor randColor()
{
return QColor(
(int)256 * randF(0.0, 1.0),
(int)256 * randF(0.0, 1.0),
(int)256 * randF(0.0, 1.0));
}

int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
QMainWindow qWin;
qWin.setWindowTitle("QSceneGraph::sceneRect Demo");
QToolBar qToolbar;
QAction qCmdAdd("Add Rect");
qToolbar.addAction(&qCmdAdd);
QAction qCmdClear("Clear Scene");
qToolbar.addAction(&qCmdClear);
qToolbar.addSeparator();
QWidget qEditAlign;
QGridLayout qGridAlign; QButtonGroup qBtnGrp;
QRadioButton qBtnTL, qBtnTC, qBtnTR;
qGridAlign.addWidget(&qBtnTL, 0, 0);
qBtnGrp.addButton(&qBtnTL, Qt::AlignLeft | Qt::AlignTop);
qGridAlign.addWidget(&qBtnTC, 0, 1);
qBtnGrp.addButton(&qBtnTC, Qt::AlignHCenter | Qt::AlignTop);
qGridAlign.addWidget(&qBtnTR, 0, 2);
qBtnGrp.addButton(&qBtnTR, Qt::AlignRight | Qt::AlignTop);
QRadioButton qBtnCL, qBtnCC, qBtnCR;
qGridAlign.addWidget(&qBtnCL, 1, 0);
qBtnGrp.addButton(&qBtnCL, Qt::AlignLeft | Qt::AlignVCenter);
qGridAlign.addWidget(&qBtnCC, 1, 1);
qBtnGrp.addButton(&qBtnCC, Qt::AlignHCenter | Qt::AlignVCenter);
qGridAlign.addWidget(&qBtnCR, 1, 2);
qBtnGrp.addButton(&qBtnCR, Qt::AlignRight | Qt::AlignVCenter);
QRadioButton qBtnBL, qBtnBC, qBtnBR;
qGridAlign.addWidget(&qBtnBL, 2, 0);
qBtnGrp.addButton(&qBtnBL, Qt::AlignLeft | Qt::AlignBottom);
qGridAlign.addWidget(&qBtnBC, 2, 1);
qBtnGrp.addButton(&qBtnBC, Qt::AlignHCenter | Qt::AlignBottom);
qGridAlign.addWidget(&qBtnBR, 2, 2);
qBtnGrp.addButton(&qBtnBR, Qt::AlignRight | Qt::AlignBottom);
qEditAlign.setLayout(&qGridAlign);
qToolbar.addWidget(&qEditAlign);
qWin.addToolBar(&qToolbar);
QGraphicsView qGView;
QGraphicsScene qGScene;
qGView.setScene(&qGScene);
qWin.setCentralWidget(&qGView);
qWin.show();
// activate button for default align of qGView
qBtnGrp.button(qGView.alignment())->setChecked(true);
// install signal handlers
QObject::connect(&qCmdAdd, &QAction::triggered,
[&]() {
// add rect
QRectF qRect(
randF(-50.0, 50.0), randF(-50.0, 50.0),
randF(10.0, 100.0), randF(10.0, 100.0));
qGScene.addRect(qRect, QPen(randColor(), 2), randColor());
// report
qDebug() << "new rect:" << qRect;
qDebug() << "scene rect:" << qGScene.sceneRect();
qDebug() << "scene (0, 0) in view at:"
<< qGView.mapFromScene(QPointF(0.0, 0.0));
qDebug() << "top left view corner in scene at:"
<< qGView.mapToScene(QPoint(0, 0));
});
QObject::connect(&qCmdClear, &QAction::triggered,
[&]() {
// clear scene
qGScene.clear();
// reset sceneRect
qGScene.setSceneRect(QRect());
});
QObject::connect(&qBtnGrp,
(void(QButtonGroup::*)(int, bool))&QButtonGroup::buttonToggled,
[&](int id, bool checked) {
if (checked) qGView.setAlignment((Qt::Alignment)id);
});
// run time loop
return app.exec();
}

对应的Qt工程(testQGraphicsSceneSceneRect.pro):

SOURCES = testQGraphicsSceneSceneRect.cc

QT += widgets

cygwin64 中编译和测试:

$ qmake-qt5 testQGraphicsSceneSceneRect.pro

$ make && ./testQGraphicsSceneSceneRect
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQGraphicsSceneSceneRect.o testQGraphicsSceneSceneRect.cc
g++ -o testQGraphicsSceneSceneRect.exe testQGraphicsSceneSceneRect.o -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
Qt Version: 5.9.4
new rect: QRectF(-4.13499,25.5605 21.8384x10.0007)
scene rect: QRectF(-5.13499,24.5605 23.8384x12.0007)
scene (0, 0) in view at: QPoint(120,3)
top left view corner in scene at: QPointF(-120,-3)
new rect: QRectF(-46.5428,33.0965 56.7475x44.5152)
scene rect: QRectF(-47.5428,24.5605 66.2462x54.0512)
scene (0, 0) in view at: QPoint(141,-17)
top left view corner in scene at: QPointF(-141,17)

Snapshot of testQGraphicsSceneSceneRect (two rects added)

之后,我通过鼠标点击改变了对齐方式:

scene (0, 0) in view at: QPoint(47,-24)
top left view corner in scene at: QPointF(-47,24)

Snapshot of testQGraphicsSceneSceneRect (alignment changed to top left)

关于c++ - 需要帮助来理解 Qt 中的 sceneRect 是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55764337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com