Results 1 to 8 of 8

Thread: Qt show modal dialog (.ui) on menu item click

  1. #1
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    2

    Exclamation Qt show modal dialog (.ui) on menu item click

    I have created several menu items in Qt but I do not know how to connect them to to the qtdialog which I created
    for the menu items :
    mainwindow.h
    Qt Code:
    1. ........
    2.  
    3. void get_started_info();
    4.  
    5. private:
    6. Ui::MainWindow *ui;
    7. //! Create the menu classes
    8. void createActions();
    9. void createMenus();
    10. //! Menu Items
    11. QMenu *home;
    12. QMenu *help;
    13. QAction *quit;
    14. QAction *start;
    15.  
    16. };
    17.  
    18. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. void MainWindow::createActions()
    2. {
    3. QPixmap startpix("../src/img/start.png");
    4.  
    5. start = new QAction(startpix,"&Get Started",this);
    6. start->setStatusTip(tr("Learn how to use this program"));
    7. connect(start,SIGNAL(triggered()),qApp,SLOT(get_started_info()));
    8.  
    9. }
    10.  
    11. void MainWindow::createMenus()
    12. {
    13. home = menuBar()->addMenu(tr("&Home")); //! Adding a home item
    14. help = menuBar()->addMenu(tr("H&elp")); //! Adding a help item
    15. help->addAction(start); //! Adding About under the help
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    in the mainwindow.cpp I also have this class which should show the dialog
    Qt Code:
    1. void MainWindow::get_started_info()
    2. {
    3. get_started one(this); //create a object directly
    4. one.exec(); //modal dialog
    5. }
    To copy to clipboard, switch view to plain text mode 

    I also have get_started.h / get_started.cpp / get_strated.ui

    I think there is an issue with this lines :
    Qt Code:
    1. start = new QAction(startpix,"&Get Started",this);
    2. start->setStatusTip(tr("Learn how to use this program"));
    3. connect(start,SIGNAL(triggered()),qApp,SLOT(get_started_info()));
    To copy to clipboard, switch view to plain text mode 

    is it possible to show the dialog when the "Get Started" is clicked from the menu ?
    thank you

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt show modal dialog (.ui) on menu item click

    Quote Originally Posted by tomorrow
    is it possible to show the dialog when the "Get Started" is clicked from the menu ?
    Yes it is possible. In the the triggered Slot of the QAction you are using show the dialog you want.
    For example:
    Qt Code:
    1. void MainWindow::on_start_triggered()
    2. {
    3. one = new get_started(this); // make sure to have it as class member or use the exec() function if you create the instance in the slot without being a member.
    4. one->setModal(true); // if you need it.
    5. one->show();
    6. }
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by tomorrow
    Qt Code:
    1. connect(start,SIGNAL(triggered()),qApp,SLOT(get_started_info()));
    To copy to clipboard, switch view to plain text mode 
    why are you using qApp as a parent here ? your slot is in the current class so you should use this instead.

    Good Luck.

  3. The following user says thank you to toufic.dbouk for this useful post:

    tomorrow (16th November 2013)

  4. #3
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    2

    Default Re: Qt show modal dialog (.ui) on menu item click

    thank you for your response
    why are you using qApp as a parent here ? your slot is in the current class so you should use this instead.
    sorry I was following a tutorial and thats how they did it, I changed it to "this"

    Yes it is possible. In the the triggered Slot of the QAction you are using show the dialog you want.
    I tried the code and had this :
    Qt Code:
    1. void MainWindow::get_started_info()
    2. {
    3. one = new get_started(this);
    4. one->setModal(true); // if you need it.
    5. one->show();
    6.  
    7. //get_started one(this); //create a object directly
    8. //one.exec(); //modal dialog
    9. }
    To copy to clipboard, switch view to plain text mode 

    and used this to connect :
    Qt Code:
    1. start = new QAction(startpix,"&Get Started",this);
    2. start->setStatusTip(tr("Learn how to use this program"));
    3. connect(start,SIGNAL(triggered()),this,SLOT(get_started_info()));
    To copy to clipboard, switch view to plain text mode 

    but it says:
    - "one" was not declared in this scope
    - expected type-specified before 'get_started'
    - expected ';' before get_started

    here are the files for get_started

    get_started.h :
    Qt Code:
    1. #ifndef GET_STARTED_H
    2. #define GET_STARTED_H
    3.  
    4. #include <QDialog>
    5. #include "ui_get_started.h"
    6.  
    7. class get_started : public QDialog
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit get_started(QWidget *parent = 0);
    12.  
    13. private:
    14. Ui:: get_started *ui;
    15. };
    16.  
    17.  
    18. #endif // GET_STARTED_H
    To copy to clipboard, switch view to plain text mode 

    get_started.cpp
    Qt Code:
    1. #include "get_started.h"
    2. #include "ui_get_started.h"
    3.  
    4. get_started::get_started(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::get_started)
    7. {
    8.  
    9. ui->setupUi(this);
    10. }
    To copy to clipboard, switch view to plain text mode 

    I have been trying to solve this issue for several hours now, really appreciate if you help me solve it

  5. #4
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt show modal dialog (.ui) on menu item click

    Add a qDebug () in your get_started_info() slot to check if it is getting called when the triggered signal is fired. Try to create a QMessageBox to see if it will show.
    Qt Code:
    1. QMessageBox::information(this,"title","message");// dont forget to include QMessageBox
    To copy to clipboard, switch view to plain text mode 
    You can go to the UI file in the designer and go to the action editor and right click your action , go to slot and press on triggered ( it will then automatically create a slot for you in your mainwindow ) and inside it create your dialog.
    Did you create a class memember from your dialog ?
    Qt Code:
    1. get_started *one; //in your mainwindow under private
    To copy to clipboard, switch view to plain text mode 

    Good Luck.

  6. #5
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    2

    Default Re: Qt show modal dialog (.ui) on menu item click

    Qt Code:
    1. void MainWindow::get_started_info()
    2. {
    3. QMessageBox::information(this,"About","message");// dont forget to include QMessageBox
    4. }
    To copy to clipboard, switch view to plain text mode 
    this works, when I click on Get Started, this message box appears

    Qt Code:
    1. get_started *one; //in your mainwindow under private
    To copy to clipboard, switch view to plain text mode 
    like this :
    Qt Code:
    1. private:
    2. Ui::MainWindow *ui;
    3. //! Create the menu classes
    4. void createActions();
    5. void createMenus();
    6. //! Menu Items
    7. QMenu *home;
    8. QMenu *help;
    9. QAction *quit;
    10. QAction *start;
    11. QAction *about;
    12. get_started *one;
    To copy to clipboard, switch view to plain text mode 
    but I get an error 'get_started' does not name a type

  7. #6
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt show modal dialog (.ui) on menu item click

    Quote Originally Posted by tomorrow
    but I get an error 'get_started' does not name a type
    Did you include get_started.h ?
    And you have
    Qt Code:
    1. #include "ui_get_started.h"
    To copy to clipboard, switch view to plain text mode 
    I am not sure if you need this to be included in both header and cpp file, maybe just in your .cpp file.

    Good Luck.

  8. The following user says thank you to toufic.dbouk for this useful post:

    tomorrow (15th November 2013)

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt show modal dialog (.ui) on menu item click

    The original code was almost correct, the only mistake was usign qApp as the receiver object instead of this.

    Creating the dialog on the stack and excuting it as a modal dialog using exec() where correct.

    Cheers,
    _

  10. #8
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt show modal dialog (.ui) on menu item click

    Quote Originally Posted by anda_skoa
    Creating the dialog on the stack and excuting it as a modal dialog using exec() where correct.
    Yes it was correct and we mentioned that he can keep his code since its right instead of creating it on heap. Creating it on the stack will limit the scope where you can use the object for further usage.

Similar Threads

  1. Replies: 2
    Last Post: 11th May 2012, 10:38
  2. Right-Click menu on a pushButton doesn't show up
    By qtpyqt in forum Qt Programming
    Replies: 6
    Last Post: 9th September 2010, 17:58
  3. How to edit view item with a modal dialog?
    By e79ene in forum Newbie
    Replies: 1
    Last Post: 26th March 2010, 15:57
  4. Need to show a menu item with disabled look but functional
    By lalesculiviu in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2009, 15:16
  5. Replies: 0
    Last Post: 29th July 2009, 19:13

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
  •  
Qt is a trademark of The Qt Company.