Results 1 to 5 of 5

Thread: QDataWidgetMapper and QCombobox

  1. #1
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QDataWidgetMapper and QCombobox

    I am using QDataWidgetMapper successfully with QLineEdit and other widgets but it doesn't work with QCombobox.

    If I replace the QCombobox by a QLineEdit, it works: the QLineEdit is updated when I select a line in my table view.
    If I come back to the QCombobox, it doesn't work: the QCombobox is not updated when I select a line in my table view.

    Do you know why ?
    Is it normal or is it a bug ?
    Do you know a simple workaround ?

    Thank you for your help

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    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 and QCombobox

    It's normal. You have to provide a custom delegate for the mapper so that it knows how to convert data between the model and the editor (combobox in this case). As far as I remember there is an article on Qt Quarterly that describes the solution.

  3. The following user says thank you to wysota for this useful post:

    miraks (7th August 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataWidgetMapper and QCombobox

    Quote Originally Posted by wysota View Post
    It's normal. You have to provide a custom delegate for the mapper so that it knows how to convert data between the model and the editor (combobox in this case). As far as I remember there is an article on Qt Quarterly that describes the solution.
    Hi wysota,

    With your answer, I found the following page http://doc.trolltech.com/qq/qq21-datawidgetmapper.html and understood that it doesn't work with QComboBox because of QComboBox doesn't have a user property.

    I didn't use the delegate solution proposed on the page, I preferred use the following one:
    Creation of a new Combo box class with a user property:
    class __attribute__ ((visibility("default"))) SKGComboBox : public KComboBox
    {
    Q_OBJECT
    Q_PROPERTY( QString text READ text WRITE setText USER true)

    public:
    /**
    * Default Constructor
    */
    SKGComboBox(QWidget *parent = 0);

    /**
    * Default Destructor
    */
    virtual ~SKGComboBox();

    /**
    * Get the text for the combo
    * @return the text
    */
    virtual QString text() const;


    /**
    * Set the text for the combo
    * @param iText the text
    */
    virtual void setText (const QString& iText);


    };
    SKGComboBox::SKGComboBox(QWidget *parent)
    : KComboBox(parent)
    {
    }

    SKGComboBox::~SKGComboBox()
    {
    }

    QString SKGComboBox::text() const
    {
    return currentText();
    }

    void SKGComboBox::setText (const QString& iText)
    {
    int pos=findText(iText);
    if (pos==-1)
    {
    pos=0;
    insertItem(pos, iText);
    }
    setCurrentIndex(pos);
    }
    And it works now like for QLineEdit.

    Thank you again.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    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 and QCombobox

    The problem with your solution is that you have to set the list of available choices manually, but that's ok if you're satisfied with it.

    I was referring to this article, by the way:
    http://doc.trolltech.com/qq/qq21-dat...toofferchoices

  6. #5
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataWidgetMapper and QCombobox

    I had same problem with http://doc.trolltech.com/qq/qq21-datawidgetmapper.html,
    but your solution with class SKGComboBox WORK...super..thanks

Similar Threads

  1. QComboBox QSqlQueryModel & relation.
    By matheww in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2007, 04:56
  2. 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.