Results 1 to 5 of 5

Thread: QDataWidgetMapper to synchronize widgets with QTableView

  1. #1
    Join Date
    May 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDataWidgetMapper to synchronize widgets with QTableView

    I have always been very happy with Qt's documentation, but when I tried to use QDataWidgetMapper I was a bit disappointed. Is this just supposed to work by magic? It doesn't do that here...

    I have a QTableView with a QAbstractTableModel derived vertical model with four columns. This works very nicely and I can view and edit rows in the table view.
    I also have a dock widget with some line edits as an alternative way to edit the fields. These fields are the one I have used a QDataWidgetMapper for. The problem is just that it doesn't work. When I select a row and edit it in the dock window, the table is updated, but not the other way around. The fields in the dock widget aren't filled at all when I change the selected row in the table view. *Something* is happening when I change the select row though, because when I submit the text from the dock widget edits the correct row is updated.

    The code:
    Qt Code:
    1. mapper = new QDataWidgetMapper();
    2. mapper->setModel(model);
    3. mapper->addMapping(lineEdit1, 0);
    4. mapper->addMapping(lineEdit2, 1);
    5.  
    6. connect(view->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), mapper, SLOT(setCurrentModelIndex(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

    view is a pointer to the table view, model is a painter to the model I use.

    What did I forget to do?
    Last edited by ehamberg; 20th February 2008 at 23:58.

  2. #2
    Join Date
    Jan 2008
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDataWidgetMapper to synchronize widgets with QTableView

    I haven't used this before, but I was just reading the docs today, so I recognized a possible problem...

    From this site
    http://doc.trolltech.com/4.3/qdatawidgetmapper.html

    I see this sample code
    Qt Code:
    1. mapper->setModel(model);
    2. mapper->addMapping(mySpinBox, 0);
    3. mapper->addMapping(myLineEdit, 1);
    4. mapper->addMapping(myCountryChooser, 2);
    5. mapper->toFirst();
    To copy to clipboard, switch view to plain text mode 


    where I notice a difference on the first line,
    but I suspect the problem is not having the last line of this sample code

    just a hunch ...
    good luck

  3. #3
    Join Date
    May 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataWidgetMapper to synchronize widgets with QTableView

    There are no rows in the model when the programme is started. The user explicitly inserts rows in the table (that is, the model) from the gui.

    So there's no «first» row to go to at start.

  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 to synchronize widgets with QTableView

    Did you call QAbstractItemView::setModel() before that connect statement?
    J-P Nurmi

  5. #5
    Join Date
    May 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataWidgetMapper to synchronize widgets with QTableView

    Quote Originally Posted by jpn View Post
    Did you call QAbstractItemView::setModel() before that connect statement?
    Yes, the QTableView's model is set to the same as the datawidgetmapper's.

Similar Threads

  1. QTableView with widgets
    By steg90 in forum Qt Programming
    Replies: 5
    Last Post: 22nd November 2007, 11:46

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.