Results 1 to 6 of 6

Thread: Qt 4.7, QDataWidgetMapper I can't figure out how to get this working at all.

  1. #1
    Join Date
    May 2010
    Posts
    14
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Qt 4.7, QDataWidgetMapper I can't figure out how to get this working at all.

    I have a working model derived from QAbstractTableModel. It's currently happily populating lists and tables with the appropriate values.

    I want to display a selected item in a widget view with some labels, text fields, etc.

    Currently, I'm just trying to statically display the first item in the list to see it work. Code references are below.

    First, I did not use mapper->setCurrentModelIndex(), but the mapper->toFirst() method always returned a QModelIndex of (-1,-1).

    I'm having a lot of issues debugging because this seems to do virtually nothing.

    It appears like it cycles through the items and calls the data() method with the Qt::EditRole, but that's it.

    Currently, editing of values is not implemented. I just need to display them in the widget fields. Am I not using QDataWidgetMapper correctly? Is it only for editable widgets? Why is it only calling the EditRole and never the DisplayRole?

    Any tips are greatly appreciated.

    My Model:

    Qt Code:
    1. #ifndef COMPONENTMODEL_H
    2. #define COMPONENTMODEL_H
    3. #include <QAbstractTableModel>
    4. #include <QResource>
    5. #include <QStringList>
    6. #include <QIcon>
    7.  
    8. #include "Common/Global.h"
    9. #include "Components/ComponentList.h"
    10. #include "Components/ComponentInterface.h"
    11.  
    12. namespace kex
    13. {
    14. class ComponentModel : public QAbstractTableModel
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. ComponentModel(int types = ComponentInterface::AllComponents,
    20. QObject *parent = 0);
    21.  
    22. /** \brief Default destructor.
    23.   *
    24.   * Copyright 2010 KSpace MRI. All Rights Reserved.
    25.   *
    26.   * \author James Kyle
    27.   * \author $LastChangedBy$
    28.   * \date 2010-4-12
    29.   * \date $LastChangedDate$
    30.   * \version $Rev$
    31.   **/
    32. ~ComponentModel() {}
    33.  
    34. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    35. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    36.  
    37. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
    38. QVariant headerData(int section, Qt::Orientation orientation,
    39. int role) const;
    40.  
    41. private:
    42. const ComponentList::ComponentQList *_componentList;
    43. public slots:
    44. void updateComponentList();
    45. };
    46.  
    47.  
    48. }
    49. #endif // COMPONENTMODEL_H
    To copy to clipboard, switch view to plain text mode 

    My Model's data() method:

    Qt Code:
    1. QVariant ComponentModel::data(const QModelIndex &index, int role) const
    2. {
    3. QVariant result;
    4. if (role == Qt::DecorationRole)
    5. {
    6. QIcon icon(":/images/other/Science-64.png");
    7. result = icon;
    8. } else if (role == Qt::DisplayRole)
    9. {
    10. qDebug() << "called model data() display role";
    11.  
    12. if (index.row() < _componentList->count() &&
    13. index.row() >= 0 &&
    14. index.column() >= 0 &&
    15. index.column() < columnCount())
    16. {
    17. qDebug() << "valid index";
    18.  
    19. AbstractComponent::Pointer comp = (*_componentList)[index.row()];
    20.  
    21. if (index.column() == 0)
    22. {
    23. qDebug() << "column 0";
    24.  
    25. result.setValue(comp->name());
    26. }
    27. else {
    28. result.setValue(QString("a column value"));
    29. }
    30. }
    31. }
    32. return result;
    33. }
    To copy to clipboard, switch view to plain text mode 

    The method that configures the QDataWidgetMapper

    Qt Code:
    1. void MainWindow::setUpWidgetMapper()
    2. {
    3. // set up the data mapper for displaying
    4. ComponentModel *model = new ComponentModel(ComponentInterface::AllComponents,
    5. this);
    6. mapper->setModel(model);
    7. mapper->addMapping(componentNameLabel, 0);
    8. mapper->addMapping(typeNameLabel, 1);
    9. mapper->addMapping(componentDurationLabel, 4);
    10. mapper->addMapping(componentDescriptionTextEdit, 3);
    11. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    12. mapper->setCurrentModelIndex(model->index(0,0));
    13. mapper->toFirst();
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Posts
    14
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.7, QDataWidgetMapper I can't figure out how to get this working at all.

    Further investigation and testing has revealed that QDataWidgetMapper only seems to work for editable widgets like QLineEdit. I've filed a bug on this issue: http://bugreports.qt.nokia.com/browse/QTBUG-10672

    It's unclear to me whether it's an intentional, but (and I hope I'm not missing something) undocumented limitation or if it's an actual bug.

    If anyone would like a full working example of this behavior, just download Qt's example projects here => http://doc.trolltech.com/qq/qq21-datawidgetmapper.html

    Then change any of the QLineEdit's to their associated QLabel's and nothing happens.

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

    Default Re: Qt 4.7, QDataWidgetMapper I can't figure out how to get this working at all.

    Quote Originally Posted by jkyle View Post
    Further investigation and testing has revealed that QDataWidgetMapper only seems to work for editable widgets like QLineEdit.
    Naah... that's not true. I used it many times with labels and such. Maybe you just didn't tell it which property it should use? If you can craft a minimal compilable example reproducing the problem, I'm sure we can work this out.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2010
    Posts
    14
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.7, QDataWidgetMapper I can't figure out how to get this working at all.

    The simple example provided in the docs is a great representation of what I'm doing. Downloaded here: http://doc.trolltech.com/qq/qq21-datawidgetmapper.html, the subproject "simplewidgetmapper". I just changed the QLineEdit pointers to QLabel pointers. It's short enough where I'll just put the constructor implementation here with a couple of comments, the rest of the app is exactly the same:

    *edit* And thanks! I've been banging my head on the desk over this one :P

    Qt Code:
    1. Window::Window(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. setupModel();
    5.  
    6. nameLabel = new QLabel(tr("Na&me:"));
    7. nameEdit = new QLineEdit();
    8. addressLabel = new QLabel(tr("&Address:"));
    9. addressEdit = new QTextEdit();
    10. ageLabel = new QLabel(tr("A&ge (in years):"));
    11. ageSpinBox = new QSpinBox();
    12. nextButton = new QPushButton(tr("&Next"));
    13. previousButton = new QPushButton(tr("&Previous"));
    14.  
    15. nameLabel->setBuddy(nameEdit);
    16. addressLabel->setBuddy(addressEdit);
    17. ageLabel->setBuddy(ageSpinBox);
    18.  
    19. mapper = new QDataWidgetMapper(this);
    20. mapper->setModel(model);
    21. /*
    22.   When we change the below from Edit's to Labels, it doesn't work.
    23.   */
    24. // This used to be mapper->addMapping(nameEdit, 0);
    25. mapper->addMapping(nameLabel, 0);
    26. // This used to be mapper->addMapping(addressEdit, 1);
    27. mapper->addMapping(addressLabel, 1);
    28. mapper->addMapping(ageSpinBox, 2);
    29.  
    30. connect(previousButton, SIGNAL(clicked()),
    31. mapper, SLOT(toPrevious()));
    32. connect(nextButton, SIGNAL(clicked()),
    33. mapper, SLOT(toNext()));
    34. connect(mapper, SIGNAL(currentIndexChanged(int)),
    35. this, SLOT(updateButtons(int)));
    36.  
    37. QGridLayout *layout = new QGridLayout();
    38. layout->addWidget(nameLabel, 0, 0, 1, 1);
    39. layout->addWidget(nameEdit, 0, 1, 1, 1);
    40. layout->addWidget(previousButton, 0, 2, 1, 1);
    41. layout->addWidget(addressLabel, 1, 0, 1, 1);
    42. layout->addWidget(addressEdit, 1, 1, 2, 1);
    43. layout->addWidget(nextButton, 1, 2, 1, 1);
    44. layout->addWidget(ageLabel, 3, 0, 1, 1);
    45. layout->addWidget(ageSpinBox, 3, 1, 1, 1);
    46. setLayout(layout);
    47.  
    48. setWindowTitle(tr("Simple Widget Mapper"));
    49. mapper->toFirst();
    50. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2010
    Posts
    14
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.7, QDataWidgetMapper I can't figure out how to get this working at all.

    With the hint of looking at properties, I made some changes. I'm getting data into the labels now but it feels odd.

    For example:

    Qt Code:
    1. mapper->addMapping(myQLabel, 0, "text");
    To copy to clipboard, switch view to plain text mode 

    But as I noticed before, this calls the Model's Qt::EditRole. So in my model I did:

    Qt Code:
    1. if (role == Qt::EditRole)
    2. {
    3. result = index.data(Qt::DisplayRole);
    4. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if this is the correct way of going about it?

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

    Default Re: Qt 4.7, QDataWidgetMapper I can't figure out how to get this working at all.

    Yes, that's ok, the data widget mapper uses the edit role to get the contents of the model. If you want to override that, you can provide it with your own item delegate with reimplemented setEditorData() method. There you can make the delegate query for the display role directly if that's what you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Draw figure underneath other figure
    By Mnemonic in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2010, 13:38
  2. figure out?
    By uygar in forum Qt Programming
    Replies: 3
    Last Post: 20th May 2009, 08:19
  3. How to put text labels on a figure
    By tommy in forum Qt Programming
    Replies: 3
    Last Post: 7th May 2009, 16:33
  4. Put figure in a grid
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2008, 03:01
  5. yet another easy task i cant figure out
    By sincnarf in forum Qt Programming
    Replies: 5
    Last Post: 16th October 2007, 05: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
  •  
Qt is a trademark of The Qt Company.