Results 1 to 3 of 3

Thread: problem in population QStandardItemModel from Mysql database

  1. #1
    Join Date
    Oct 2008
    Location
    Dar es Salaam,Tanzania
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default problem in population QStandardItemModel from Mysql database

    Hi guys ,
    I tried to populate QStandardItemModel from Mysql database,but when i view the Tableview only the last low from the database is displayed in the tableview.
    where did i go wrong?

    Here are the codes related to my problem,
    Qt Code:
    1. customers::customers(QWidget *parent) :
    2. QWidget(parent),
    3. m_ui(new Ui::customers)
    4. {
    5. m_ui->setupUi(this);
    6. createMainTable();
    7. populateMainTable();
    8. }
    9. void customers:: createMainTable()
    10. {
    11.  
    12. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    13. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    14. model->setHeaderData(2, Qt::Horizontal, QObject::tr("First Name"));
    15. model->setHeaderData(3, Qt::Horizontal, QObject::tr("Category"));
    16. model->setHeaderData(4, Qt::Horizontal, QObject::tr("Total Amount"));
    17.  
    18. m_ui->customerView->setModel(model);
    19. m_ui->customerView->setSelectionBehavior(QAbstractItemView::SelectRows);
    20. m_ui->customerView->setSelectionMode(QAbstractItemView::SingleSelection);
    21. m_ui->customerView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    22.  
    23. m_ui->customerView->setColumnWidth(0, 50);
    24. m_ui->customerView->setColumnWidth(1, 180);
    25. m_ui->customerView->setColumnWidth(2, 180);
    26. m_ui->customerView->setColumnWidth(3, 90);
    27. m_ui->customerView->setColumnWidth(4, 90);
    28.  
    29.  
    30. m_ui->customerView->verticalHeader()->hide();
    31.  
    32. //connect(m_ui->customerView->, SIGNAL(clicked(QModelIndex)), this, SLOT(changeClientInfos(QModelIndex)));
    33.  
    34. }
    35. void customers::populateMainTable() {
    36.  
    37. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    38. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    39. model->setHeaderData(2, Qt::Horizontal, QObject::tr("First Name"));
    40. model->setHeaderData(3, Qt::Horizontal, QObject::tr("Category"));
    41. model->setHeaderData(4, Qt::Horizontal, QObject::tr("Total Amount"));
    42.  
    43. QSqlQuery query;
    44. query.exec(QString("SELECT id,LastName, FirstName, Category,Amount FROM customer"));
    45. while (query.next()) {
    46.  
    47. db_id = query.value(0).toString();
    48. db_LastName = query.value(1).toString();
    49. db_FirstName = query.value(2).toString();
    50. db_Category = query.value(3).toString();
    51. db_Amount = query.value(4).toString();
    52.  
    53. }
    54. int no_cus = query.exec("SELECT COUNT(*) FROM customer");
    55. for (int i = 0; i < no_cus; i++) {
    56.  
    57. model->setItem(i, 0, new QStandardItem(db_id));
    58. model->setItem(i, 1, new QStandardItem(db_LastName));
    59. model->setItem(i, 2, new QStandardItem(db_FirstName));
    60. model->setItem(i, 3, new QStandardItem(db_Category));
    61. model->setItem(i, 4, new QStandardItem(db_Amount));
    62.  
    63. }
    64.  
    65. m_ui->customerView->setModel(model);
    66.  
    67.  
    68. }
    69.  
    70. void customers::changeEvent(QEvent *e)
    71. {
    72. QWidget::changeEvent(e);
    73. switch (e->type()) {
    74. case QEvent::LanguageChange:
    75. m_ui->retranslateUi(this);
    76. break;
    77. default:
    78. break;
    79. }
    80. }
    81.  
    82. /* Get data from a form and insert into database */
    83.  
    84. void customers::on_newCustomer_clicked()
    85. {
    86. dialog = new newCustomer(this);
    87. int accepted = dialog->exec();
    88. if(accepted == 1 )
    89. {
    90.  
    91. QList<QString> list = dialog->addCustomer();
    92.  
    93. QSqlQuery query;
    94. query.exec("INSERT INTO customer (LastName, FirstName, Category, Amount) VALUES('" + list[0] + "', '" + list[1] + "', '" +list[2] + "', '" + list[3] + "')");
    95. populateMainTable();
    96. }
    97. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: problem in population QStandardItemModel from Mysql database

    did you try to use QSqlTableModel/QSqlQueryModel?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in population QStandardItemModel from Mysql database

    You would be better off using one of the prebuilt SQL models, but I think your problem is that you have created your QStandardItemModel to contain no rows!

    Try changing the zero to something else or using the appendRow() method.

Similar Threads

  1. Submitting QSqlRecord to MySQL database
    By soxs060389 in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2009, 08:54
  2. Replies: 3
    Last Post: 6th May 2009, 11:16
  3. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48
  4. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 17:50
  5. Replies: 8
    Last Post: 7th March 2006, 13:40

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.