PDA

View Full Version : New to QT, problem displaying main window...



McCall
15th June 2007, 11:43
Hi Folks!

Hopefully someone can help me with this newbie problem...

I am fairly new to Qt, and I have been using MiniGW, QDevelop, Qt Designer and Qt 4.3 under WindowsXP to try and build a "Playground" applciation that I can add things to while I learn Qt. I haven't really used Qt before, but I have used various C++ API's, most notibly the BeAPI from BeOS and Haiku. I also haven't programmed for a few years, so I am a little unsure if my problem is not knowing how to use Qt, or me forgetting how to program C++ period :)

I expect my application to show a QMainWindow that will stay on the screen until I press the [x] button, at this point it will close. What I am getting is a Window that flashes on the screen and closes straight away, leaving a process in the Windows process list (I think this is because I am not deleting my new in the relevent destructor)

I have 3 source files, 3 header files and a .UI file. The .UI file contains a Window with a QMainWIndow object called PlaygroundWindowGUI. If needed, I can send a zip file of my folder to anyone that would like to help.

Playground.h:

#ifndef __MAIN_H__
#define __MAIN_H__

// place your code here

#endif // __MAIN_H__

Playground.cpp:

#include <stdio.h>
#include "Playground.h"
#include "PlaygroundApplication.h"

int main(int argc, char *argv[])
{// Main

printf ("Entering main...\n");
PlaygroundApplication Playground(argc, argv);

return Playground.exec();

}// Main

PlaygroundApplication.h:

#ifndef __PLAYGROUNDAPPLICATION_H__
#define __PLAYGROUNDAPPLICATION_H__

#include <QApplication>

class PlaygroundApplication : public QApplication
{
public:
PlaygroundApplication(int argc, char *argv[]);

};

#endif // __PLAYGROUNDAPPLICATION_H__

PlaygroundApplication.cpp:

#include "PlaygroundApplication.h"
#include "PlaygroundWindow.h"

PlaygroundApplication::PlaygroundApplication(int argc, char *argv[]):QApplication(argc, argv)
{

PlaygroundWindow thisPlaygroundWindow;
thisPlaygroundWindow.connect( &thisPlaygroundWindow, SIGNAL( lastWindowClosed() ), &thisPlaygroundWindow, SLOT( quit() ) );
thisPlaygroundWindow.show();

}

PlaygroundWindow.h:

#ifndef __PLAYGROUNDWINDOW_H__
#define __PLAYGROUNDWINDOW_H__

#include "ui_playgroundwindowgui.h"

class PlaygroundWindow : public QMainWindow, public Ui::PlaygroundWindowGUI
{
Q_OBJECT
public:
PlaygroundWindow( QWidget * parent = 0, Qt::WFlags f = 0 );
private slots:
};

#endif // __PLAYGROUNDWINDOW_H__

PlaygroundWindow.cpp:

#include "PlaygroundWindow.h"

PlaygroundWindow::PlaygroundWindow( QWidget * parent, Qt::WFlags f)
: QMainWindow(parent, f)
{
setupUi(this);

}

Can someone please help me work out why this isn't displaying like I expect?

Thanks,

Andrew McCall.

gri
15th June 2007, 11:54
PlaygroundWindow thisPlaygroundWindow;
thisPlaygroundWindow.connect( &thisPlaygroundWindow, SIGNAL( lastWindowClosed() ), &thisPlaygroundWindow, SLOT( quit() ) );
thisPlaygroundWindow.show();

You create the MainWindow on the stack! It will be closed when the PlaygroundApplication constructor ends. Create your MainWindow with new or do it in the main()-function on the stack.

McCall
15th June 2007, 12:26
Thanks for the quick reply!

I have move the creation of the window from the stack, but its still not working. One thing I have noticed is that the printf isn't showing any output either, which I am 99.999% sure should be showing!

Thanks,

Andrew McCall

gri
15th June 2007, 12:36
You may take this as example


int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QMainWindow wnd;
wnd.show();
app.setActiveWindow(&wnd);

int ret = app.exec();

// cleanup whatever

return ret;
}

McCall
15th June 2007, 14:27
I think its QDevelop thats causing problems.

If I use qmake -project, qmake, make from the command line my application works.