Results 1 to 5 of 5

Thread: Seg fault QSqlRecord

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Seg fault QSqlRecord

    I am a bit frustrated with seg faults.

    I have the following main
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. bool connection;
    4.  
    5.  
    6. QApplication app(argc, argv);
    7.  
    8. if (!createConnection())
    9. connection = false;
    10.  
    11. else
    12. connection = true;
    13. qDebug()<<"connection: "<<connection;
    14. mainWindow start(connection,0);
    15. start.show();
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    and the connection.h is

    Qt Code:
    1. inline bool createConnection()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    4. db.setDatabaseName("ppl.db");
    5. if (!db.open()) {
    6. QMessageBox::critical(0,QObject::tr("Non riesco a connettermi al database. Controlla il nome o verifica che il file sia presente"),db.lastError().text());
    7. return false;
    8. }
    9. return true;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Now, in mainWindow.cpp I have a QTableView tied with a database:

    Qt Code:
    1. QSqlQueryModel *model = new QSqlQueryModel(this);
    2. model->setQuery("SELECT * FROM persone");
    3. ...
    4. QTableView *listTable = new QTableView(this);
    5. listTable->setShowGrid(false);
    6. listTable->setModel(model);
    7. ...
    8. connect(listTable,SIGNAL(clicked(const QModelIndex &)),this,SLOT(updateDetails(const QModelIndex &)));
    9. }
    10.  
    11. void mainWindow::updateDetails(const QModelIndex &selection)
    12. {
    13. /* Get the selected indexes in the TableView*/
    14. if (selection.isValid()) {
    15. qDebug() << selection.row();
    16. QSqlRecord rec = model->record(0);
    17.  
    18. // QString temp = record.value("nome").toString();
    19. }
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 
    The compilation is good, but when i click on the first row of the Table a seg fault is fired.
    Any help?
    Attached is an excerpt of the gdb output:

    Qt Code:
    1. \#0 QSqlRecord (this=0xbf8a4388, other=@0x16d45a8) at ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h:122
    2. \#1 0x0082d40c in QSqlQueryModel::record (this=0xbf8a54d8, row=0) at models/qsqlquerymodel.cpp:481
    3. \#2 0x0804b726 in mainWindow::updateDetails ()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Seg fault QSqlRecord

    It seems that if I declare

    Qt Code:
    1. model = new QSqlQueryModel(this);
    To copy to clipboard, switch view to plain text mode 

    all works.
    Which is the difference?

  3. #3
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Seg fault QSqlRecord

    Quote Originally Posted by giusepped View Post
    It seems that if I declare

    Qt Code:
    1. model = new QSqlQueryModel(this);
    To copy to clipboard, switch view to plain text mode 

    all works.
    Which is the difference?
    Outch, this happens when only a part of the code is posted. But what I do not understand is why your code compiles at all.

    You do a
    Qt Code:
    1. QSqlQueryModel *model = new QSqlQueryModel(this);
    To copy to clipboard, switch view to plain text mode 
    in your constuctor? Don't know, this part is missing. However, your *model is a local variable, which has absolutely nothing to do with the model in updateDetails(...). Wonder what 'model' the compiler finds in updateDetails(...) at all. Or you have a model pointer as member variable. However, this is not the one you initialize in your constructor.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Seg fault QSqlRecord

    because you have a class member variable model and it was not initilazed.
    in constructor you create a variable with the same name
    Qt Code:
    1. ...
    2. QSqlQueryModel *model = new QSqlQueryModel(this);
    3. ...
    To copy to clipboard, switch view to plain text mode 

    and when you try to access to methods in a model (it is still isn't initilazed) you get seg fault, because model variable has wrong pointer.

  5. #5
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Seg fault QSqlRecord

    Solved.Thank you.
    In fact, in the header I defined
    Qt Code:
    1. QSqlRecord *model;
    To copy to clipboard, switch view to plain text mode 
    That was the error.
    step by step I am going to get accustomed to C++.
    Bye

Similar Threads

  1. Replies: 3
    Last Post: 2nd September 2008, 00:57
  2. Using qsqlrecord?
    By triperzonak in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2008, 12:04
  3. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 17:35
  4. QSqlRecord problem
    By stevey in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2006, 23:32
  5. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 17: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.