PDA

View Full Version : Class member reference



waynew
20th December 2009, 00:29
Thanks to the folks who have already helped me along with this, but I am still having a little trouble referencing class members.

Here's the relevant part of the header:


class QSqlTableModel;
class QTableView;

private:
QSqlTableModel *model;
QTableView *view;



And part of the cpp:



model = new QSqlTableModel();
view = new QTableView();

QSqlTableModel *model = new QSqlTableModel();
model->setTable("log");
model->select();

QTableView *view = new QTableView;
view->setModel(model);
view->setMinimumSize(1200,100);
view->move(35,80);
view->setSortingEnabled(TRUE);
QHeaderView *header = view->horizontalHeader();
header->setMovable(TRUE);
view->show();


All of that works fine. Problem comes when I try to use model-> or view->
in a local function. No errors, but no results. Like trying to set the view title or apply a filter to the model. Both of these work fine if the code for them is right after the code shown above, but when in a local function like this, they have no effect.



checkPrefs();

void MainWindow::checkPrefs() {
view->setWindowTitle("My Log");
MainWindow::model->setFilter("call = 'k0zav'");
}


And if I check the model assigned to the view in the checkPrefs code, it comes back empty.

So, what am I doing wrong here?

wysota
20th December 2009, 00:40
You have local variables that shadow the ones declared in the class.

waynew
20th December 2009, 01:38
Thanks for the reply. I understand what you are saying, conceptually, but not sure where. Do you mean lines 1 and 2 of the first lines in cpp?
Without those, it crashes when checkPrefs is called.
So I'm confused.

Ok Wysota - I got it now.
Just removed the *model and *view lines and it is working perfectly.
Thanks for your help!