Results 1 to 8 of 8

Thread: QDataWidgetmapper

  1. #1
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default QDataWidgetmapper

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataWidgetmapper

    Could you please show us what have you tried to do? We can't correct your code without it.

  3. #3
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDataWidgetmapper

    Thanks for your attention wysota. This what i have tried to do so far:

    Qt Code:
    1. x::x(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. setupUi(this);
    5.  
    6.  
    7.  
    8. QSqlTableModel *model = new QSqlTableModel(this);
    9. model->setTable("user_info");
    10.  
    11.  
    12.  
    13. QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
    14. mapper->setModel(model);
    15.  
    16. mapper->addMapping(spinBox, 0);
    17. mapper->addMapping(lineEdit, 1);
    18. mapper->addMapping(lineEdit_2, 2);
    19. mapper->addMapping(spinBox_2, 3);
    20. mapper->toFirst();
    To copy to clipboard, switch view to plain text mode 

    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".

    Qt Code:
    1. #ifndef X_H
    2. #define X_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_x.h"
    6.  
    7. class x : public QMainWindow,public Ui_xClass
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. x(QWidget *parent = 0, Qt::WFlags flags = 0);
    13. ~x();
    14.  
    15.  
    16. };
    To copy to clipboard, switch view to plain text mode 

    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.
    Attached Files Attached Files

  4. #4
    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: QDataWidgetmapper

    Looks like you forgot to populate the model, try calling QSqlTableModel::select() after setting the table.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDataWidgetmapper

    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.

  6. #6
    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: QDataWidgetmapper

    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?
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    locus (20th January 2007)

  8. #7
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: QDataWidgetmapper

    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.



    locus

  9. #8
    Join Date
    May 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: QDataWidgetmapper

    Quote Originally Posted by jpn View Post
    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?

Similar Threads

  1. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 10:50

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.