Using QDataWidgetMapper to show data from a database in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Overview[edit | edit source]

This example shows you how to map a UI component to a database table using 76ytuiytuityutyutututyutyutyu and 76ytuiytuityutyutututyutyutyu in Qt. Searching and updating data can be done without using any SQL statements.

The table 'person' has the following columns:

  • 76ytuiytuityutyutututyutyutyu, this is an autoincrement field
  • 76ytuiytuityutyutututyutyutyu
  • 76ytuiytuityutyutututyutyutyu
  • 76ytuiytuityutyutututyutyutyu


Preconditions[edit | edit source]

For Maemo SQLite development, the following packages must be installed:

  • libqt4-sql
  • libqt4-sql-sqlite
  • libsqlite3-0
  • libsqlite3-dev


Header[edit | edit source]

Dialog for showing and updating a person's last name:

  1. include <QtGui>
  2. include <QtSql>

class MyDialog : public QDialog

   {
   Q_OBJECT
   public:
       MyDialog(int id = 0, QWidget *parent = 0);
       ~MyDialog();
   public slots:
       void save();
       
   private:
       // "person" table mode class
       QSqlTableModel*             tableModel;
       // Maps QLineEdit ui control into "person"
       // table's lastname field
       QDataWidgetMapper*          dataMapper;
       QVBoxLayout*                vboxlayout;
       QLineEdit*                  nameLineEdit;
       QPushButton*                saveButton;
   };


Source[edit | edit source]

MyDialog::MyDialog(int id, QWidget *parent)

   : QDialog(parent)
   {
   vboxlayout = new QVBoxLayout(this);
   nameLineEdit = new QLineEdit(this);
   nameLineEdit->setContextMenuPolicy(Qt::NoContextMenu);
   nameLineEdit->setAlignment(Qt::AlignTop);
   vboxlayout->addWidget(nameLineEdit);
   
   saveButton = new QPushButton("Save",this);
   saveButton->setFixedHeight(60);
   QObject::connect(saveButton, SIGNAL(clicked()), this, SLOT(save()));
   vboxlayout->addWidget(saveButton);
    
   // 'person' table model
   tableModel = new QSqlTableModel(this);
   tableModel->setTable("person");
   // Set where clause
   // QLineEdit shows person lastname that id is given parameter
   tableModel->setFilter(QString("id=%1").arg(id));
   tableModel->select();
   
   // Maps QLineEdit ui control into "person" table's lastname field
   dataMapper = new QDataWidgetMapper(this);
   dataMapper->setModel(tableModel);
   // We want that data is stored only if we call QDataWidgetMapper::submit()
   dataMapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
   dataMapper->setItemDelegate(new QSqlRelationalDelegate(dataMapper));
   dataMapper->addMapping(nameLineEdit, tableModel->fieldIndex("lastname"));
   dataMapper->toFirst();
   setLayout(vboxlayout);
   }

MyDialog::~MyDialog()

   {
   }


void MyDialog::save()

   {
   // Stores data from QLineEdit to database using QDataWidgetMapper
   dataMapper->submit();
   }


Postconditions[edit | edit source]

The UI controls get data from the database and the data can be updated via controls without using any SQL statements.


See also[edit | edit source]