PDA

View Full Version : How to display widget in calling method, called from main



kaydknight
8th March 2007, 14:49
Hi, this is just a general question, I've been wondering, is it possible if I call a method from main which displays a widget.

Recently, I tried writing a simple code for quick coding. I wrote a main method, and decided to call another method to draw widgets. Problem is, the widget will not
stay and display it, even though I directed the widget to be shown (using widget->show()) in the calling method.

My guess is that main method is the only one that called QApplication and asked it to perform event exec(), waiting for exit signal or something, while the calling method is only executed once and on-the-go (no wait flag or anything). Can anyone explaina bit further on this?

Also, if I decided to code from main, and then calling in methods that draws widgets, how can I make them stay displayed? Is there any function to keep the widget being displayed?

Here is a sample code snippet:


#include <QApplication>
#include <QMainWindow>
#include <QPushButton>


void newWindow()
{
QMainWindow main;
main.show();
}


int main(int argc, char*argv[])
{
QApplication app(argc,argv);
return app.exec();
}


The problem with this code is that, the widget won't display at all, or more accurately, it'll display the widget when entering method newWindow, but when returning to main, it'll be destroyed.

Is there some way to make the QMainWindow still being displayed, even after returning to main method?

jpn
8th March 2007, 14:54
The "QMainWindow main" goes out of scope when the execution returns from newWindow(). You would have to create the main window on the heap (with keyword "new") to keep it's instance around. For example:


void newWindow()
{
QMainWindow* main = new QMainWindow;
main->setAttribute(QT::WA_DeleteOnClose);
main->show();
}

kaydknight
9th March 2007, 03:36
Thank you for the feedback, unfortunately, the widget in the calling method will still be destroyed after returning to main method. I don't think there is anyway to do it either.

The reason why I wanted to do this is because I've coded several methods, and wanted to call those methods in main. Some of the methods constructs a set of widgets.
The methods that did not require widgets display works fine, but those that do, experience this problem.

Hmm.. I think the only way to solve this. is for all those methods that constructs widgets inside to be created as classes instantiating QMainWindow or something.

jpn
9th March 2007, 05:00
Maybe it's just the problem with the example, but main() in the first post is not calling newWindow()? :)

Here are a couple of examples. Two windows appear with self-explanatory titles:


// main.cpp
#include <QApplication>
#include <QMainWindow>

void newWindow()
{
QMainWindow* window = new QMainWindow;
window->setWindowTitle("Created in newWindow()");
window->setAttribute(Qt::WA_DeleteOnClose);
window->show();
}

QMainWindow* createWindow()
{
QMainWindow* window = new QMainWindow;
window->setWindowTitle("Created in createWindow()");
window->setAttribute(Qt::WA_DeleteOnClose);
return window;
}

int main(int argc, char* argv[])
{
QApplication a(argc, argv);
newWindow();
QMainWindow* window = createWindow();
window->show();
return a.exec();
}

kaydknight
10th March 2007, 18:01
Opps! Silly me, I did not include newWindow in the first post.

Emmm, anyway, as per previous situation, I did call method newWindow from main, Just that it was still destroyed after returning to main.

But you know what's weird? I tried running your original solution again, and this time it worked!!! I don't know why. I suspect it must have something to do with not cleaning my previous built project solution (I use vc2005 express) and kept some of the old settings, or something else maybe :p

Well, the example you showed now works! I'm also able to add other widgets into the main widget. Thank you very much!