Results 1 to 7 of 7

Thread: Scope problem maybe?

  1. #1
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Question Scope problem maybe?

    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,

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Scope problem maybe?

    Your question is not enough to answer it or suggest any solution.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Mar 2009
    Location
    California, USA
    Posts
    31
    Thanks
    2
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Cool Re: Scope problem maybe?

    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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Scope problem maybe?

    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.

  5. #5
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Scope problem maybe?

    Well, as requested here comes the code:

    Qt Code:
    1. class mainwindow : public QMainWindow {
    2. ...
    3. public:
    4. //declaring the SqlTable model
    5. //declaring the searchBox
    6. QLineEdit *searchBox;
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    And later on in the implementation file:

    Qt Code:
    1. //this submits all changes to the database
    2. void mainwindow::save() {
    3. model->submitAll();
    4. }
    To copy to clipboard, switch view to plain text mode 

    or even,
    Qt Code:
    1. /*this funtion sets up everything in the toolbar (buttons, icons, descriptions of buttons, connections)
    2.   and menus*/
    3. void mainwindow::setting_up_toolbar_menus() {
    4. ...
    5. ui->toolBar->addWidget(searchBox);
    6. searchBox->show();
    7. QAction *searchAct;
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    Hope this helps...

  6. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Scope problem maybe?

    Do you have

    Qt Code:
    1. class QLineEdit;
    To copy to clipboard, switch view to plain text mode 

    before class definition?
    I'm a rebel in the S.D.G.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Scope problem maybe?

    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:
    Qt Code:
    1. void mainwindow::mainwindow() ...
    2. {
    3. model = new QSqlTableModel(...);
    4. }
    To copy to clipboard, switch view to plain text mode 
    or in the case of the UI elements, just before you add them to the UI;
    Qt Code:
    1. /*this funtion sets up everything in the toolbar (buttons, icons, descriptions of buttons, connections)
    2.   and menus*/
    3. void mainwindow::setting_up_toolbar_menus() {
    4. ...
    5. searchBox = new QLineEdit(this);
    6. ui->toolBar->addWidget(searchBox);
    7. searchBox->show();
    8. QAction *searchAct;
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 
    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).

  8. The following user says thank you to ChrisW67 for this useful post:

    Nefastious (17th September 2009)

Similar Threads

  1. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  2. nmake error during .pro compiling
    By mattia in forum Installation and Deployment
    Replies: 5
    Last Post: 18th June 2008, 10:15
  3. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  5. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.