如何在QGraphicsScene中放置QFrame?

时间:2015-11-02 19:24:24

标签: c++ qt

我希望在QGraphicsScene中按顺序逐个放置3个QFrame。 我尝试使用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();

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

1 个答案:

答案 0 :(得分:3)

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