Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: how to launch a wizard from the mainwindow's File Menu

  1. #1
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default how to launch a wizard from the mainwindow's File Menu

    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

    Qt Code:
    1. #include <QApplication>
    2. #include <QTranslator>
    3. #include <QLocale>
    4. #include <QLibraryInfo>
    5.  
    6. #include "SkoolCalcWizard.h"
    7. #include "mainwindow.h"
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11.  
    12.  
    13. QApplication app(argc, argv);
    14.  
    15. #ifndef QT_NO_TRANSLATION
    16. QString translatorFileName = QLatin1String("qt_");
    17. translatorFileName += QLocale::system().name();
    18. QTranslator *translator = new QTranslator(&app);
    19. if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
    20. app.installTranslator(translator);
    21. #endif
    22.  
    23. MainWindow window;
    24.  
    25. window.show();
    26.  
    27. return app.exec();
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void MainWindow::newFile()
    2. {
    3.  
    4.  
    5. SkoolCalcWizard *wizard = new SkoolCalcWizard;
    6. return wizard->exec();
    7.  
    8.  
    9. }
    10.  
    11. void MainWindow::createActions()
    12. {
    13. newAct = new QAction(tr("&New"), this);
    14. newAct->setShortcuts(QKeySequence::New);
    15. newAct->setStatusTip(tr("Create a new file"));
    16. connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
    17. }
    To copy to clipboard, switch view to plain text mode 

    your help will be much appreciated, thanks in advance!!!!!
    Last edited by wysota; 2nd September 2013 at 22:42.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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:
    Qt Code:
    1. int MainWindow::newFile()
    2. {
    3.  
    4.  
    5. SkoolCalcWizard wizard;
    6. return wizard.exec();
    7.  
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. void MainWindow::newFile()
    2. {
    3.  
    4.  
    5. SkoolCalcWizard wizard;
    6. wizard.show();
    7.  
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by pkjag; 3rd September 2013 at 11:01. Reason: reformatted to look better

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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...

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to launch a wizard from the mainwindow's File Menu

    Quote Originally Posted by pkjag View Post
    So i did this:
    Qt Code:
    1. void MainWindow::newFile()
    2. {
    3.  
    4.  
    5. SkoolCalcWizard wizard;
    6. wizard.show();
    7.  
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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:

    Qt Code:
    1. #include <QApplication>
    2. #include <QTranslator>
    3. #include <QLocale>
    4. #include <QLibraryInfo>
    5.  
    6. #include "SkoolCalcWizard.h"
    7. #include "mainwindow.h"
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11.  
    12.  
    13. QApplication app(argc, argv);
    14.  
    15. #ifndef QT_NO_TRANSLATION
    16. QString translatorFileName = QLatin1String("qt_");
    17. translatorFileName += QLocale::system().name();
    18. QTranslator *translator = new QTranslator(&app);
    19. if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
    20. app.installTranslator(translator);
    21. #endif
    22.  
    23. SkoolCalcWizard wizard;
    24.  
    25. wizard.show();
    26.  
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to launch a wizard from the mainwindow's File Menu

    Is there a question here somewhere?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to launch a wizard from the mainwindow's File Menu

    Yeah its at the beginning of the thread.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to launch a wizard from the mainwindow's File Menu

    So what is the meaning of post #6?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to launch a wizard from the mainwindow's File Menu

    That was meant to address aamer4yu's concerns for me not posting the whole code.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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??

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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 ??

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to launch a wizard from the mainwindow's File Menu

    Quote Originally Posted by pkjag View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to launch a wizard from the mainwindow's File Menu

    Quote Originally Posted by pkjag View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #20
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to launch a wizard from the mainwindow's File Menu

    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??

Similar Threads

  1. how to add my menu class to my MainWindow?
    By saman_artorious in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2012, 21:27
  2. Replies: 1
    Last Post: 29th September 2011, 11:14
  3. Launch app by opening a file
    By Windsoarer in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2009, 06:22
  4. Replies: 1
    Last Post: 13th May 2009, 03:18
  5. MainWindow Menu bug in Qt 4.2.3?
    By No-Nonsense in forum Qt Programming
    Replies: 4
    Last Post: 11th March 2007, 11:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.