Results 1 to 20 of 41

Thread: closing a dialog

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default closing a dialog

    I made a plasma app that opens a settings dialog
    I'm trying to close a dialog with a close button
    If I open it like this

    Qt Code:
    1. void test::settings() {
    2. myQtApp *dialog = new myQtApp;
    3. dialog->show();
    4. }
    To copy to clipboard, switch view to plain text mode 

    How can i close it like this

    Qt Code:
    1. myQtApp::myQtApp(QWidget *parent)
    2. {
    3. setupUi(this);
    4. connect( pushButton_close, SIGNAL( clicked() ), this, SLOT( close() ) );
    5. }
    6. void myQtApp::close() {
    7. QMessageBox::about(this,"close",
    8. "this shold close the dialog");
    9. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: closing a dialog

    First, you are leaking memory because you create a QDialog on the heap without a proper parent. Don't do this, read something about object trees in Qt:

    http://doc.trolltech.com/4.5/objecttrees.html

    Then remove your own implementation of close because there is already one in QDialog. I am pretty sure that you showing your message box, you overwrite the already provided close-slot.
    It's nice to be important but it's more important to be nice.

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

    chrisb123 (24th October 2009)

  4. #3
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: closing a dialog

    i found out if use
    Qt Code:
    1. this->hide();
    To copy to clipboard, switch view to plain text mode 
    it will close, however I should delete the close function
    Sorry my programming skills are limited, I still dont quite understand
    I do a lot of copy, paste, modify with-out understanding whats really going on
    If I remove the "QWidget *parent" is that better?
    The application still works as expected

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: closing a dialog

    If you don't use parent you should use a model dialog so you can delete the dialog when the model returns

  6. #5
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: closing a dialog

    Don't you just hate crappy programmers like me...
    I have a
    Qt Code:
    1. target::target(QObject *parent, const QVariantList &args)
    2. : Plasma::Applet(parent, args),
    To copy to clipboard, switch view to plain text mode 
    it contains parent, again, which I don't understand.

  7. #6
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: closing a dialog

    The point is:

    You usually create a tree of objects. If you delete an object, all its childs are deleted as well. So usually you do not have to care about deleting most of the objects because they will be deleted by their parent object.

    Also if you create an object on the stack (without new and not as a pointer) it will be deleted when going "out of scope", e.g. when the program goes out of the "block" in which the object was created.

    Your dialog was not created on the stack but on the heap, it did not got a parent pointer and you do not delete it yourself. So it will not be deleted at all.

    I'm a bit picky about this because at some other forum, a lot of people completely ignoring this fundamental feature of Qt. So everytime I see someone now who does not use proper parent child, some red lights turn on here.
    It's nice to be important but it's more important to be nice.

  8. #7
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: closing a dialog

    I understand I should be correctly creating child objects of the parent
    I dont understand how do write code so the object is a child of the parent
    Like I said I barely understand what I'm doing
    is there another tutorial I can read relating to this?

  9. #8
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: closing a dialog

    Pass some other QWidget as the first argument to the constructor of myQtApp.
    e.g not myQtApp but myQtApp(this).
    It's nice to be important but it's more important to be nice.

  10. #9
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: closing a dialog

    doing this
    Qt Code:
    1. myQtApp::myQtApp(this)
    To copy to clipboard, switch view to plain text mode 
    i get the error
    expected unqualified-id before 'this'

  11. #10
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: closing a dialog

    No no, like this:

    Qt Code:
    1. void test::settings() {
    2. myQtApp *dialog = new myQtApp(this);
    3. dialog->show();
    4. }
    To copy to clipboard, switch view to plain text mode 

    But you see that everytime you call the settings slot, a new instance is created and only released when you delete "this". Do it rather like this:

    Qt Code:
    1. void test::settings() {
    2. myQtApp dialog(this);
    3. dialog.show();
    4. } // dialog is deleted now.
    To copy to clipboard, switch view to plain text mode 
    It's nice to be important but it's more important to be nice.

  12. #11
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: closing a dialog

    neither work
    i need to change the header, to what i have no idea

  13. #12
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: closing a dialog

    I dont get it ??? I'm lost

    So I should not open a new dialog like this
    Qt Code:
    1. void test::web() {
    2. QWidget *web = new webapp();
    3. web->show();
    To copy to clipboard, switch view to plain text mode 

    and adding a webkit widget to this new dialg like this is incorrect
    Qt Code:
    1. webapp::webapp(){
    2. QWebView *view = new QWebView(this);
    3. view->show();
    4. }
    To copy to clipboard, switch view to plain text mode 

  14. #13
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: closing a dialog

    Quote Originally Posted by chrisb123 View Post
    I made a plasma app that opens a settings dialog
    I'm trying to close a dialog with a close button
    If I open it like this

    Qt Code:
    1. void test::settings() {
    2. myQtApp *dialog = new myQtApp;
    3. dialog->show();
    4. }
    To copy to clipboard, switch view to plain text mode 

    How can i close it like this

    Qt Code:
    1. myQtApp::myQtApp(QWidget *parent)
    2. {
    3. setupUi(this);
    4. connect( pushButton_close, SIGNAL( clicked() ), this, SLOT( close() ) );
    5. }
    6. void myQtApp::close() {
    7. QMessageBox::about(this,"close",
    8. "this shold close the dialog");
    9. }
    To copy to clipboard, switch view to plain text mode 
    Didn't read up all if you actually fixed your problem already but:
    Qt Code:
    1. myQtApp *dialog = new myQtApp(this);
    2. dialog->setAttribute(Qt::WA_DeleteOnClose);
    3. dialog->show();
    To copy to clipboard, switch view to plain text mode 

    this would solve your problem with deleting dialog.

  15. #14
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: closing a dialog

    Quote Originally Posted by RSX View Post
    Didn't read up all if you actually fixed your problem already but:
    Qt Code:
    1. myQtApp *dialog = new myQtApp(this);
    2. dialog->setAttribute(Qt::WA_DeleteOnClose);
    3. dialog->show();
    To copy to clipboard, switch view to plain text mode 

    this would solve your problem with deleting dialog.
    That works but how do I test if dialog has been deleted or not, I only want to have one created at any one time

  16. #15
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: closing a dialog

    I think it's deleted when you close the dialog. Qt uses to do a lot of hard work for you

    Well, if so, you can delete it manually.
    Qt Code:
    1. myQtApp *dialog = new myQtApp(this);
    2. dialog->setAttribute(Qt::WA_DeleteOnClose);
    3. dialog->show();
    4. delete dialog;
    5. dialog = 0;
    To copy to clipboard, switch view to plain text mode 

    What I do is to create the dialog on the stack. So it will be deleted for sure when it's out of scope. But I do it with small and temporary dialogs.

    Qt Code:
    1. myQtApp dialog(this);
    2. dialog.setAttribute(Qt::WA_DeleteOnClose);
    3. dialog.show();
    To copy to clipboard, switch view to plain text mode 

  17. #16
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: closing a dialog

    My application is a plasma widget so 99.99% of the time its just the one widget, there is no reason to have anything else just waiting, hidden, invisible or not shown.

    When I use this code
    Qt Code:
    1. m_setapp = new SetApp(this);
    To copy to clipboard, switch view to plain text mode 
    I get this error while compiling
    Qt Code:
    1. mum.cpp: In member function 'void mum::settings()':
    2. mum.cpp:75: error: no matching function for call to 'SetApp::SetApp(mum* const)'
    3. myqtapp.h:34: note: candidates are: SetApp::SetApp(QWidget*)
    4. myqtapp.h:29: note: SetApp::SetApp(const SetApp&)
    To copy to clipboard, switch view to plain text mode 
    But this works ok
    Qt Code:
    1. m_setapp = new SetApp(NULL);
    To copy to clipboard, switch view to plain text mode 

    From what I can tell it's not deleted when it's closed, I have to manually delete it when its closed, then create it again when required

  18. #17
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: closing a dialog

    Right, you have to delete it yourself when you close the application.

    But again: Dont try to be to clever. Get your application up and running and dont bother too much in saving memory when you are not know what you are actually doing. Keeping the dialog in memory will not hurt very much. I'd keep it there so it gets shown faster the second time I open it.
    It's nice to be important but it's more important to be nice.

  19. The following user says thank you to axeljaeger for this useful post:

    chrisb123 (27th October 2009)

  20. #18
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: closing a dialog

    thanks for your help, its been useful
    I still need to learn c++ as object oriented programming is new to me
    As for my application, it does what is needed thats why Ive moved onto improving it

Similar Threads

  1. Closing a dialog using a button
    By srohit24 in forum Qt Programming
    Replies: 5
    Last Post: 21st July 2009, 05:57
  2. Issue with Modelless dialog on Mac
    By Satyanarayana Chebrolu in forum Qt Programming
    Replies: 0
    Last Post: 24th February 2009, 10:10
  3. Dialog is not closing
    By manmohan in forum Newbie
    Replies: 5
    Last Post: 1st December 2008, 17:04
  4. Replies: 9
    Last Post: 13th August 2008, 18:07
  5. closing dialog in hidden main Widget
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2006, 10:35

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.