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:
Code:
void server :: showCandidates()
{
testQuery->setQuery("SELECT * from candidates");
test->setModel(testQuery);
test->setWindowTitle("Candidates");
test->show();
}
and I just trigger that with a signal, like
Code:
connect(ui->candidatesButton, SIGNAL(clicked()), this, SLOT(showCandidates()));
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!
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.
Re: Looking for help with closing SQL QTableView
hi ,
i think the best way would be using mdisubwindow.
add a mdisubwindow.
Code:
QMdiSubWindow *s=new QMdiSubWindow;
test->setModel(testQuery);
test->setWindowTitle("Candidates");
s->show();
hope it helps
Bala
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
Re: Looking for help with closing SQL QTableView
hi @marcvanriet
Quote:
@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
Re: Looking for help with closing SQL QTableView
Quote:
Originally Posted by
marcvanriet
@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?
That has the QTableView appear actually inside the window, not in a new window.
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
Re: Looking for help with closing SQL QTableView
Quote:
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.
Code:
view->resize(1200, 300);
view->move(35, 80);
view->show();
Works perfectly for me. Separate window and it puts it where you want it and no problems with closing.
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.
Re: Looking for help with closing SQL QTableView
hi,
as tbscope said u can try this,
Code:
testQuery->setQuery("SELECT * from candidates");
test->setModel(testQuery);
test->setWindowTitle("Candidates");
d->show();
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
Code:
QMdiArea *mdi=new QMdiArea;
testQuery->setQuery("SELECT * from candidates");
test->setModel(testQuery);
test->setWindowTitle("Candidates");
mdi->addSubWindow(d);
d->show();
setCentralWidget(mdi);
hope it helps
Bala
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.