Page 1 of 3 123 LastLast
Results 1 to 20 of 41

Thread: closing a dialog

  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
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: closing a dialog

    Well, without knowing the error message we cannot help.
    It's nice to be important but it's more important to be nice.

  14. #13
    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

    the error i get
    Qt Code:
    1. test.cpp:112: error: no matching function for call to 'myQtApp::myQtApp(test* const)'
    2. test.cpp:65: note: candidates are: myQtApp::myQtApp()
    3. test.h:79: note: myQtApp::myQtApp(const myQtApp&)
    To copy to clipboard, switch view to plain text mode 

  15. #14
    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

    You need your constructor to accept a QObject parent as argument. See the signature of QDialog's constructor.
    It's nice to be important but it's more important to be nice.

  16. #15
    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 

  17. #16
    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

    Quote Originally Posted by chrisb123 View Post
    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 
    Right, because web will never be freed again and you lose memory. Also the dialog does not have a parent that is used for other purposes, for example, center the dialog above the mainwindow. Even if you would have passed a proper parent, the dialog would be created again and again and you would have a lot of instances of it in memory. So either create it on the stack or save the pointer to that dialog so that the next time you do not have to create a new one.


    Quote Originally Posted by chrisb123 View Post
    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 
    No, this is correct, because you pass a proper parent to the webview. But make the webview's constructor have a single argument, a QWidget so you can also pass a proper parent to that:

    Qt Code:
    1. webapp::webapp(QWidget* parent) : QWidget(parent) { // I assume here that webapp directly inherits QWidget
    2. QWebView *view = new QWebView(this);
    3. view->show();
    4. }
    To copy to clipboard, switch view to plain text mode 

    You will also have to adjust your header file to have that argument to the constructor.
    It's nice to be important but it's more important to be nice.

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

    chrisb123 (24th October 2009)

  19. #17
    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

    ok, how should the function create the new dialog
    and what should the header for it look like?
    and I also assume that webapp directly inherits QWidget
    Qt Code:
    1. class webapp : public QWidget
    2.  
    3. {
    4. Q_OBJECT
    5. public:
    6. webapp(QWidget* parent);
    7. ~webapp();
    8. private:
    9. QWebView *view;
    10.  
    11. protected:
    12. void resizeEvent(QResizeEvent *event);
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 

  20. #18
    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

    Header looks OK. The only thing I do not like is a lowercase class name because usually the first letter in the classname is uppercase.

    Regarding the dialog: Create the constructor in the constructor, save the pointer in a member variable and then only call show on it in your method.
    It's nice to be important but it's more important to be nice.

  21. #19
    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 axeljaeger View Post
    Regarding the dialog: Create the constructor in the constructor, save the pointer in a member variable and then only call show on it in your method.
    how do i save the pointer in a member variable?
    i assume you mean "web"
    and how do i only call show in the method?
    I want the widget to display immediately after selecting the menu item

    so it should like this
    the slot function gets called from a menu item
    Qt Code:
    1. void test::web() {
    2. webapp::webapp(*parent);
    To copy to clipboard, switch view to plain text mode 
    which calls this
    Qt Code:
    1. webapp::webapp(QWidget* parent) {
    2. QWidget *web = new webapp(this);
    3. web->resize(1000, 350);
    4. web->show();
    5. view = new QWebView(this);
    6. view->show();
    7. }
    To copy to clipboard, switch view to plain text mode 
    It doesnt work, the program runs but the new widget doesnt display

  22. #20
    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

    Yes, because both widget gets "this" as parent. If you want your webview to be shown inside "web", pass web as the parent to the webview.

    Then do not use QWidget as baseclass for web but QDialog so it will be shown in a new window on the screen.
    It's nice to be important but it's more important to be nice.

Similar Threads

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