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. [Solved]QGLWidget to QMainWindow
Forum Updated to NodeBB v4.3 + New Features

[Solved]QGLWidget to QMainWindow

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 6.9k Views 1 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.
  • G Offline
    G Offline
    glararan
    wrote on last edited by
    #1

    Hi, i trying to create MainWindow based on QMainWindow with centralwidget as QWidget and layout with QHBoxLayout with widget QGLWidget.

    And problem? When i running open it it flashes and close app. I dont know why.

    In old: I create MapView(contain GLWidget[myOwn] + widget[NULL]) in QApplication
    New: I create MapView(contain GLWidget[myOwn] + widget[myOwn]) in QMainWindow

    old code:
    @application::create(){MapView* map(new MapView(_dummy_gl_widget, NULL);map->show();}@

    new code:
    @application::create(){MainWindow m(_dummy_gl_widget, _settings;m.show();}

    MainWindow::MainWindow(QGLWidget* _dummy_gl_widget, QSettings* _settings)
    {
    setWindowTitle("Application");

    QWidget* centralWidget (new QWidget);
    setCentralWidget(centralWidget);
    
    MapView* map_view(new MapView (_dummy_gl_widget, centralWidget));
    

    QScrollArea glWidgetArea (new QScrollArea);
    glWidgetArea.setWidget(map_view);
    glWidgetArea.setWidgetResizable(true);
    glWidgetArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    glWidgetArea.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    glWidgetArea.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    glWidgetArea.setMinimumSize(480, 480);

    QHBoxLayout* widgetLayout (new QHBoxLayout);
    widgetLayout->addWidget(_dummy_gl_widget);
    centralWidget->setLayout(widgetLayout);
    }@

    I can't find fault :(
    It must be little error

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      bq. application::create(){MainWindow m(_dummy_gl_widget, _settings;m.show();}

      why create a MainWindow, then destroy it before it is visible?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        glararan
        wrote on last edited by
        #3

        And where you see it?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          That seem to be your problem.
          in the old code:
          [quote author="glararan" date="1334073742"]
          @application::create()
          {
          MapView* map(new MapView(_dummy_gl_widget, NULL);
          map->show();
          }@
          [/quote]
          you have pointer. You create your view with new. But as soon as you leave your create method. The pointer is lost and you have a memory leak. Since the memory is not overwritten directly, it may work.

          in the new code:
          @
          application::create()
          {
          MainWindow m(_dummy_gl_widget, _settings;
          m.show();
          }
          @
          Here the window is destroy immediately when you are exiting the method.
          Your first approach with a pointer is better, but you need to have the pointer part of member values in the class.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • G Offline
            G Offline
            glararan
            wrote on last edited by
            #5

            So i must use pointer for success?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              Well, if the object does not outlive your method where you create it, it may be too short living as in your case.
              But again, it is only by accident, that it seems to run perfectly because of your memory leaking.

              It would not be the right thing to do using always pointers.

              Please post the header file and source file of application.

              Presumably it could be beneficial for you to look into a tutorial on C++ memory management.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • G Offline
                G Offline
                glararan
                wrote on last edited by
                #7

                Header: http://pastebin.com/AWFCi5FJ
                Source: http://pastebin.com/NgD5RuNs

                Wrong at create Window

                repair:
                MainWindow* main_window(new MainWindow(world, _dummy_gl_widget, _settings));

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  In the header file you add the MainWindow pointer as in here:
                  @
                  class application : public QApplication
                  {
                  Q_OBJECT

                  public:
                  application (int& argc, char** argv);
                  ~application();

                  MainWindow* _main_window;    // <<<   pointer added
                  QGLWidget* _dummy_gl_widget;
                  

                  @

                  in the source file you should destroy the allocated memory in the destructor.
                  @
                  application::~application()
                  {
                  delete _main_window;
                  }
                  @

                  the create routine is assigning memory for the MainWindow to your pointer.
                  @
                  void application::create_main_window()
                  {
                  _main_window = new MainWindow (_dummy_gl_widget, _settings); // <<<< this creates the main window
                  @

                  Now the pointer is surviving the end of the create routine.

                  Note: This is just typed into the code you provided. Not tested not compiled. I guess it will work. But it is easy to overlook problems.

                  Again I would like to recommend some tutorials on C++ programming. You need a sound background before you are starting larger Qt projects. Otherwise you will be fast beyond a point someone will see your problems on a short glance.

                  Also you should follow some basic advise on separation of declarations (header files *.h) and implementation (source files *.cpp). What you have done is possible and legal, but separation will save you a lot of trouble in the future.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    glararan
                    wrote on last edited by
                    #9

                    After i use edit and repair code it working...so thank for find bug.

                    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