我有一个图形视图,其中包含一个场景与一些图形项目。如何使这些图形项显示在场景的左侧而不是中心?
我已经考虑了很多想法,但都没有成功。
发布于 2013-04-09 19:00:11
最简单的方法是添加一个不可见的QGraphicsRectItem作为第一项。
然后添加其他项作为第一个项的子项。
第一个项目的中心是坐标的中心。
QGraphicsScene *scene = new QGraphicsScene(widget.graphicsView);
// ....
QGraphicsRectItem *frame = scene->addRect(-100,-100,100,100);
frame->setPen(Qt::NoPen);
// ....
// Add new items as children of frame
// Align them relative to their parent ^
|
|
-100,100 | 100,100
+-----------------------------+
| | |
| | |
| | |
| | |
| | |
| | |
| | |
---------------------------+------------------------>
| | |
| | |
| | |
| | |
| | |
| | |
| | |
+-----------------------------+
-100,100 | 100,-100
|要使其知道窗口大小,请覆盖以下方法:
class MainForm : public QMainWindow
{
//...
QGraphicsScene *scene; // Move it here
QGraphicsRectItem *frame; // Move it here
protected:
virtual void resizeEvent(QResizeEvent * event);
virtual bool event(QEvent * event);
virtual void resizeAction();
//...
};
void MainForm::resizeAction()
{
QRectF rect(field->rect());
widget.graphicsView->setSceneRect(rect);
widget.graphicsView->fitInView(rect, Qt::KeepAspectRatio);
}
void MainForm::resizeEvent(QResizeEvent * event)
{
resizeAction();
QMainWindow::resizeEvent(event);
}
bool MainForm::event(QEvent * event)
{
if (event->type() == QEvent::Show)
resizeAction();
return QMainWindow::event(event);
}发布于 2013-11-10 15:49:16
Scene_item.setSceneRect(x0,y0,x1,y1);https://stackoverflow.com/questions/15899956
复制相似问题