Results 1 to 6 of 6

Thread: [Solved] QSqlTableModel issues..

  1. #1
    Join Date
    Nov 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [Solved] QSqlTableModel issues..

    I am having an issue with a school project. I am supposed to create a ballot server which can receive votes and tally then up. The storage is supposed to use SQL. One of the requirements is that I use a QSqlTableModel and QTableView to display the candidates and the amount of votes they current have. I am able to populate the table but am unable to view the data using a QSqlTableModel and QTableView. The window shows but it is completely empty.

    The code is as follows:
    Qt Code:
    1. void server::initializeModel(QSqlTableModel *model, const QString tableName){
    2.  
    3. qDebug() << database.open("awesomeUser", "awesomePassword");
    4. model->setTable(tableName);
    5. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    6. model->select();
    7. model->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
    8. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Votes"));
    9. }
    10.  
    11. QTableView* server::createView(const QString &title, QSqlTableModel *model){
    12.  
    13. QTableView *view = new QTableView;
    14. view->setModel(model);
    15. view->setWindowTitle(title);
    16. return view;
    17. }
    18.  
    19. void server::votersTable(){
    20.  
    21. qDebug() << database.open("awesomeUser", "awesomePassword");
    22.  
    23.  
    24. initializeModel(&model, "Candidates");
    25.  
    26. QTableView *view = createView(QObject::tr("Voters"), &model);
    27. view->show();
    28. }
    To copy to clipboard, switch view to plain text mode 
    votersTable() is called from an on click signal from a button in the UI.

    If anyone could tell me where I went wrong, I would very much appreciate it.

    For reference, I know my Sql table has data inside. I run the following code:
    Qt Code:
    1. { query.exec("DROP TABLE Candidates"); query.exec("CREATE TABLE Candidates (candidate TEXT, votes INTEGER)"); query.exec("INSERT INTO Candidates (candidate, votes) VALUES ('Buggs_Bunny', 0)"); query.exec("INSERT INTO Candidates (candidate, votes) VALUES ('Daffy_Duck', 0)"); query.exec("INSERT INTO Candidates (candidate, votes) VALUES ('Road_Runner', 0)"); query.exec("INSERT INTO Candidates (candidate, votes) VALUES ('Wile_E_Coyote', 0)"); query.exec("SELECT candidate, votes FROM Candidates");
    2.  
    3. }
    4.  
    5. qDebug() << "Candidates: "; while(query.next()){ qDebug() << query.value(0).toString() << " " << query.value(1).toString(); }
    To copy to clipboard, switch view to plain text mode 

    and receive the following output:
    Qt Code:
    1. Candidates:
    2. "Buggs_Bunny" "0"
    3. "Daffy_Duck" "0"
    4. "Road_Runner" "0"
    5. "Wile_E_Coyote" "0"
    To copy to clipboard, switch view to plain text mode 


    I believe this is enough code to demonstrate the setup. For the complete code, pastebin has it at http://pastebin.com/r4tCtTcn

    Please let me know if you can figure out whats going on with this. It would be greatly appreciated!
    Last edited by Nedlinin; 30th November 2010 at 00:14. Reason: Solved

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlTableModel issues..

    Your model is created on stack and when the votersTable() method returns it goes out of scope and is destroyed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel issues..

    Quote Originally Posted by wysota View Post
    Your model is created on stack and when the votersTable() method returns it goes out of scope and is destroyed.
    I love you.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlTableModel issues..

    Quote Originally Posted by Nedlinin View Post
    I love you.
    I'm sorry I can't return the feeling
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel issues..

    Quote Originally Posted by wysota View Post
    I'm sorry I can't return the feeling
    Haha. I appreciate the assistance. Sometimes the easiest reasons for the problems elude me.

  6. #6
    Join Date
    Nov 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel issues..

    i have a issues too when i use QSqlTableModel
    setfilter("limit 4")

Similar Threads

  1. QSqlTableModel
    By codeman in forum Qt Programming
    Replies: 7
    Last Post: 4th May 2009, 11:04
  2. QSqlTableModel and LIKE
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2008, 08:45
  3. QSqlTableModel Help pls
    By munna in forum Newbie
    Replies: 1
    Last Post: 7th March 2006, 19:40
  4. QSqlTableModel
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 12:35
  5. QSqlTableModel Help pls
    By munna in forum Newbie
    Replies: 1
    Last Post: 26th January 2006, 07:58

Tags for this Thread

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.