[Solved]QGLWidget to QMainWindow
-
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 QMainWindowold 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 -
bq. application::create(){MainWindow m(_dummy_gl_widget, _settings;m.show();}
why create a MainWindow, then destroy it before it is visible?
-
And where you see it?
-
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. -
So i must use pointer for success?
-
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.
-
Header: http://pastebin.com/AWFCi5FJ
Source: http://pastebin.com/NgD5RuNsWrong at create Window
repair:
MainWindow* main_window(new MainWindow(world, _dummy_gl_widget, _settings)); -
In the header file you add the MainWindow pointer as in here:
@
class application : public QApplication
{
Q_OBJECTpublic:
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.
-
After i use edit and repair code it working...so thank for find bug.