Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Fail to add view into addWidget
Forum Updated to NodeBB v4.3 + New Features

Fail to add view into addWidget

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 729 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by Chris Kawa
    #1

    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 ```.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved