PDA

View Full Version : how to launch a wizard from the mainwindow's File Menu



pkjag
2nd September 2013, 14:00
hi all
i was wondering how i'd go about launching my wizard from the file menu of the mainwindow using the New action in the file menu, so that when i click the New action the wizard is successfully launched.

here's a snapshot of the main.cpp file


#include <QApplication>
#include <QTranslator>
#include <QLocale>
#include <QLibraryInfo>

#include "SkoolCalcWizard.h"
#include "mainwindow.h"

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


QApplication app(argc, argv);

#ifndef QT_NO_TRANSLATION
QString translatorFileName = QLatin1String("qt_");
translatorFileName += QLocale::system().name();
QTranslator *translator = new QTranslator(&app);
if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsP ath)))
app.installTranslator(translator);
#endif

MainWindow window;

window.show();

return app.exec();


And here's the part of the mainwindow where i've tried defining the newFile() action to suit my needs


void MainWindow::newFile()
{


SkoolCalcWizard *wizard = new SkoolCalcWizard;
return wizard->exec();


}

void MainWindow::createActions()
{
newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
}

your help will be much appreciated, thanks in advance!!!!! :):)

wysota
2nd September 2013, 22:44
The code seems fine apart the return call which tries to return a value from a function returning void. Also there is no method in QAction called setShortcuts() but there is one called setShortcut().

Also is there any message you are getting in the console? Like about missing slots or something similar?

pkjag
3rd September 2013, 10:44
Hi, so thanks for replying and the warning, i'm new here so will work on that.
I do get a compiler error about the return type, so i changed it to this:


int MainWindow::newFile()
{


SkoolCalcWizard wizard;
return wizard.exec();


}


And also updated the mainwindow.h, the code did run but when i clicked the File menu's New action in the mainwindow, the window immediately exits.
Notice i also replaced the pointer notation with just a normal object declaration, i don't know if this has a significant impact??
So i did this:


void MainWindow::newFile()
{


SkoolCalcWizard wizard;
wizard.show();


}

But then the program runs and does the same thing as above, it immediately exits once i click on the New action.
And no i don't get errors on missing slots

aamer4yu
3rd September 2013, 13:52
The window will no doubt exit immediately since you are creating it on stack.

Also mention what problem you are exactly getting.
We also need the declaration for class SkoolWizard, without which we can only make guesses...

wysota
3rd September 2013, 17:32
So i did this:


void MainWindow::newFile()
{


SkoolCalcWizard wizard;
wizard.show();


}

But then the program runs and does the same thing as above, it immediately exits once i click on the New action.

You create a local variable that goes out of scope once the function ends. While exec() is a blocking call that doesn't return until the dialog is closed, show() behaves differently -- it schedules an event to show the widget and returns immediately.

pkjag
3rd September 2013, 18:32
The code for the SkoolCalc wizard is very long like 500 lines and i'm nt sure it'll be practical posting it here cause it's tight.
I tested it before writing the mainwindow code and it runs perfectly
I did this:



#include <QApplication>
#include <QTranslator>
#include <QLocale>
#include <QLibraryInfo>

#include "SkoolCalcWizard.h"
#include "mainwindow.h"

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


QApplication app(argc, argv);

#ifndef QT_NO_TRANSLATION
QString translatorFileName = QLatin1String("qt_");
translatorFileName += QLocale::system().name();
QTranslator *translator = new QTranslator(&app);
if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsP ath)))
app.installTranslator(translator);
#endif

SkoolCalcWizard wizard;

wizard.show();

return app.exec();
}


All i can say is that i don't think its necessary, the itchy bit is how to interface my wizard with the mainwindow. But if you think otherwise please convince me.

wysota
3rd September 2013, 19:35
Is there a question here somewhere?

pkjag
3rd September 2013, 19:41
Yeah its at the beginning of the thread.

wysota
3rd September 2013, 20:49
So what is the meaning of post #6?

pkjag
3rd September 2013, 20:58
That was meant to address aamer4yu's concerns for me not posting the whole code.

wysota
3rd September 2013, 21:50
But that code differs from what you posted in #1. So which one is correct, which class is the main window and which is not?

Anyway, your vanishing window problem comes from the already mentioned twice problem that you are allocating the window on the stack that unwinds immediately after the function completes its course.

pkjag
3rd September 2013, 22:51
The code at #6 is meant to launch my SkoolCalcWizard and i wrote it way before i wrote the mainwindow.cpp code, its basically the execution part in main.cpp to confirm that my wizard actually runs. So i posted that to reassure aamer4yu that i needn't post the whole of my SkoolCalcWizard.cpp code here since the wizard's code is tight. It isn't really important in this context.
So the original question is at #1.

wysota
4th September 2013, 08:35
I have already told you that code from post #1 is correct apart from compilation issues and a memory leak. The first snippet from #3 is also fine provided that the exec method is a regular QDialog::exec() call.

pkjag
4th September 2013, 09:46
So what do i do exactly cause thats why am here, that's where i got stuck. I do not know how to resolve the memory leak problem??

wysota
4th September 2013, 14:21
Unless you post some minimal example reproducing the problem, we can't help you as the code you initially posted is not the source of the problem. If that's your actual code of course and not something that only resembles your code.

pkjag
4th September 2013, 16:23
If what i posted isn't the problem then what is? Because i don't really know how to go about anymore.

Like i said my wizard works fine, it launches yes and thats why i posted the code at #6. I am now looking for a way to launch it from my mainwindow when i click New from my mainwindow's File Menu.

The mainwindow also launches successfully and has all the File menu actions like New, Open, Save, Save As, etc defined but i chose to only post the definition for New implemented in MainWindow::newFile() here cause i thought i could change it to suit my needs.

I also showed how New is connected to newFile() in the function MainWindow::createActions() which i originally posted in the first post.

So i really don't know what you want more, all i'm asking is if there's some alternative way i could change the portions of the code snippets i posted to suit my needs because that's where i am stuck, because there's a memory leak so my method is apparently wrong, isn't there an alternative.

Both the wizard and the mainwindow launch when you run one and then change the code to run the other, in main.cpp so how would you connect the two so that they launch the way i want ??

wysota
4th September 2013, 19:24
If what i posted isn't the problem then what is?
No idea, we're yet to see some compilable example reproducing the problem.


I am now looking for a way to launch it from my mainwindow when i click New from my mainwindow's File Menu.
Code in post #1 does that (once you fix compilation errors).


So i really don't know what you want more
We want a (1) minimal (i.e. max 50 lines of code), (2) compilable (i.e. creating a launchable executable) example (3) reproducing the problem (i.e. demonstrating what doesn't work).



all i'm asking is if there's some alternative way i could change the portions of the code snippets i posted
What you posted should work fine.


because there's a memory leak
Do you know what a memory leak is and how to fix one?

pkjag
4th September 2013, 20:41
I know what a memory leak is but can't figure out how to fix one.

I already modified code in #1 to #3 and so there's no compilation error just runtime error.
So seems like the memory leak is the culprit since the code compiles just fine it just doesn't produce output that behaves as well

wysota
4th September 2013, 20:49
I know what a memory leak is but can't figure out how to fix one.
Delete the object causing the leak once you don't need it anymore or use some mechanism (like Qt's parent-child relationship or the signal-slot mechanism together with deleteLater() slot) that will do that for you.


I already modified code in #1 to #3 and so there's no compilation error just runtime error.
So unmodify the code to have what you had in #1 minus compilation errors.


So seems like the memory leak is the culprit
So you don't know what a memory leak is after all, do you?

pkjag
4th September 2013, 21:05
Not in this context i just vaguely understand it like in assembly i.e a stack overflow.

What object is causing the leak and in what file should i delete it??

wysota
4th September 2013, 21:15
Not in this context i just vaguely understand it like in assembly i.e a stack overflow.
Stack overflow has nothing to do with memory leaks.


What object is causing the leak and in what file should i delete it??

http://lmgtfy.com?q=memory+leak

pkjag
5th September 2013, 15:27
So i figured that the object causing the memory leak is SkoolCalcWizard wizard but i don't understand why cause i already replaced the pointer notation with the normal object declaration in #3.

And i do use the object by calling show or return since i have already used both of these functions and they both make my program vanish.

So i'd like if you could spell it out for me like explicitly.

wysota
5th September 2013, 15:53
So i figured that the object causing the memory leak is SkoolCalcWizard wizard but i don't understand why cause i already replaced the pointer notation with the normal object declaration in #3.
... which causes your code (the second snippet, the first is fine) to fail because of what was posted in #4, #5 and #11.

I told you that your code from post #1 was correct apart the compilation errors and the memory leak. Code from #3 (the second snippet) is invalid because of the variable going out of scope.

pkjag
5th September 2013, 16:50
I think i'm starting to get the hang of this memory leak business. They've defined it as allocating memory and refusing to deallocate it or use it later. If you ask me they should've called it memory block, the name's got more sense. So how should i use the allocated memory to do what i want??

But i understand you saying i have a variable that's out of scope means that the wizard object is the variable. And being out of scope when the function ends is saying that the function is show() and because it returns immediately after it schedules an event it ends?? that's the tricky part that my mind can't wrap around.

So what's the difference between code at #1 and the correct code at #3 and which should i use??

wysota
5th September 2013, 17:01
I think i'm starting to get the hang of this memory leak business. They've defined it as allocating memory and refusing to deallocate it or use it later. If you ask me they should've called it memory block, the name's got more sense.
I don't know who is "they" but this looks like a "memory leak" and not a "memory block" to me:


for(int i=0;i<100;++i) new char[1000];


So how should i use the allocated memory to do what i want??
I don't have the slightest idea what you mean.


But i understand you saying i have a variable that's out of scope means that the wizard object is the variable. And being out of scope when the function ends is saying that the function is show() and because it returns immediately after it schedules an event it ends?? that's the tricky part that my mind can't wrap around.
By the variable going out of scope I mean exactly what is meant in C/C++ when saying about variables going out of scope.


So what's the difference between code at #1 and the correct code at #3 and which should i use??
Read my posts again, please. I really don't want to repeat myself again. I'm assuming you have some decent C++ skills so you should be able to handle that yourself. If not, maybe it's time to do some reading on C++. Qt really has nothing to do with this.

pkjag
5th September 2013, 17:15
I do understand C++ but you are being too vague. What variable is out of what function's scope?? since there are two functions there, show() and newFile(). You mentioned i have a memory leak problem but you haven't showed me how i fix that hence my question how i should use the allocated memory thats causing the leak. You previously mentioned sonething along the lines of slots and signals, i've about a months worth experience with Qt and i don't know how to use them in this situation. I don't know why its so difficult for you to help me with this memory leak situation.

wysota
5th September 2013, 21:54
I do understand C++ but you are being too vague. What variable is out of what function's scope??
If you understood C++ you wouldn't be asking that question.



int func() {
A a;
}

In the code above local variable "a" goes out of scope (its own scope, as in "it dies") when "func" function returns as the stack unwinds destroying all local variables.



You mentioned i have a memory leak problem but you haven't showed me how i fix

I did:

Delete the object causing the leak once you don't need it anymore or use some mechanism (like Qt's parent-child relationship or the signal-slot mechanism together with deleteLater() slot) that will do that for you.


that hence my question how i should use the allocated memory thats causing the leak.
Use it any way you want. The point is to seal the leak (aka delete the object) once you are done using it. That's really one of the most basic things in C++ and I'm sure that if you spent at least 10 hours learning that language, you should have been taught (or learnt yourself) to clean up memory once you are finished using it.



You previously mentioned sonething along the lines of slots and signals, i've about a months worth experience with Qt and i don't know how to use them in this situation. I don't know why its so difficult for you to help me with this memory leak situation.
You don't need any Qt experience for that. Here is an example:

Leak:

class A {
public:
A() { m_var = 0; }
void func() { m_var = new B; }
private:
B *m_var;
};

No leak:

class A {
public:
A() { m_var = 0; }
~A() { delete m_var; }
void func() { m_var = new B; }
private:
B *m_var;
};

Another example:

Leak:

class A {
public:
A() {}
void func() { B *var = new B; var->doSomething(); /* var not needed anymore */ }
};

No leak:

class A {
public:
A() { }
void func() { B var; var.doSomething(); /* var not needed anymore */ }
};

And another example, this time using Qt's facilities:

Leak:

class A : public QObject {
public:
A(QObject *parent =0) : QObject(parent) { }
void func() { B *var = new B; }
};

No leak:

class A : public QObject {
public:
A(QObject *parent =0) : QObject(parent) { }
void func() { B *var = new B(this); }
};

And one more:

Leak:

class A {
public:
A() { }
void func() { B *var = new B; }
};

No leak:

class A {
public:
A() { }
void func() { B *var = new B; var->deleteLater(); }
};

And yet another one:

Leak:

class A : public QObject {
public:
A(QObject *parent =0) : QObject(parent) { }
void func() { B *var = new B; }
};

No leak:

class A : public QObject {
public:
A(QObject *parent =0) : QObject(parent) { }
void func() {
B *var = new B(this);
connect(var, SIGNAL(finishedIamNotUsedAnymore()), var, SLOT(deleteLater()));
}
};

Let me spell it out again for you: fixing memory leaks means freeing up heap memory before you lose the last handle to that memory (using whatever mechanism you have at hand -- the delete operator, smart pointers or some kind of deferred deletion). This is basic C++ stuff and there are zillons or webpages mentioning "memory leaks" and instructions how to seal them, if in your prior programming education you have not encountered that term.

Up till now I was assuming you had programming skills, that's why I was telling you "fix the memory leak" and not "please delete the variable you are not using anymore and remember to always free up memory you are not using anymore". Apparently that was my mistake... I guess we have both learned something here.

pkjag
6th September 2013, 22:23
At last it worked!!!!
Thanks guys, especially wysota for being patient with me!!!'