Results 1 to 2 of 2

Thread: Applying QSqlTableModel to QTableView

  1. #1
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Applying QSqlTableModel to QTableView

    Hi!

    I have QSqlTableModel with sample data from Qt SQL Examples "connection.h" file. If I want to get records using model.record() it works. model.rowCount() works perfectly too. But if I want to apply my model to tableView it doesn't work:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "F:/Qt/2010.01/qt/examples/sql/connection.h"
    4. #include <QtSql>
    5. #include <QDebug>
    6. #include <QMessageBox>
    7. #include <QSqlTableModel>
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14.  
    15. if (!createConnection())
    16. QMessageBox::critical(this, tr("Błąd bazy danych"), tr("Nie można połączyć się z bazą danych."));
    17.  
    18. model.setTable("person");
    19. model.select();
    20.  
    21. ui->tableView->setModel(&model);
    22.  
    23. ui->label->setText(model.record(1).value("firstname").toString());
    24. qDebug() << model.rowCount();
    25. }
    26.  
    27. MainWindow::~MainWindow()
    28. {
    29. delete ui;
    30. }
    31.  
    32. void MainWindow::changeEvent(QEvent *e)
    33. {
    34. QMainWindow::changeEvent(e);
    35. switch (e->type()) {
    36. case QEvent::LanguageChange:
    37. ui->retranslateUi(this);
    38. break;
    39. default:
    40. break;
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

    I post my final result in attachment.

    Ps. What are the differences between "QSqlTableModel model;" and "QSqlTableModel *model = new QSqlTableModel;"?
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Applying QSqlTableModel to QTableView


Similar Threads

  1. Deleting QSqlTableModel and QTableView
    By waynew in forum Qt Programming
    Replies: 7
    Last Post: 24th December 2009, 01:17
  2. Replies: 2
    Last Post: 6th January 2009, 21:55
  3. QTableView and QSqlTableModel problem
    By Misenko in forum Qt Programming
    Replies: 5
    Last Post: 19th August 2008, 22:58
  4. QSqlTableModel and QTableView and sorting
    By JeanC in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2008, 14:22
  5. QTableView and QSQLTableModel
    By raphaelf in forum Qt Programming
    Replies: 6
    Last Post: 4th March 2006, 19:09

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.