Results 1 to 11 of 11

Thread: Looking for help with closing SQL QTableView

  1. #1
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4

    Default Looking for help with closing SQL QTableView

    Hey guys,

    I've been programming in QT for a few months here and there, but I seem to have run in to an issue.

    I'm writing a program that reads from an SQL database (and displays the data in a QTableView when the user clicks on a button). The only thing I need help with is that the windows don't close automatically when I close the main program.

    I'm using this function template to display my windows:
    Qt Code:
    1. void server :: showCandidates()
    2. {
    3. QSqlQueryModel *testQuery = new QSqlQueryModel;
    4. testQuery->setQuery("SELECT * from candidates");
    5. QTableView *test = new QTableView;
    6. test->setModel(testQuery);
    7. test->setWindowTitle("Candidates");
    8. test->show();
    9. }
    To copy to clipboard, switch view to plain text mode 
    and I just trigger that with a signal, like
    Qt Code:
    1. connect(ui->candidatesButton, SIGNAL(clicked()), this, SLOT(showCandidates()));
    To copy to clipboard, switch view to plain text mode 
    I tried to tie the QTableView to the main window (as the parent) but that was giving me trouble (it wouldn't create a new window, it would just pop the table view in the main window and overlap with the GUI).

    Any help would be much appreciated, I'm not the smartest with programming so please don't make answers too complicated.

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Looking for help with closing SQL QTableView

    Closing a widget doesn't quit the application if more widgets are still there.
    If you want that, you should implement the main window close event and handle the closing of all open windows, saving settings and quiting the app.

  3. #3
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Looking for help with closing SQL QTableView

    hi ,
    i think the best way would be using mdisubwindow.
    add a mdisubwindow.
    Qt Code:
    1. QMdiSubWindow *s=new QMdiSubWindow;
    2. QTableView *test = new QTableView(s);
    3. test->setModel(testQuery);
    4. test->setWindowTitle("Candidates");
    5. s->show();
    To copy to clipboard, switch view to plain text mode 

    hope it helps
    Bala

  4. #4
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Looking for help with closing SQL QTableView

    Hi,

    @BalaQT : won't de QMdiSubWindow still stay alive when the main window is closed ? I'm not sure.

    @duave : you created a tableview object with 'new'. If your mainwindow is closed, the object isn't automatically destroyed, and it isn't closed (as tbscope said). If you set your main window as parent of the tableview, the main window takes care of this.

    You can remember the pointer 'test' to your QTableView as a member variable of your main window class. Then, in the close event of your main window class, see if a QTableView was created by checking this member variable, and delete it if necessary.

    Best regards,
    Marc

  5. #5
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Looking for help with closing SQL QTableView

    hi @marcvanriet
    @BalaQT : won't de QMdiSubWindow still stay alive when the main window is closed ? I'm not sure.
    no it wont. when u close the mainwindow, mdisubwindow will be closed too.

    Bala

  6. #6
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4

    Default Re: Looking for help with closing SQL QTableView

    Quote Originally Posted by marcvanriet View Post
    @duave : you created a tableview object with 'new'. If your mainwindow is closed, the object isn't automatically destroyed, and it isn't closed (as tbscope said). If you set your main window as parent of the tableview, the main window takes care of this.
    You mean like the following?
    Qt Code:
    1. QTableView *test = new QTableView(this);
    To copy to clipboard, switch view to plain text mode 
    That has the QTableView appear actually inside the window, not in a new window.

  7. #7
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Looking for help with closing SQL QTableView

    Hi duave,

    Yes, it shows inside the window, and I know that that is not what you want. I just wanted to explain why it isn't closed automatically when you don't give it a parent.

    To elaborate my 'solution' :

    In the class definition of you mainwindow, add something like QTableView *m_pCandidatesView;
    In the constructor, put this : m_pCandidatesView = NULL;
    In your showCandidates function, do this : m_pCandidatesView = new QTableView;
    In the destructor of your mainwindow, or a function connected to its destroyed() signal, put this : if( m_pCandidatesView ) m_pCandidatesView->close();

    Note that this stil isn't a clean programming style. In my opinion, you'd better create a dialog (or QMdiSubWindow) with a tableview in it and show that dialog. The dialog could be instantiated once and re-used, or instantiated and deleted when closed. See the many topics on 'opening a second window' in this forum.

    Best regards,
    Marc

  8. #8
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Looking for help with closing SQL QTableView

    I tried to tie the QTableView to the main window (as the parent) but that was giving me trouble (it wouldn't create a new window, it would just pop the table view in the main window and overlap with the GUI).
    That's interesting because that is the way I am doing it and I don't get that result.
    Are you sure it's really IN the main window and not just overlapping it?
    Did you try to set the position of the table view with view->move(x,y) ???
    Mainwindow is the parent of my table view and I just reposition it as above.
    Then when you close the application, it dies, as Marc said.

    Qt Code:
    1. view = new QTableView(this);
    2. view->resize(1200, 300);
    3. view->move(35, 80);
    4. view->show();
    To copy to clipboard, switch view to plain text mode 
    Works perfectly for me. Separate window and it puts it where you want it and no problems with closing.

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Looking for help with closing SQL QTableView

    You can also use a dialog too. Add the table view to a dialog. Make the dialog a child of the main window.

  10. #10
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Looking for help with closing SQL QTableView

    hi,
    as tbscope said u can try this,
    Qt Code:
    1. QSqlQueryModel *testQuery = new QSqlQueryModel;
    2. testQuery->setQuery("SELECT * from candidates");
    3. QDialog *d=new QDialog(this);
    4. QTableView *test = new QTableView(d);
    5. test->setModel(testQuery);
    6. test->setWindowTitle("Candidates");
    7. d->show();
    To copy to clipboard, switch view to plain text mode 

    but again , if mainwindow is minimized ur tableview wont be minimized.
    when the mainwindow is minimized ,if u want to minimize the tableview use mdi

    example for mdi
    Qt Code:
    1. QMdiArea *mdi=new QMdiArea;
    2. QSqlQueryModel *testQuery = new QSqlQueryModel;
    3. testQuery->setQuery("SELECT * from candidates");
    4. QDialog *d=new QDialog;
    5. QTableView *test = new QTableView(d);
    6. test->setModel(testQuery);
    7. test->setWindowTitle("Candidates");
    8. mdi->addSubWindow(d);
    9. d->show();
    10. setCentralWidget(mdi);
    To copy to clipboard, switch view to plain text mode 

    hope it helps
    Bala

  11. #11

    Default Re: Looking for help with closing SQL QTableView

    This still doesnt work. The items inside the box cant be viewed due to the box sizing.

Similar Threads

  1. [OT] closing of qt bugs
    By luf in forum Qt Programming
    Replies: 0
    Last Post: 26th July 2010, 13:55
  2. Application Closing
    By manti_madhu in forum Qt Programming
    Replies: 2
    Last Post: 27th November 2009, 09:55
  3. What happens after closing and before destruction?
    By Raccoon29 in forum Qt Programming
    Replies: 45
    Last Post: 20th May 2008, 09:33
  4. closing a widget
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2007, 09:57
  5. Closing a QDialog
    By eu.x in forum Newbie
    Replies: 2
    Last Post: 12th March 2007, 08:33

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.