English 中文(简体)
调整 QGraphicsView 不移动部件
原标题:Resize QGraphicsView doesn t move the widgets

我有一个QMainWindows, 内装QGraphics View 的 QMainWindow, 它包含所有客户区域。 在视图中, 我做了一些 QGraphicsTextTextTextTextTextText 项。 只要我不调整主窗口大小, 所有的东西都会工作。 如果我这样做, 物品会留在原处, 不移动到新的正确位置 。 我要做的基本上就是在布局部件中出现类似的行为, 如果需要的话会扩展

<强 > UPATE 1

""https://i.sstatic.net/XvISr.png" alt="此处输入图像描述"/ >

UPDATE 2

<强 > UPATE 3

按钮位于客户区右侧, 带有以下代码。 944 是QGraphicsView 的 Qt 设计师定义的宽度。 我对区域左侧的部件有相同的代码, 从 10 开始 xPos, 每次我添加新按钮时都会加号 。 如果我调整窗口的大小, 左边的部件总是移到新位置, 右侧没有相同的动作 。 为什么?

this->ui.setupUi(this);

qreal xPos = 944 - 10;

QGraphicsTextItem *myButton1 = new QGraphicsTextItem("my text");

myButton1 ->adjustSize();
myButton1 ->setPos(xPos - myButton1 ->textWidth(), 10);

xPos -= myButton1 ->textWidth();

this->m_graphicsScene->addItem(myButton1);


QGraphicsTextItem *myButton2 = new QGraphicsTextItem("my text 2");

myButton2 ->adjustSize();
myButton2 ->setPos(xPos - myButton2->textWidth(), 10);

xPos -= myButton2 ->textWidth();

this->m_graphicsScene->addItem(myButton2);
问题回答

QGriphicsView 与现场内的 QGriphicsTroit 对象没有直接联系。 它的唯一目的是在屏幕上直观您现场的一块片段, 并将用户的交互作用传送到现场。 没有布局, 但是增加了布局, 用于 QGriphics 部件的布局( 但这是个特殊需要的情况) 。 不论如何, 这仅与视图的翻译有关, 而不是与现场物品的位置有关( 完全不相干) 。

What you will most likely need to do, is subclass QGraphicsView, and re-implement resizeEvent in your view: http://qt-project.org/doc/qt-4.8/qgraphicsview.html#resizeEvent
In the resizeEvent, you would translate the view by an amount based on the resize: http://qt-project.org/doc/qt-4.8/qgraphicsview.html#translate

基本上,您正在自定义的 QGraphicsView 中创建自己的布局管理器功能 。

<强 > 更新

我理解你的意思,当场景矩形大于 QGraphicsView 时, 它会使用滚动条, 而不是将视图放在调整大小上。 理想的情况是, 如果您正在创建内容不需要滚动的图形场景类型, 您应该离开场景矩形默认值, 或者将其设置为视图的大小 。

否则,这里将是一个非常基本的例子, 说明您如何使用变整 Event 。 这是在 PyQt 中, 但我相信您可以翻译 :

class Dialog(QtGui.QDialog):

    def __init__(self):
        super(Dialog, self).__init__()
        self.resize(640,480)
        layout = QtGui.QVBoxLayout(self)

        self.scene = QtGui.QGraphicsScene(self)
        self.scene.setSceneRect(-500,-500,1000,1000)
        self.view = GraphicsView(self)
        self.view.setScene(self.scene)
        layout.addWidget(self.view)

        text = self.scene.addText("Foo")

        self.view.updateCenter()


class GraphicsView(QtGui.QGraphicsView):

    def __init__(self, parent=None):
        super(GraphicsView, self).__init__(parent)
        self._center = None

    def resizeEvent(self, event):
        super(GraphicsView, self).resizeEvent(event)
        if self._center:
            self.centerOn(self._center)

    def updateCenter(self):
        center = self.geometry().center()
        self._center = self.mapToScene(center)

它会保存您在调用 < code> updateCenter () 时感兴趣的场景点。 然后, 每次视图调整大小时, 它会回到该场景点 。

<强 > 更新2 :在图片发布和更新后

如果您在有布局的 QGraphicsSceen 中真的需要这种正常的 QWids 行为, 那么您应该查看支持布局的 QGraphics 部件。 然后您可以制作一个大的 root “ canvas” 部件, 并将所有其他部件都变成布局中的子元素。 然后您只需要担心根部件的大小是否正确。 这样您就可以使用上面的相同示例来管理根部件的大小和位置 。

Refer to this question for more details since its the same situation of someone wanting to integrate a QGraphicsItem into layouts:
QGraphicsWidget vs. QGraphicsLayoutItem





相关问题
Qt (Creator) with WinSocks (ws2_32)

I want to use an older code-fragment in my Qt-project, which is using WinSocks. I created my program with Qt Creator and I don t know, how I can link to the ws2_32-Library. I already added LIBS += -...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

PRECOMPILED_HEADER and Subdirs template on Qt

I have a big Qt project, splitted on several static libraries (around 70) and one application, and for that I m using a .pro file with the subdirs template. To speed up the compilations time, I want ...

how do i place QTableWidgetItem Icon in center of cell

i want a table cell to have just an icon without any text. i see the QTableWidgetItem class has a method to align the text (int QTableWidgetItem::textAlignment () const) i find no way to adjust the ...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Hiding a QWidget on a QToolbar?

I have directly added some QWidgets to a QToolbar but simply going widget->setVisible(false) did not work. Can someone please give me an example of how to show and hide a widget that is on a QToolbar? ...