Results 1 to 4 of 4

Thread: QTableView and MySQL

  1. #1
    Join Date
    Jul 2006
    Location
    Atlanta, GA
    Posts
    86
    Thanks
    26
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QTableView and MySQL

    Could someone point me to an example of how to populate a QTableView widget created with designer, getting its rows from a MySQL database? Would I just do something like:

    Qt Code:
    1. QSqlQueryModel *tvdsModel = new QSqlQueryModel;
    2. tvdsModel->setQuery("SELECT blah FROM main");
    3.  
    4. ui.tvdsView->setModel(tvdsModel);
    To copy to clipboard, switch view to plain text mode 
    fnmblot
    --------------------------------------
    Gee Ricky, I'm sorry your mom blew up.

  2. #2
    Join Date
    Jul 2006
    Location
    Atlanta, GA
    Posts
    86
    Thanks
    26
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView and MySQL

    I guess all I had to do was think about it and try it before I posted. If it is any consolation, here is what I did:

    Qt Code:
    1. //testTable.cpp
    2. #include "tableTest.h"
    3. #include <QtSql>
    4. //
    5. TableTestDlg::TableTestDlg( QWidget * parent, Qt::WFlags f)
    6. : QDialog(parent, f)
    7. {
    8. tableViewUi.setupUi(this);
    9.  
    10. QSqlQueryModel *tableViewModel = new QSqlQueryModel;
    11. tableViewModel->setQuery("SELECT piece_name, nn, status FROM main WHERE nn !='0' ORDER BY uid");
    12.  
    13. tableViewModel->setHeaderData(0, Qt::Horizontal, "Piece Name");
    14. tableViewModel->setHeaderData(1, Qt::Horizontal, "NN");
    15. tableViewModel->setHeaderData(2, Qt::Horizontal, "Status");
    16.  
    17. tableViewUi.tableView->setModel(tableViewModel);
    18. tableViewUi.tableView->resizeColumnsToContents();
    19. tableViewUi.tableView->setAlternatingRowColors(true);
    20. }
    21. TableTestDlg::~TableTestDlg()
    22. {}
    To copy to clipboard, switch view to plain text mode 
    fnmblot
    --------------------------------------
    Gee Ricky, I'm sorry your mom blew up.

  3. #3
    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: QTableView and MySQL

    Quote Originally Posted by fnmblot View Post
    I guess all I had to do was think about it and try it before I posted.
    It's always a good idea. Also, notice that a bunch of SQL examples is shipped with Qt.
    J-P Nurmi

  4. #4
    Join Date
    Aug 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: QTableView and MySQL

    Hey fnmblot:

    You probably have forgotten about this post, but I would like to thank you for your original question, AND the courtesy of following up with the solution you came up with. This is very helpful to others (like me).

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. QTableView refresh
    By eleanor in forum Qt Programming
    Replies: 9
    Last Post: 6th October 2007, 18:00
  3. MYSQL 5 Table qt model as small Mysql admin
    By patrik08 in forum Qt-based Software
    Replies: 0
    Last Post: 1st May 2007, 09:43
  4. QTableView with a checkbox
    By maxpower in forum Qt Programming
    Replies: 17
    Last Post: 18th February 2007, 09:45
  5. QTableView - Add Row
    By maxpower in forum Qt Programming
    Replies: 1
    Last Post: 24th November 2006, 18:18

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.