Results 1 to 19 of 19

Thread: How to show a QDialog ( Designed ) ?

  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to show a QDialog ( Designed ) ?

    I had created a new design form but i do not know how to show it
    Take a look to the example i have created ...
    Because i don't know why it doesn't let me to attach a rar file i have uploaded the example to megaupload.. Download link: Here



    This documentation for signal and slots cannot help me so i would appreciate if you can edit my project..

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    Try the examples, the simple ones first and then advance to more complicated ones, to understand the signal/slot mechanism, to understand how to write your own signal and slots and how to connect them.

    A hint for your project (this is just one way to achieve this, depend on what functionality you want in your application)
    You can create a slot in your main window (the one with the Button) and in this slot you can create an instance of the dialog (create it on the heap and pass 'this' pointer as parent, so that the main window will be parent of your dialog) and then show the created dialog.
    Of course in the main window class constructor you will connect the button's clicked signal with the slot you created (careful on how you access the button Hint: need to use ui->Your_Button_Name)

  3. #3
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to show a QDialog ( Designed ) ?

    You know the documentation for signal and slots doesn't answer me directly to my question... I want when i press for example a button, a new Qdialog to pop up.
    I can show a Qdialog with this..

    QDialog *gamatosdialog = new QDialog;
    gamatosdialog->show();

    But i do not know how to design this QDialog... Show i thought it is a good idea to make a new design form .. So when you press a button for example this design form to be a dialog and pop up... I still cannot undersdand how to do this..

    P.s Maybe somehow you connect the design form with a new Qdialog? I do not know..

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    You know the documentation for signal and slots doesn't answer me directly to my question
    Are you sure about that?

    Qt Code:
    1. connect(yourButton, SIGNAL(clicked()), yourForm, SLOT(showNewDialog());
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    Quote Originally Posted by Bong.Da.City View Post
    You know the documentation for signal and slots doesn't answer me directly to my question...
    Indeed it doesn't answer directly to your question, but you need to understand how signal/slot and connect "mechanisms" work, then this won't be so "complicated"
    Quote Originally Posted by Bong.Da.City View Post
    QDialog *gamatosdialog = new QDialog;
    gamatosdialog->show();
    Almost correct (this is the way to create and show a dialog)

    But (as i said before) you need to:
    1) create a slot (witch is just a member function only that has "public slots:" access specifier) in the main window class
    2) this slot will contain the code witch create and show (or exec) the dialog, with the only modification that you will pass 'this' pointer as a parent to the dialog
    3) in the main window constructor you will connect the ui->NameOfButton signal clicked with the slot that you created (the one that create and show the dialog)

  6. #6
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to show a QDialog ( Designed ) ?

    Qt Code:
    1. void example::on_pushButton_clicked()
    2. {
    3. QDialog *gamatos = new QDialog;
    4. connect(ui->pushButton, SIGNAL(clicked()), hi, SLOT(gamatos->show()));
    5. }
    To copy to clipboard, switch view to plain text mode 
    This doenot work!!!

  7. #7
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    Of course it doesn't. You never make a connection in the slot that it is supposed to call.

    edit:

    Qt Code:
    1. void example::on_pushButton_clicked()
    2. {
    3. QDialog *gamatos = new QDialog;
    4. connect(ui->pushButton, SIGNAL(clicked()), hi, SLOT(gamatos->show()));
    5. }
    To copy to clipboard, switch view to plain text mode 

    should be something along the lines of

    Qt Code:
    1. yourClass::yourConstructor(...) : yourParent() {
    2. ...
    3. connect(ui->pushButton, SIGNAL(clicked()), hi, SLOT(on_pushButton_clicked());
    4. ...
    5. }
    6.  
    7. void example::on_pushButton_clicked()
    8. {
    9. QDialog *gamatos = new QDialog;
    10. ...
    11. gamatos->exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    You *really* need to read and re-read the signals and slots documentation very carefully until you understand it. A slot isn't a statement, it's a function name, and the connection must be established outside of the slot otherwise the slot *cannot* be called!
    Last edited by Urthas; 17th August 2010 at 19:11.

  8. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    You don't connect in the slot...
    Qt Code:
    1. void example::on_pushButton_clicked()
    2. {
    3. QDialog *gamatos = new QDialog(this); //you need to pass 'this' as parent so that you don't get a memory leak
    4. gamatos->show(); //you show the dialog
    5. }
    To copy to clipboard, switch view to plain text mode 

    Now the connect "stuff" if you create slots by the autoconnect (in the end of that article) rules you don't need to manually connect (the autoconnect does the connection for you)

    Else (and i really recommend this method) you do the connection in the class constructor, something like:
    Qt Code:
    1. example::example // : parameter passing...
    2. {//...
    3. connect(ui->NameOfButton, SIGNAL (clicked()), this, SLOT(on_pushButton_clicked()));
    4. //... rest of the constructor code
    5. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to show a QDialog ( Designed ) ?

    See this... It still doesn't work ..

    Qt Code:
    1. #include "example.h"
    2. #include "ui_example.h"
    3. #include <QDialog>
    4.  
    5. example::example(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::example)
    8. {
    9. ui->setupUi(this);
    10. QDialog *gamatos = new QDialog(this);
    11. connect(ui->pushButton, SIGNAL(clicked()), hi.ui, SLOT(gamatos()));
    12.  
    13. }
    14.  
    15. example::~example()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void example::changeEvent(QEvent *e)
    21. {
    22. QMainWindow::changeEvent(e);
    23. switch (e->type()) {
    24. case QEvent::LanguageChange:
    25. ui->retranslateUi(this);
    26. break;
    27. default:
    28. break;
    29. }
    30. }
    31.  
    32. void example::on_pushButton_clicked()
    33. {
    34. connect(ui->pushButton, SIGNAL (clicked()), this, SLOT(on_pushButton_clicked()));
    35. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    And where is the gamatos() method, exactly? Stop thrashing. Read your error messages! Look at the code that has been handed to you...

  11. #11
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    Qt Code:
    1. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
    To copy to clipboard, switch view to plain text mode 

    That 'this' is a pointer to the object itself (the main window in your case) and this has the slot that will create and show the dialog (on_pushButton_clicked() in your case )

    LE: the slot should be:
    Qt Code:
    1. void example::on_pushButton_clicked()
    2. {
    3. QDialog *gamatos = new QDialog(this); //you need to pass 'this' as parent so that you don't get a memory leak
    4. gamatos->show(); //you show the dialog
    5. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to show a QDialog ( Designed ) ?

    I don't know if you understand the problem. I understood the Bong's problem. His has 2 ui files. One ui is its main file. The other ui is specially designed for the QDialog. How to connect the other.ui file with the push Button???

  13. #13
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    I understood his problem, and the solution is in front of you:
    Qt Code:
    1. #include "example.h"
    2. #include "ui_example.h"
    3. #include "what_ever_dialog_class_with_ui.h" //include the header file
    4.  
    5. example::example(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::example)
    8. {
    9. ui->setupUi(this);
    10. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked())); //connect... the signal with slot ...
    11.  
    12. }
    13.  
    14. example::~example()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void example::on_pushButton_clicked()
    20. {
    21. //use the other class to instantiate the QDialog or the QDialog derived class (with or without ui)
    22. what_ever_dialog_class_with_ui *gamatos = new what_ever_dialog_class_with_ui(this); //you need to pass 'this' as parent so that you don't get a memory leak
    23. gamatos->show(); //you show the dialog
    24. }
    25.  
    26. void example::changeEvent(QEvent *e)
    27. {
    28. QMainWindow::changeEvent(e);
    29. switch (e->type()) {
    30. case QEvent::LanguageChange:
    31. ui->retranslateUi(this);
    32. break;
    33. default:
    34. break;
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to show a QDialog ( Designed ) ?

    I cannot understand what should i change on what_ever_dialog_class_with_ui ? look what i have done..

    Qt Code:
    1. #include "example.h"
    2. #include "ui_example.h"
    3. #include "hi.h" //include the header file
    4.  
    5. example::example(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::example)
    8.  
    9. {
    10. ui->setupUi(this);
    11. QDialog *gamatos = new QDialog(this);
    12. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked())); //connect... the signal with slot ...
    13. }
    14.  
    15. example::~example()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void example::on_pushButton_clicked()
    21.  
    22. {
    23. //use the other class to instantiate the QDialog or the QDialog derived class (with or without ui)
    24. hi.h *gamatos = new QDialog(this); //you need to pass 'this' as parent so that you don't get a memory leak
    25. gamatos->show(); //you show the dialog
    26. }
    27.  
    28. void example::changeEvent(QEvent *e)
    29. {
    30. QMainWindow::changeEvent(e);
    31. switch (e->type()) {
    32. case QEvent::LanguageChange:
    33. ui->retranslateUi(this);
    34. break;
    35. default:
    36. break;
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    Qt Code:
    1. #include "example.h"
    2. #include "ui_example.h"
    3. #include "hi.h" //include the header file
    4.  
    5. example::example(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::example)
    8. {
    9. ui->setupUi(this);
    10.  
    11. // note that the declaration of pointer can be done only once
    12.  
    13. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked())); //connect... the signal with slot ...
    14.  
    15. }
    16.  
    17. example::~example()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void example::on_pushButton_clicked()
    23. {
    24. //use the other class to instantiate the QDialog or the QDialog derived class (with or without ui)
    25.  
    26. hi *gamatos = new hi(this); //this is valid if the class name is "hi"... replace that with your class name //you need to pass 'this' as parent so that you don't get a memory leak
    27.  
    28. gamatos->show(); //you show the dialog
    29. }
    30.  
    31. void example::changeEvent(QEvent *e)
    32. {
    33. QMainWindow::changeEvent(e);
    34. switch (e->type()) {
    35. case QEvent::LanguageChange:
    36. ui->retranslateUi(this);
    37. break;
    38. default:
    39. break;
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 

    NOTE: the code changed a bit... i corrected in the previous post, but you already copied... you can't declare the pointer twice (you don't declare it in the constructor, only in the slot)

  16. #16
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to show a QDialog ( Designed ) ?

    Ok i founded it.. .But know two Qdialogs are been shown :P

    Qt Code:
    1. #include "example.h"
    2. #include "ui_example.h"
    3. #include "hi.h" //include the header file
    4.  
    5. example::example(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::example)
    8.  
    9. {
    10. ui->setupUi(this);
    11. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked())); //connect... the signal with slot ...
    12. }
    13.  
    14. example::~example()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void example::on_pushButton_clicked()
    20.  
    21. {
    22. //use the other class to instantiate the QDialog or the QDialog derived class (with or without ui)
    23. hi *gamatos = new hi(this); //you need to pass 'this' as parent so that you don't get a memory leak
    24. gamatos->show(); //you show the dialog
    25. }
    26.  
    27. void example::changeEvent(QEvent *e)
    28. {
    29. QMainWindow::changeEvent(e);
    30. switch (e->type()) {
    31. case QEvent::LanguageChange:
    32. ui->retranslateUi(this);
    33. break;
    34. default:
    35. break;
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

  17. #17
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    See the constructor... and the edits of my previous posts... you initialize the second dialog twice (create two dialogs... with two pointers that have the same name...)

  18. #18
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show a QDialog ( Designed ) ?

    Or, maybe the name of the slot match the autoconnect rules (i gave you a link in a previous post)

    You can rename the slot: something like
    Qt Code:
    1. void example::openDialog() //and connect with this slot name
    2. {
    3. //use the other class to instantiate the QDialog or the QDialog derived class (with or without ui)
    4. hi *gamatos = new hi(this); //you need to pass 'this' as parent so that you don't get a memory leak
    5. gamatos->show(); //you show the dialog
    6. }
    To copy to clipboard, switch view to plain text mode 

    Or don't connect manually and let autoconnect do the connection
    Last edited by Zlatomir; 17th August 2010 at 19:59. Reason: i forgot to rename the slot

  19. #19
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to show a QDialog ( Designed ) ?

    Ok it works... Many thanks.. I need it..

Similar Threads

  1. Replies: 7
    Last Post: 25th July 2016, 14:42
  2. How to show a QDialog ( Designed ) ?
    By Bong.Da.City in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2010, 16:54
  3. use QDialog to show file name
    By damonlin in forum Qt Programming
    Replies: 3
    Last Post: 16th November 2008, 13:52
  4. can't show my QDialog
    By sincnarf in forum Qt Programming
    Replies: 3
    Last Post: 22nd October 2007, 09:33
  5. QDialog: show() and exec() together in constructor?
    By Teuniz in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2007, 11:43

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.