Results 1 to 19 of 19

Thread: How to show a QDialog ( Designed ) ?

Hybrid View

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

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

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

    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.

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

    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 

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

    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 

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

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

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

    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 

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

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

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

    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 

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

    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 

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

    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)

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

    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 

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

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

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

    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

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

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