간단한 앵커 레이아웃 예시

그래픽 보기 장면에서 앵커 레이아웃을 보여줍니다.

간단한 앵커 레이아웃 예제는 QGraphicsAnchorLayout 클래스의 기본 사용법을 보여줍니다.

이 예제는 QGraphicsScene (scene), 3개의 위젯 (a, b, c) 및 QGraphicsAnchorlayout (layout)을 생성하는 것으로 시작합니다.

    QGraphicsScene scene;

    Widget *a = new Widget(Qt::blue, Qt::white, "a");
    a->setPreferredSize(100, 100);
    Widget *b = new Widget(Qt::green, Qt::black, "b");
    b->setPreferredSize(100, 100);
    Widget *c = new Widget(Qt::red, Qt::black, "c");
    c->setPreferredSize(100, 100);

    QGraphicsAnchorLayout *layout = new QGraphicsAnchorLayout;

먼저 a 항목의 왼쪽 상단 모서리를 layout 의 왼쪽 상단 모서리에 고정합니다. 이 작업은 두 단계로 수행할 수 있습니다:

    layout->addAnchor(a, Qt::AnchorTop, layout, Qt::AnchorTop);
    layout->addAnchor(a, Qt::AnchorLeft, layout, Qt::AnchorLeft);

또는 한 단계로 할 수도 있습니다:

    layout->addCornerAnchors(a, Qt::TopLeftCorner, layout, Qt::TopLeftCorner);

그런 다음 a 의 오른쪽 앵커를 b 의 왼쪽 앵커에 앵커링하고 b 항목의 상단을 a 의 하단에 앵커링합니다.

    layout->addAnchor(b, Qt::AnchorLeft, a, Qt::AnchorRight);
    layout->addAnchor(b, Qt::AnchorTop, a, Qt::AnchorBottom);

위젯 b 아래에 세 번째 위젯 c 을 배치합니다:

    layout->addAnchor(b, Qt::AnchorBottom, c, Qt::AnchorTop);

bc 항목은 서로 가로로 앵커링됩니다:

    layout->addAnchors(b, c, Qt::Horizontal);

항목 c는 오른쪽 하단에 앵커링됩니다. layout

    layout->addCornerAnchors(c, Qt::BottomRightCorner, layout, Qt::BottomRightCorner);

마지막으로 QGraphicsWidget wQGraphicsView view 에 표시됩니다.

    auto w = new QGraphicsWidget(nullptr, Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    w->setPos(20, 20);
    w->setMinimumSize(100, 100);
    w->setPreferredSize(320, 240);
    w->setLayout(layout);
    w->setWindowTitle(QApplication::translate("simpleanchorlayout", "QGraphicsAnchorLayout in use"));
    scene.addItem(w);

    QGraphicsView view;
    view.setScene(&scene);
    view.setWindowTitle(QApplication::translate("simpleanchorlayout", "Simple Anchor Layout"));

    view.resize(360, 320);
    view.show();

    return app.exec();

예제 프로젝트 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.