PDA

View Full Version : QDataWidgetmapper



locus
20th January 2007, 06:03
I really would like to know how to get this class to work. The code i have written so far in trying to get it to work is as follows:

the documentation says:


The following code will map the columns of the model to widgets called mySpinBox, myLineEdit and myCountryChooser:
QDataWidgetMapper *mapper = new QDataWidgetMapper;
mapper->setModel(model);
mapper->addMapping(mySpinBox, 0);
mapper->addMapping(myLineEdit, 1);
mapper->addMapping(myCountryChooser, 2);
mapper->toFirst();

i have tried this, using some editors that have in a form i created, but the data form my database is not being displayed. Any help on how to get this to work will be greatly appreciated.
using qt 4.2

wysota
20th January 2007, 10:02
Could you please show us what have you tried to do? We can't correct your code without it.

locus
20th January 2007, 15:09
Thanks for your attention wysota. This what i have tried to do so far:



x::x(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
setupUi(this);



QSqlTableModel *model = new QSqlTableModel(this);
model->setTable("user_info");



QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
mapper->setModel(model);

mapper->addMapping(spinBox, 0);
mapper->addMapping(lineEdit, 1);
mapper->addMapping(lineEdit_2, 2);
mapper->addMapping(spinBox_2, 3);
mapper->toFirst();



Class "x" inherits a form that i created. This form was created in designer with the editors (lineEdit, lineEdit_2,ect) that were mapped to "mapper".

The code above is the constructor for class "x". Below is the declaration for class "x".



#ifndef X_H
#define X_H

#include <QtGui/QMainWindow>
#include "ui_x.h"

class x : public QMainWindow,public Ui_xClass
{
Q_OBJECT

public:
x(QWidget *parent = 0, Qt::WFlags flags = 0);
~x();


};


This was simply an attempt to get QDatawidgetmapper to work as explained in the docs, but i would really like to use it in my project.

my connections to the database were done in main, and works fine as i tested it by assigning the model to a QTableView, and the information came up. Nothing comes up in the widgets that were mapped though.:confused:

jpn
20th January 2007, 17:39
Looks like you forgot to populate the model, try calling QSqlTableModel::select() after setting the table.

locus
20th January 2007, 20:46
Hey jpn,
thanks a million for the response man, i've been on this thing for a week on and off.
I tried "select", but the model was not populated for some reason. The call returns a bool, so i tested it, and it returned false.

Could you send me the code that you used, it would be greatly appreciated.

I am trying to figure out now why the model is not being populated, my database connection seems to be good, but any suggestions you can give will be welcomed.

jpn
20th January 2007, 20:50
I just played around with one of my pet projects.. ;)

Notes:
- in the example of yours, createConnection() is never called
- are you sure that such table "user_info" exists in the database?

locus
20th January 2007, 21:16
yow man,

jpn,

can't thank you enuff man, you are right, i did not call createconnection, i was too fixated on the QDataWidgetmapper, but i have learnt my lesson; i'll make sure i don't overlook the little things. So i have connected, and this thing works like a charm.

:D

locus

dedantu
5th June 2012, 12:24
Looks like you forgot to populate the model, try calling QSqlTableModel::select() after setting the table.

hi there
i might look late on this but just help me out. am trying to learn qt but the problem is mapping data on the interface. if u dont mind would you send me a fully working example of your code please?