What I have is similar to the following:
我所拥有的与以下类似:
int main(int argc, char **argv) {
QApplication app(argc, argv);
MainWindow appWindow;
appWindow.show();
return app.exec();
}
class MainWindow : public QMainWindow {
...
private:
QGraphicsScene *mScene;
QGraphicsView *mView;
QGraphicsItem *mItem;
QPushButton *mButton1, *mButton2;
};
MainWindow::MainWindow(...) {
mScene = new QGraphicsScene(this);
mScene->setItemIndexMethod(QGraphicsScene::NoIndex);
mView = new QGraphicsView(mScene, this);
mView->setAlignment(Qt::AlignLeft | Qt::AlignTop);
mButton1 = new QPushButton("Create Item", this);
QObject::connect(mButton1, SIGNAL(clicked()), ...);
mButton2 = new QPushButton("Set Item Position");
QObject::connect(mButton2, SIGNAL(clicked()), ...);
}
void MainWindow::button1Clicked() {
mItem = new QGraphicsSimpleTextItem("Test Item");
mItem->setPos(mItem->pos() + QPointF(7.0f, 7.0f)); // doesn't work
mScene->addItem(mItem);
// even when I move the setPos() call after QGraphicsScene::addItem,
// the item still paints at the top-left corner (0.0f, 0.0f)
}
void MainWindow::button2Clicked() {
mItem->setPos(mItem->pos() + QPointF(7.0f, 7.0f)); // works perfect
}
I'm very very new to Qt and most likely misunderstanding a basic concept. Can anyone spot what I'm doing wrong here?
我对Qt非常非常熟悉,而且很可能误解了一个基本概念。有人知道我做错了什么吗?
1 个解决方案
#1
5
From QGraphicsView
doc:
从QGraphicsView医生:
The visualized area is by default detected automatically when the view is displayed for the first time (by calling QGraphicsScene::itemsBoundingRect()).
当第一次显示视图时,默认情况下会自动检测到可视化区域(通过调用QGraphicsScene::itemsBoundingRect())。
That means when the view is first shown, it uses the combined item bounds as its bounds. So when you first added the item, whatever the item's position is, it will be used as the top-left of the displayed scene. So your item is actually moved, but the scene is shown with an offset. So it looks like it's at (0, 0). And when you move it the second time, it's actually moved twice already.
这意味着当视图第一次显示时,它使用组合的项边界作为其边界。因此,当您第一次添加该项目时,无论该项目的位置是什么,它将被用作显示场景的左上角。所以你的项目实际上是移动的,但是场景显示的是偏移量。所以它看起来是(0,0),当你第二次移动它时,它实际上已经移动了两次。
The solution is to set the seceneRect
to a known rect before showing it. That will fix the displayed area.
解决方案是在显示之前将seceneRect设置为已知的rect。这将修复显示的区域。
#1
5
From QGraphicsView
doc:
从QGraphicsView医生:
The visualized area is by default detected automatically when the view is displayed for the first time (by calling QGraphicsScene::itemsBoundingRect()).
当第一次显示视图时,默认情况下会自动检测到可视化区域(通过调用QGraphicsScene::itemsBoundingRect())。
That means when the view is first shown, it uses the combined item bounds as its bounds. So when you first added the item, whatever the item's position is, it will be used as the top-left of the displayed scene. So your item is actually moved, but the scene is shown with an offset. So it looks like it's at (0, 0). And when you move it the second time, it's actually moved twice already.
这意味着当视图第一次显示时,它使用组合的项边界作为其边界。因此,当您第一次添加该项目时,无论该项目的位置是什么,它将被用作显示场景的左上角。所以你的项目实际上是移动的,但是场景显示的是偏移量。所以它看起来是(0,0),当你第二次移动它时,它实际上已经移动了两次。
The solution is to set the seceneRect
to a known rect before showing it. That will fix the displayed area.
解决方案是在显示之前将seceneRect设置为已知的rect。这将修复显示的区域。