Results 1 to 5 of 5

Thread: SQL and QItemDelegate problem

  1. #1
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default SQL and QItemDelegate problem

    I have a table of data with a combo box delegate on one of the columns. I have a problem in creating the delegate efficiently.

    The table data is a joined query so I can have the column I want to edit showing the text it relates to instead of the integer code in the table.

    I can't use a QSqlRelational* object because the related table data is a subset from a table without a useable index and a some WHERE clauses to filter out unrelated rows. My solution is to load the combo box with a query.

    It works, but the combo box is created in the createEditor method, which means every time the field is being edited the query has to be executed. The sensible thing to do here is have the combo box created in the ctr and loaded once...right ?

    My problem is I can't work out how to create the combo box in the ctr. QComboBox requires a QWidget as it's parent (which is supplied via createEditor(QWidget *parent... but the ctr code has a QObject parameter as a parent and the code doesn't compile. This is probably a simple answer but I can't think what it may be. Can anyone help please ?
    Last edited by vieraci; 29th May 2009 at 16:55. Reason: reformatted to look better

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

    Default Re: SQL and QItemDelegate problem

    You don't have to create the combobox in the constructor, You only need to fetch the data there and you can store it for later use. Once you reach createEditor() you just retrieve that data (model, probably) from your storage and set it on the combobox. Actually you should do that in setEditorData() but with createEditor() it will work in most cases as well. Just be aware you can have stale data if you retrieve the contents of the combobox only once.
    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.


  3. #3
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: SQL and QItemDelegate problem

    Quote Originally Posted by wysota View Post
    You only need to fetch the data there and you can store it for later use.
    Yes I could do that, but I'm trying not to duplicate the data un-necessarily.
    The reason I want to create the combo box in the creator is so I could use it like this:

    Qt Code:
    1. class ComboBoxDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. //...all the usual declarations...
    6. void addItem(QString text, int index) const;
    7. void setEditColumn(int colNumber) const;
    8. private:
    9. QComboBox *editor;
    10. int editColumn;
    11. };
    To copy to clipboard, switch view to plain text mode 

    And then I can fill the combo box with data after I create it...

    Qt Code:
    1. ComboBoxDelegate *delegate = new ComboBoxDelegate(this);
    2. delegate->addItem(string, int);
    3. delegate->setEditColumn(int);
    To copy to clipboard, switch view to plain text mode 
    This way the class is re-useable
    Last edited by vieraci; 30th May 2009 at 04:55. Reason: updated contents

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

    Default Re: SQL and QItemDelegate problem

    Quote Originally Posted by vieraci View Post
    Yes I could do that, but I'm trying not to duplicate the data un-necessarily.
    You are not duplicating data.
    And then I can fill the combo box with data after I create it...

    Qt Code:
    1. ComboBoxDelegate *delegate = new ComboBoxDelegate(this);
    2. delegate->addItem(string, int);
    3. delegate->setEditColumn(int);
    To copy to clipboard, switch view to plain text mode 
    This way the class is re-useable
    This way you do duplicate data :-) Just fetch the data into the model in your delegate's constructor and then set the model on a combo box in createEditor or in setEditorData using QComboBox::setModel().
    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.


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

    vieraci (31st May 2009)

  6. #5
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb Solved: SQL and QItemDelegate problem

    Of course ! Why didn't I think of it myself...
    Create the QSqlQueryModel in the constructor, then the rest is as you said.

    Thanks again wysota.

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.