为什么我的 QGraphicsView 没有显示在 Qt4 的主窗口中?
这可能是非常明显的事情,但我对 Qt 很陌生,无法弄清楚。我有一个简单的主窗口,它有一个按钮。单击该按钮时,我想创建一个 QGraphicsScene,添加几行,然后在窗口中显示。但是,当我在窗口中运行此代码时,它不会显示。
但是,如果我将其作为 QApplication 运行,它会显示得很好。我缺少什么?
这是主窗口中的代码:
void TheDrawings::drawScene() {
qDebug() << "Setting up Scene";
QGraphicsScene scene(QRect(-50, -50, 400, 200));
QPen pen(Qt::red, 3, Qt::DashDotDotLine);
QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRect(-50, -50, 400,
200), 0, &scene);
rectItem->setPen(pen);
rectItem->setBrush(Qt::gray);
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(
"Amit Bahree", 0, &scene);
textItem->setPos(50, 0);
QGraphicsEllipseItem *eclipseItem = new QGraphicsEllipseItem(QRect(170, 20,
100, 75), 0, &scene);
eclipseItem->setPen(QPen(Qt::darkBlue));
eclipseItem->setBrush(Qt::darkBlue);
QGraphicsPolygonItem *polygonItem = new QGraphicsPolygonItem(QPolygonF(
QVector<QPointF> () << QPointF(10, 10) << QPointF(0, 90)
<< QPointF(40, 70) << QPointF(80, 110) << QPointF(70, 20)),
0, &scene);
polygonItem->setPen(QPen(Qt::darkGreen));
polygonItem->setBrush(Qt::yellow);
qDebug() << "Setting up the view";
QGraphicsView view;
view.setScene(&scene);
view.show();
}
This is probably something very obvious, but I have a new to Qt and can't figure it out. I have a simple MainWindow which has one button. When that button is clicked I want to create a QGraphicsScene, add a few lines and then show that in the Window. However when I run this code in a Window it does not show up.
BUT, if I run this as a QApplication, it shows up just fine. What am I missing?
Here is the code in the MainWindow:
void TheDrawings::drawScene() {
qDebug() << "Setting up Scene";
QGraphicsScene scene(QRect(-50, -50, 400, 200));
QPen pen(Qt::red, 3, Qt::DashDotDotLine);
QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRect(-50, -50, 400,
200), 0, &scene);
rectItem->setPen(pen);
rectItem->setBrush(Qt::gray);
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(
"Amit Bahree", 0, &scene);
textItem->setPos(50, 0);
QGraphicsEllipseItem *eclipseItem = new QGraphicsEllipseItem(QRect(170, 20,
100, 75), 0, &scene);
eclipseItem->setPen(QPen(Qt::darkBlue));
eclipseItem->setBrush(Qt::darkBlue);
QGraphicsPolygonItem *polygonItem = new QGraphicsPolygonItem(QPolygonF(
QVector<QPointF> () << QPointF(10, 10) << QPointF(0, 90)
<< QPointF(40, 70) << QPointF(80, 110) << QPointF(70, 20)),
0, &scene);
polygonItem->setPen(QPen(Qt::darkGreen));
polygonItem->setBrush(Qt::yellow);
qDebug() << "Setting up the view";
QGraphicsView view;
view.setScene(&scene);
view.show();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 QGraphicsView 需要将主窗口的中央小部件(或您想要将其放在其上的任何小部件)设置为父窗口。此外,您还需要“新”视图和场景对象以将它们放在堆上,这样一旦drawScene完成它们就不会被破坏。查看对代码的以下更改对您来说效果很好:
希望这有帮助,问候
your QGraphicsView needs a centralwidget of the mainwindow (or whatever widget you want to put it on top of) to be set as a parent. Also you need to "new" your view and scene objects to put them on the heap so they don't get destroyed once drawScene finishes. See of following changes to your code would work fine for you:
hope this helps, regards