Qt-interest Archive, April 2008
Newbie: Lost in a private virtual void
Message 1 in thread
Hi All,
I had a QTableWidget (I'm now using a QTableView) that I wanted to
display some words in from a MySQL database.
To do this I had the following function:
void MainWindow::setupVocabTable()
{
QSqlTableModel *model = new QSqlTableModel;
model->setTable("Vocab");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
tableVocabNewEntries->setModel(model);
}
However when I was compiling my program I got the following error:
/usr/include/qt4/QtGui/qtablewidget.h: In member function 'void
MainWindow::setupVocabTable()':
/usr/include/qt4/QtGui/qtablewidget.h:329: error: 'virtual void
QTableWidget::setModel(QAbstractItemModel*)' is private
mainwindow.cpp:135: error: within this context
make: *** [mainwindow.o] Error 1
I then changed from a QTableWidget to a QTableView and everything
compiled without a problem.
Can someone please tell me why this is?
Also can someone please tell me what the difference is between this line:
QSqlTableModel *model = new QSqlTableModel;
and this one? What is the significance of 'this'?
QSqlTableModel* model = new QSqlTableModel(this);
Is one syntax preferred over another?
Many thanks,
Peter.
--
[ signature omitted ]
Message 2 in thread
Peter wrote:
> I then changed from a QTableWidget to a QTableView and everything
> compiled without a problem.
>
> Can someone please tell me why this is?
QTableWidget is a QTableView with its own internal model based on
QStandardItemModel. It overloads the method as private to stop you from
changing the model.
> Also can someone please tell me what the difference is
> between this line:
> QSqlTableModel *model = new QSqlTableModel;
>
> and this one? What is the significance of 'this'?
> QSqlTableModel* model = new QSqlTableModel(this);
>
> Is one syntax preferred over another?
It's not really s syntax issue. The parameter to QSqlTableModel constructor
defaults to NULL, which implies that you are responsible for deleting it
later. In the second case, you are saying to automatically delete the model
when the QMainWindow is deleted. See the description for QObject for further
info.
The Qt Assistant is your best friend - keep it handy! Especially the links
that say 'List all members, including inherited members'.
Hope that helps,
Tony Rietwyk,
--
[ signature omitted ]
Message 3 in thread
Thank you very much Tony. I appreciate you taking the time to explain
this to me.
>
> It's not really s syntax issue. The parameter to QSqlTableModel constructor
> defaults to NULL, which implies that you are responsible for deleting it
> later. In the second case, you are saying to automatically delete the model
> when the QMainWindow is deleted. See the description for QObject for further
> info.
Is it usually best to have the model automatically deleted when the
QMainWindow is deleted?
Are there any cases where you might not want this to happen?
Thanks again for your time.
Peter.
--
[ signature omitted ]
Message 4 in thread
Understanding your parent-child relationships in QT is critical to
successful QT usage.
When you send in 'this' as the parent, the model will automatically be
deleted when QMainWindow is deleted... also, when the model is deleted
the parent will be notified and take out of the child list.
When you don't want it sent in... If you plan on moving the child to
another thread... If it's a widget that will be placed in a layout
later...
There are other reasons... but they tend to come with experience with QT
Scott
-----Original Message-----
From: Peter E Dennis [mailto:peter.edwin.dennis@xxxxxxxxx]
Sent: Tuesday, April 08, 2008 5:51 PM
To: qt-interest@xxxxxxxxxxxxx
Subject: Re: Newbie: Lost in a private virtual void
Thank you very much Tony. I appreciate you taking the time to explain
this to me.
>
> It's not really s syntax issue. The parameter to QSqlTableModel
constructor
> defaults to NULL, which implies that you are responsible for deleting
it
> later. In the second case, you are saying to automatically delete the
model
> when the QMainWindow is deleted. See the description for QObject for
further
> info.
Is it usually best to have the model automatically deleted when the
QMainWindow is deleted?
Are there any cases where you might not want this to happen?
Thanks again for your time.
Peter.
--
[ signature omitted ]