Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: Segmentation fault

  1. #21
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Segmentation fault

    I think most of the QtSql examples I've seen simply access database connection via the static method QSqlDatabase::database() whenever needed, without the need of storing connection as member variable anywhere. QSqlDatabase::database() seems to be basically a call to QHash::value() in a thread-safe manner so it's not that inefficient at least.
    J-P Nurmi

  2. #22
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation fault

    Ok, I know, I've complicated things a bit here but I simply wanted to have A SINGLE INSTANCE of object that connects to database, i.e., an instance of application is allowed to connect to database ONLY ONCE. Is this right policy or not?
    Qt 5.3 Opensource & Creator 3.1.2

  3. #23
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Red face Re: Segmentation fault

    This thread has really helped me out, but I am still somewhat confused about what appears to be the foundation of the problem (I am fairly new to C++):
    How do you know if you should be creating a pointer or not? How do I know when to use *model or model?

    The documentation from Trolltech (http://doc.trolltech.com/4.3/qsqlquerymodel.html) actually makes it even worse for a beginner because they seem to use the two interchangeably - but when I do so, I get Segmentation faults if I can compile at all:
    Qt Code:
    1. model->setQuery("SELECT name, salary FROM employee");
    2. model->setHeaderData(0, Qt::Horizontal, tr("Name"));
    3. model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
    4.  
    5. QTableView *view = new QTableView;
    6. view->setModel(model);
    7. view->show();
    8.  
    9. We set the model's query, then we set up the labels displayed in the view header.
    10.  
    11. QSqlQueryModel can also be used to access a database programmatically, without binding it to a view:
    12.  
    13. QSqlQueryModel model;
    14. model.setQuery("SELECT * FROM employee");
    15. int salary = model.record(4).value("salary").toInt();
    To copy to clipboard, switch view to plain text mode 

  4. #24
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault

    Quote Originally Posted by Frozenjim View Post
    How do you know if you should be creating a pointer or not?
    It depends on how long you need the object. If you create it on the stack it will be destroyed when it goes out of scope, so all objects that need to exist longer should be created using new operator. If you use a model only within one function, you can create it on the stack. If you pass a model to a view, it has to exist as long as the view, so you have to use new.

    Quote Originally Posted by Frozenjim View Post
    The documentation from Trolltech actually makes it even worse for a beginner because they seem to use the two interchangeably
    The docs assume you know C++.

Similar Threads

  1. Strange segmentation fault
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 4th January 2009, 19:50
  2. Segmentation Fault
    By Krish_ng in forum Qt Programming
    Replies: 8
    Last Post: 7th August 2007, 10:49
  3. Process aborted. Segmentation fault
    By Pragya in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2007, 08:12
  4. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 16:35
  5. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 16:30

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.