Results 1 to 10 of 10

Thread: MySQL database table row value display in QTableView like below image

  1. #1
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default MySQL database table row value display in QTableView like below image

    rsz_screenshot.jpg

    I need to show the primary key column values like specified in the above image. Can it be achieved? As QTableView will only make rows and columns available in the DB table, I need only particular values (of primary key column) iterated with for(each) loop inside while(query.next())...kindly guide me, thank you in advance.

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

    Default Re: MySQL database table row value display in QTableView like below image

    If you want to use QTableView, then you will need either need to use QSqlTableModel to read database or implement a QAbstractTableModel, a proxymodel, and a delegate
    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.

  3. The following user says thank you to Santosh Reddy for this useful post:

    abhiabhi (14th February 2013)

  4. #3
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MySQL database table row value display in QTableView like below image

    Thank you for ur reply , can u plz share an example for each one? and when we use a TableModel, it will be in rowXcolumn view, but I need a plain interface like I stated in my designer file. plz explain me the further approach, thanK u again

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

    Default Re: MySQL database table row value display in QTableView like below image

    Here is an example, see if you can build and run, if not I have also attached the screenshot of the output. I use Sqlite but once can use MySql also

    Qt Code:
    1. //Helper.pro
    2. QT += core gui sql
    3.  
    4. SOURCES += \
    5. main.cpp
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //main.cpp
    2. #include <QtGui>
    3. #include <QDebug>
    4. #include <QSqlQuery>
    5. #include <QSqlTableModel>
    6.  
    7. class MyView : public QWidget
    8. {
    9. public:
    10. explicit MyView(QWidget * parent = 0)
    11. : QWidget(parent)
    12. , mLayout(new QGridLayout(this))
    13. , mGroup(0)
    14. , mModel(0)
    15. , mColumn(0)
    16. {
    17. setLayout(mLayout);
    18. updateView();
    19. }
    20.  
    21. void setModel(QAbstractTableModel *model)
    22. { mModel = model, updateView(); }
    23.  
    24. void setColumn(int column)
    25. { mColumn = column, updateView(); }
    26.  
    27. private:
    28. QGridLayout * mLayout;
    29. QGroupBox * mGroup;
    30. int mColumn;
    31.  
    32. void updateView(void)
    33. {
    34. if(mGroup)
    35. delete mGroup;
    36.  
    37. mGroup = new QGroupBox;
    38.  
    39. if(mGroup->layout())
    40. delete mGroup->layout();
    41.  
    42. QVBoxLayout * layout = new QVBoxLayout;
    43.  
    44. if(mModel != 0)
    45. for(int i = 0; i < mModel->rowCount(); i++)
    46. layout->addWidget(new QRadioButton(mModel->index(i, mColumn).data().toString()));
    47.  
    48. mGroup->setLayout(layout);
    49. mLayout->addWidget(mGroup);
    50. update();
    51. }
    52. };
    53.  
    54. int main(int argc, char *argv[])
    55. {
    56. QApplication app(argc, argv);
    57.  
    58. // Create Sample Databse (can use MYSQL, instead of Sqlite)
    59. QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE", "Test-sqlite");
    60. database.setDatabaseName("Test.sqlite");
    61.  
    62. // open database
    63. if(database.open())
    64. {
    65. // if table does not exists
    66. if(database.tables().count() == 0)
    67. {
    68. // Create table
    69. QString q("CREATE TABLE Employee ( Name varchar(255), Salary int );");
    70.  
    71. QSqlQuery query(q, database);
    72. query.exec();
    73. database.commit();
    74. }
    75. }
    76.  
    77. // Create model for the sql table
    78. QSqlTableModel * model = new QSqlTableModel(&app, database);
    79. model->setTable("Employee");
    80. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    81. model->select();
    82.  
    83. // if not rows exist in table
    84. if(model->rowCount() == 0)
    85. {
    86. // Add sample rows/records
    87. model->insertRows(0, 10);
    88. for(int i = 0; i < model->rowCount(); i++)
    89. {
    90. model->setData(model->index(i, 0), QString("Employee-%1").arg(i + 1));
    91. model->setData(model->index(i, 1), i*555);
    92. }
    93. model->submit();
    94. }
    95.  
    96. // Create a Table View
    97. v1.setModel(model), v1.setWindowTitle("QTableView"), v1.show();
    98.  
    99.  
    100. // Create a MyView 1
    101. MyView v2;
    102. v2.setModel(model), v2.setWindowTitle("MyView 2"), v2.setColumn(0), v2.show();
    103.  
    104. // Create a MyView 2
    105. MyView v3;
    106. v3.setModel(model), v3.setWindowTitle("MyView 3"), v3.setColumn(1), v3.show();
    107.  
    108. return app.exec();
    109. }
    To copy to clipboard, switch view to plain text mode 

    Example.jpg
    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.

  6. The following user says thank you to Santosh Reddy for this useful post:

    abhiabhi (14th February 2013)

  7. #5
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MySQL database table row value display in QTableView like below image

    Yeah! Yeah! you made it, but one question, how to reduce the spacing between one radio button to another??

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

    Default Re: MySQL database table row value display in QTableView like below image

    Qt Code:
    1. void updateView(void)
    2. {
    3. ...
    4. QVBoxLayout * layout = new QVBoxLayout;
    5. layout->setSpacing(0); // << add this
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    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.

  9. The following user says thank you to Santosh Reddy for this useful post:

    abhiabhi (14th February 2013)

  10. #7
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MySQL database table row value display in QTableView like below image

    Hmmm... Just one doubt, if I want to add another Form fields like PushButton or ButtonGroup or some checkboxes, how I would add it? As I dont have the designer file..

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

    Default Re: MySQL database table row value display in QTableView like below image

    You can create a form as you want in void updateView(void).
    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.

  12. #9
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MySQL database table row value display in QTableView like below image

    yes, if I need to add buttons?? plz kindly help me, i am in a bit hurry, i need to add two push buttons (OK and Cancel)
    Last edited by abhiabhi; 14th February 2013 at 12:18.

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

    Default Re: MySQL database table row value display in QTableView like below image

    yes, if I need to add buttons?? plz kindly help me, i am in a bit hurry, i need to add two push buttons (OK and Cancel)
    If you want push buttons then add them, what is stopping you from doing that?
    Did you try somting?

    Any way here is an example (just an example)
    Qt Code:
    1. void updateView(void)
    2. {
    3. if(mGroup)
    4. delete mGroup;
    5.  
    6. mGroup = new QGroupBox;
    7.  
    8. if(mGroup->layout())
    9. delete mGroup->layout();
    10.  
    11. QVBoxLayout * layout = new QVBoxLayout;
    12. layout->setSpacing(0);
    13.  
    14. if(mModel != 0)
    15. for(int i = 0; i < mModel->rowCount(); i++)
    16. layout->addWidget(new QRadioButton(mModel->index(i, mColumn).data().toString()));
    17.  
    18. mGroup->setLayout(layout);
    19. mLayout->addWidget(mGroup, 0, 0, 1, 2); // Here
    20. mLayout->addWidget(new QPushButton("Next >"), 1, 0, 1, 1); // Here
    21. mLayout->addWidget(new QPushButton("Cancel"), 1, 1, 1, 1); // Here
    22. update();
    23. }
    To copy to clipboard, switch view to plain text mode 
    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: 7
    Last Post: 14th August 2015, 20:49
  2. how to save a image file ( say png ) into mysql database?
    By AviMittal in forum Qt Programming
    Replies: 12
    Last Post: 21st July 2011, 12:49
  3. Replies: 2
    Last Post: 17th February 2010, 14:32
  4. Check if a table exists in a mysql database
    By graciano in forum Qt Programming
    Replies: 8
    Last Post: 5th November 2009, 02:44
  5. 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

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.