PDA

View Full Version : Scope problem maybe?



Nefastious
15th September 2009, 10:09
Hi there guys,

I have a bit of problem here I declare a QSqlTable Model as a public member of a class, but unfortunately to use it in other members of the class (functions) later proves to be quite awful, as the entire program crashes, the same happens when declaring QLineEdit and later trying to use it in other member functions (such as one that adds it to a toolbar). I have an impression this is a scope problem, but I'm not sure how to solve it.

Any additional information will be posted upon request (code aswell).

Thanks in advance,
:)

lyuts
15th September 2009, 11:55
Your question is not enough to answer it or suggest any solution.

Chisum
15th September 2009, 18:32
To solve just about any problem in coding, you need to be specific. So yes, take the first example you sort of give. Post the code for the class you created including the functions where you use it AND then post the error messages that you receive when you try to run it.

Nearly all problems in coding are solvable this way. Basically, this is the same message I received from a FORTRAN consultant in 1963 when I encountered problems in my first program. (Minus the profanity and screaming.).

Gary

ChrisW67
16th September 2009, 08:50
Check that you are defining instances of the objects as member variables or, if you are creating pointers to objects as the member variables, that you are creating the objects with new (in the constructor or somehere else that makes sense) before you try to use them.

Nefastious
16th September 2009, 11:51
Well, as requested here comes the code:


class mainwindow : public QMainWindow {
...
public:
//declaring the SqlTable model
QSqlTableModel *model;
//declaring the searchBox
QLineEdit *searchBox;
...
}

And later on in the implementation file:


//this submits all changes to the database
void mainwindow::save() {
model->submitAll();
}

or even,

/*this funtion sets up everything in the toolbar (buttons, icons, descriptions of buttons, connections)
and menus*/
void mainwindow::setting_up_toolbar_menus() {
...
ui->toolBar->addWidget(searchBox);
searchBox->show();
QAction *searchAct;
...
}

Hope this helps...

lyuts
16th September 2009, 12:00
Do you have



class QSqlTableModel;
class QLineEdit;


before class definition?

ChrisW67
16th September 2009, 23:00
You are declaring pointers to Qt objects as member variable. You don't show code that initialises these pointers. Often this would occur in a constructor:


void mainwindow::mainwindow() ...
{
model = new QSqlTableModel(...);
}

or in the case of the UI elements, just before you add them to the UI;


/*this funtion sets up everything in the toolbar (buttons, icons, descriptions of buttons, connections)
and menus*/
void mainwindow::setting_up_toolbar_menus() {
...
searchBox = new QLineEdit(this);
ui->toolBar->addWidget(searchBox);
searchBox->show();
QAction *searchAct;
...
}
If you try to use the member variables without allocating an instance of the objects then crashes is the sort of thing you'll get (NULL pointer exceptions).