Fail to add view into addWidget
-
Want to merge collidingmice and 2dpainting using GridLayout. Falled. Please advice
#include "glwidget.h" #include "widget.h" #include "window.h" #include <QtWidgets> #include <math.h> #include "mouse.h" #include <QGridLayout> #include <QLabel> #include <QTimer> static const int MouseCount = 7; Window::Window() { setWindowTitle(tr("2D Painting on Native and OpenGL Widgets")); // Widget *native = new Widget(&helper, this); GLWidget *openGL = new GLWidget(&helper, this); // QLabel *nativeLabel = new QLabel(tr("Native")); // nativeLabel->setAlignment(Qt::AlignHCenter); // QLabel *openGLLabel = new QLabel(tr("OpenGL")); // openGLLabel->setAlignment(Qt::AlignHCenter); qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); QGraphicsScene scene; scene.setSceneRect(-300, -300, 600, 600); scene.setItemIndexMethod(QGraphicsScene::NoIndex); for (int i = 0; i < MouseCount; ++i) { Mouse *mouse = new Mouse; mouse->setPos(::sin((i * 6.28) / MouseCount) * 200, ::cos((i * 6.28) / MouseCount) * 200); scene.addItem(mouse); } QGraphicsView view(&scene); view.setRenderHint(QPainter::Antialiasing); //view.setBackgroundBrush(QPixmap(":/images/cheese.jpg")); view.setCacheMode(QGraphicsView::CacheBackground); view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); view.setDragMode(QGraphicsView::ScrollHandDrag); view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Colliding Mice")); view.resize(400, 300); view.show(); QTimer timer; QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance())); timer.start(1000 / 33); QGridLayout *layout = new QGridLayout; layout->addWidget(view, 0, 0); layout->addWidget(openGL, 0, 1); // layout->addWidget(nativeLabel, 1, 0); // layout->addWidget(openGLLabel, 1, 1); setLayout(layout); QTimer *timer1 = new QTimer(this); //connect(timer, SIGNAL(timeout()), native, SLOT(animate())); connect(timer1, SIGNAL(timeout()), openGL, SLOT(animate())); timer1->start(50); }
[Edit: fixed code formatting, Chris Kawa]: please surround code with ```.
-
Hi,
Most of your widgets are stored on the stack so they'll get destroyed at the end of the constructor hence you won't see them.