PDA

View Full Version : Looking for help with closing SQL QTableView



duave
30th November 2010, 06:46
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:

void server :: showCandidates()
{
QSqlQueryModel *testQuery = new QSqlQueryModel;
testQuery->setQuery("SELECT * from candidates");
QTableView *test = new QTableView;
test->setModel(testQuery);
test->setWindowTitle("Candidates");
test->show();
}and I just trigger that with a signal, like

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!

tbscope
30th November 2010, 06:57
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.

BalaQT
30th November 2010, 07:36
hi ,
i think the best way would be using mdisubwindow.
add a mdisubwindow.

QMdiSubWindow *s=new QMdiSubWindow;
QTableView *test = new QTableView(s);
test->setModel(testQuery);
test->setWindowTitle("Candidates");
s->show();

hope it helps
Bala

marcvanriet
30th November 2010, 11:50
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

BalaQT
30th November 2010, 12:10
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

duave
30th November 2010, 20:08
@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?

QTableView *test = new QTableView(this);
That has the QTableView appear actually inside the window, not in a new window.

marcvanriet
30th November 2010, 22:56
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

waynew
1st December 2010, 01:00
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.



view = new QTableView(this);
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.

tbscope
1st December 2010, 04:21
You can also use a dialog too. Add the table view to a dialog. Make the dialog a child of the main window.

BalaQT
1st December 2010, 05:58
hi,
as tbscope said u can try this,

QSqlQueryModel *testQuery = new QSqlQueryModel;
testQuery->setQuery("SELECT * from candidates");
QDialog *d=new QDialog(this);
QTableView *test = new QTableView(d);
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

QMdiArea *mdi=new QMdiArea;
QSqlQueryModel *testQuery = new QSqlQueryModel;
testQuery->setQuery("SELECT * from candidates");
QDialog *d=new QDialog;
QTableView *test = new QTableView(d);
test->setModel(testQuery);
test->setWindowTitle("Candidates");
mdi->addSubWindow(d);
d->show();
setCentralWidget(mdi);

hope it helps
Bala

levorn10
7th January 2011, 03:38
This still doesnt work. The items inside the box cant be viewed due to the box sizing.