Results 1 to 20 of 28

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,359
    Thanks
    3
    Thanked 5,015 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,359
    Thanks
    3
    Thanked 5,015 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,359
    Thanks
    3
    Thanked 5,015 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,359
    Thanks
    3
    Thanked 5,015 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,359
    Thanks
    3
    Thanked 5,015 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.

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.