c++ - 如何在 QGraphicsScene 中放置 QFrame?

标签 c++ qt

我想在 QGraphicsScene 中按垂直顺序一一放置 3 个 QFrames。 我尝试使用 QHBoxLayout 和 QLayout 但什么也没有,QGraphicsScene 不接受 QLayout。

QFrame * top = new QFrame;
QFrame * middle = new QFrame;
QFrame * bottom = new QFrame;

top->setFrameStyle(QFrame::StyledPanel | QFrame::Plain);
middle->setFrameStyle(QFrame::StyledPanel | QFrame::Plain);
bottom->setFrameStyle(QFrame::StyledPanel | QFrame::Plain);

top->setLineWidth(1);
middle->setLineWidth(1);
bottom->setLineWidth(1);

top->setFixedWidth(400);
top->setFixedHeight(150);
middle->setFixedWidth(400);
middle->setFixedHeight(250);
bottom->setFixedWidth(400);
bottom->setFixedHeight(150);

scene = new QGraphicsScene;
scene->setSceneRect(0, 0, 400, 550);
scene->addWidget(top);
scene->addWidget(middle);
scene->addWidget(bottom);

setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(400, 550);
setScene(scene);
show();

我应该怎么做才能让它发挥作用?

最佳答案

QFrame * top = new QFrame;
QFrame * middle = new QFrame;
QFrame * bottom = new QFrame;

top->setFrameStyle(QFrame::StyledPanel | QFrame::Plain);
middle->setFrameStyle(QFrame::StyledPanel | QFrame::Plain);
bottom->setFrameStyle(QFrame::StyledPanel | QFrame::Plain);

top->setLineWidth(1);
middle->setLineWidth(1);
bottom->setLineWidth(1);

scene = new QGraphicsScene;
scene->setSceneRect(0, 0, 400, 550);
QGraphicsWidget *topWidget = scene->addWidget(top);
QGraphicsWidget *midWidget = scene->addWidget(middle);
QGraphicsWidget *botWidget = scene->addWidget(bottom);

QGraphicsGridLayout *layout = new QGraphicsGridLayout;
layout->addItem(topWidget, 0, 0);
layout->addItem(midWidget, 1, 0);
layout->addItem(botWidget, 2, 0);

QGraphicsWidget *form = new QGraphicsWidget;
form->setLayout(layout);
form->setPreferredSize(400,500);
scene->addItem(form);


setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

setFixedSize(400, 550);
setScene(scene);
show();

enter image description here

关于c++ - 如何在 QGraphicsScene 中放置 QFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33485277/

相关文章:

c++ - 我可以在 C++ 中使用的最小嵌入式浏览器是什么?

c++ - sprintf 生成的字符串的值顺序与 Ubuntu 服务器上传递的参数顺序不同

c++ - 在 Linux 上编译外部库的问题

c++ - 如何从 QString 设置 Q_ENUM

python - PySide Qt : Auto vertical growth for TextEdit Widget, 和垂直布局中小部件之间的间距

c++ - Qt:未定义对 'vtable for MyClass' 的引用

qt - QList:内存不足

c++ - Qt 计算器问题 - C++

c++ - g++ ld 共享库错误代码:: block

c++ -/** 在 C++ 中是什么意思