PDA

View Full Version : Open new window when button on main-window is clicked



SNK111
23rd December 2010, 02:59
Hi
I am trying to create a Nokia mobile application using Qt. It has two forms/windows (for now) . Window1 is shown below:
http://www.nagorik.org/qt/window1.gif
Fig: window1

When I click on the first button (‘Add’) in window1, I want it to open window2 shown below:
http://www.nagorik.org/qt/window2.gif
Fig: window2

Both were created using Qt Designer in Qt Creator.

Window1 is the MainWindow template and window2 is of Widget template.

I also have the following headers and classes. When I run the application, it builds with no errors and I can see the main window but when I click on the add button I don’t see window2.
CAN ANY ONE HELP ME IDENTIFY WHAT I AM DOING WRONG?

http://www.nagorik.org/qt/wt_h.gif
Fig3: main window header file

http://www.nagorik.org/qt/wt_cpp.gif
Fig4: main window class file

I could not attach the window2 header and class file due to the limit on attaching images but the full project can be found here: http://www.nagorik.org/qt/WT.rar

PLEASE HELP ME GET STARTED WITH THIS PROJECT.
Thanks in advance for your help.

ChrisW67
23rd December 2010, 04:06
For a start, post code in [code] tags, not as images.

You have an Add push button in the WT Designer UI that you completely ignore: this button does nothing as a result. You then create another push button labelled Add and show it as a top-level window (look carefully when you start the application) and connect it to your slot. When you click the free-standing Add button the second form is shown.

The second form shows overlaid in a messy fashion on the main form's content because you have given the second form the first form as parent: you probably don't want to do this if the second form is supposed to stand alone.

SNK111
23rd December 2010, 05:29
Thanks Chris for your reply.
I will certainly post code in [CODE] tags next time.

Regarding providing a parent, how can I prevent memory leaks if I don't provide the first form as a parent.
Some details on the concept of preventing memory leaks, in the context of multiple forms in Qt, would be very much appreciated.

Thanks again for your help.

ChrisW67
23rd December 2010, 06:55
This is just standard C++ memory management. It depends on where and how you use it. For windows that are to be short lived (e.g. modal QDialogs) then you can create them on the stack and they are automatically cleaned up as they go out of scope. If you allocate the memory on the heap with new then you must ensure that delete is called at some point. Usually the destructor of the allocating class is the correct place. Another option is to use one of the "smart" pointers: QScopedPointer or QSharedPointer.