Originally Posted by
Chicken Blood Machine
...snip...
Where QObject really helps out is when you create a GUI. A toplevel window, say derived from QMainWindow, may contain a myriad of other subwidgets within itself (Consider a complex gui that you have created in Designer). When you intsantiate your main window widget, you may do it like this:
MyMainWindow* mw = new MyMainWindow(0);// note null parent
MyMainWindow* mw = new MyMainWindow(0);// note null parent
To copy to clipboard, switch view to plain text mode
Within the MyMainWindow constructor, there will be code that creates and arranges many child widgets of the main window.
When you delete the mainwindow, you would do so like this
delete mw;
delete mw;
To copy to clipboard, switch view to plain text mode
You don't worry about deleting all the pushbuttons, listboxes, labels etc that are within your main window. Why? because QObject takes care of it with its parent-child model.
Well, here is the main function, from my main.cpp, from my Homestead application:
int main( int argc, char * argv[] ) {
app.setQuitOnLastWindowClosed(false);
dlgLogin dlg;
if( dlg.
exec() == QDialog::Accepted ){ hapdb.setHostName(DBHOST);
hapdb.setDatabaseName(DBNAME);
hapdb.setUserName(dlg.dui.leUserName->text());
hapdb.setPassword(dlg.dui.leUserPassword->text());
if ( hapdb.open() ) {
homestead ht;
ht.RevID = dlg.dui.leUserName->text();
ht.show();
app.setQuitOnLastWindowClosed(true);
return app.exec();
} else {
strRejected
= QString("The Login was rejected because: %1").
arg(hapdb.
lastError().
text()).
toLatin1();
QMessageBox::information(0,
"Login Rejected!",strRejected,
return 1;
}
} else {
strRejected
= QString("User Canceled the login!").
toLatin1();
QMessageBox::information(0,
"Login Rejected!",strRejected,
return 2;
}
}
int main( int argc, char * argv[] ) {
QString strRejected = "";
QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(false);
dlgLogin dlg;
if( dlg.exec() == QDialog::Accepted ){
QSqlDatabase hapdb = QSqlDatabase::addDatabase(DBDRIVER);
hapdb.setHostName(DBHOST);
hapdb.setDatabaseName(DBNAME);
hapdb.setUserName(dlg.dui.leUserName->text());
hapdb.setPassword(dlg.dui.leUserPassword->text());
if ( hapdb.open() ) {
homestead ht;
ht.RevID = dlg.dui.leUserName->text();
ht.show();
app.setQuitOnLastWindowClosed(true);
return app.exec();
} else {
strRejected = QString("The Login was rejected because: %1").arg(hapdb.lastError().text()).toLatin1();
QMessageBox::information(0,"Login Rejected!",strRejected,
QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
return 1;
}
} else {
strRejected = QString("User Canceled the login!").toLatin1();
QMessageBox::information(0,"Login Rejected!",strRejected,
QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
return 2;
}
}
To copy to clipboard, switch view to plain text mode
As I understand your comments, since "dlg" is called only once, and that call is in this function, its memory will be recovered when main goes out of scope (i.e., the app closes). And, hapdb is also instantiated only once so its memory, too, will be reclaimed when the app closes. The ht object, where Homestead really operates, is also instantiated only once and like the others, the memory it uses is returned to the OS when the app closes. So, I don't need as the last three lines of main():
delete ht;
delete hapdb;
delete dlg;
delete ht;
delete hapdb;
delete dlg;
To copy to clipboard, switch view to plain text mode
because none of these objects leak memory, even though dlg has a parent:
{
Q_OBJECT
public:
Ui::dlgLoginUi dui;
dlgLogin()
{
dui.setupUi(this);
dui.leUserName->setText("your revid");
dui.leUserPassword->setText("");
dui.leUserName->setFocus();
dui.leUserName->selectAll();
}
};
class dlgLogin : public QDialog
{
Q_OBJECT
public:
Ui::dlgLoginUi dui;
dlgLogin()
{
dui.setupUi(this);
dui.leUserName->setText("your revid");
dui.leUserPassword->setText("");
dui.leUserName->setFocus();
dui.leUserName->selectAll();
}
};
To copy to clipboard, switch view to plain text mode
but homestead does not...
{
Q_OBJECT
public:
...
class homestead : public QMainWindow
{
Q_OBJECT
public:
homestead(QWidget *parent = 0);
...
To copy to clipboard, switch view to plain text mode
Right?
Bookmarks