Results 1 to 4 of 4

Thread: Set model not working!!

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    31

    Default Set model not working!!

    I am using QSqlQuerModel in my program and using table view to display the contents of the model.
    But when i set the model nothing displays in the table other than two brown lines at the horizontal and verticla headers.....
    my program code....



    Qt Code:
    1. void win::on_pushButton_clicked()
    2. {
    3.  
    4. QStringList signalList;
    5. QString filename;
    6. filename="C:/Users/Desktop/abc.txt";
    7. headersList=GetheaderNames(filename);
    8.  
    9. QFileInfo file(filename);
    10. createTable(file.baseName(),headersList);
    11. GetFileContent(filename);
    12. QSqlQueryModel plainModel2;
    13. initializeModel(&plainModel2,file.baseName(),headersList);
    14.  
    15. ui->tableView_2->setModel(&plainModel2);
    16. }
    To copy to clipboard, switch view to plain text mode 



    initialiseModel function
    Qt Code:
    1. void initializeModel(QSqlQueryModel *model,QString filename, QStringList headersList)
    2. {
    3. QString queryString=QString("select * from %1").arg(filename);
    4.  
    5. model->setQuery(queryString);
    6. for(int i=0;i<headersList.count();i++)
    7. {
    8. model->setHeaderData(i, Qt::Horizontal, (headersList.at(i)));
    9. }
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 


    plz help me to sort out the problm here!!

  2. #2
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 13 Times in 13 Posts

    Default Re: Set model not working!!

    Is it work?
    Qt Code:
    1. model->setHeaderData(i, Qt::Horizontal, (headersList.at(i)));
    To copy to clipboard, switch view to plain text mode 
    Output headersList.at(i) with qDebug();
    Try create plainModel2 in heap.
    Qt Code:
    1. QSqlQueryModel *plainModel2 = new QSqlQueryModel;
    2. initializeModel( plainModel2, file.baseName(), headersList );
    To copy to clipboard, switch view to plain text mode 


    Added after 5 minutes:


    Check errors
    Qt Code:
    1. model->setQuery(queryString);
    2. if (model->lastError().isValid())
    3. qDebug() << model->lastError().text();
    To copy to clipboard, switch view to plain text mode 
    Last edited by Jonny174; 29th May 2012 at 10:53.

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    31

    Default Re: Set model not working!!

    Thank u jonny......as u identified, i created it on stack......now its working....

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Set model not working!!

    You are creating the model on stack, which will not work in your case, as it will go out of scope after on_pushButton_clicked(0 is exited.

    Create the model on heap.

    QSqlQueryModel* plainModel2 = new QSqlQueryModel(...)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. custom table model is not working
    By aurora in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2012, 20:02
  3. Filtering "NoDotDot" in filesystem model not working
    By aurora in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2012, 10:02
  4. Replies: 1
    Last Post: 13th June 2008, 18:58
  5. Help with getting my custom model working
    By thomaspu in forum Qt Programming
    Replies: 19
    Last Post: 29th July 2007, 18:35

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
  •  
Qt is a trademark of The Qt Company.