Results 1 to 2 of 2

Thread: how to avoid switch statements in item delegates

  1. #1
    Join Date
    Mar 2010
    Posts
    107
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default how to avoid switch statements in item delegates

    Hi
    I am implementing a Item Delegate for my tree based model.
    I need to create a different editor depending upon the item being edited.
    At the moment I am using switch statements to decide which editor to supply, but as my model grows so do the switch statements.

    Qt Code:
    1. QWidget* editor;
    2.  
    3. QModelIndex parentIndex = index.model()->parent(index);
    4.  
    5. switch (index.model()->parent(index).row())
    6. {
    7. case 0:
    8. {
    9. switch (index.row())
    10. {
    11. case 0:
    12. case 1:
    13. {
    14. editor = new QLineEdit(parent);
    15. }
    16. break;
    17.  
    18. case 2:
    19. {
    20. Common::Enumerations::surveyUnits().getDisplayStrings(list);
    21. editor = new QComboBox(parent);
    22. ((QComboBox*)editor)->addItems(list);
    23. }
    24. break;
    25. }
    26. }
    27. break;
    28.  
    29. case 1:
    30. {
    31. editor = new QLineEdit(parent);
    32. ((QLineEdit*)editor)->setValidator(new QIntValidator());
    33. }
    34. break;
    35. }
    36. return editor;
    To copy to clipboard, switch view to plain text mode 


    Is there a better way to achieve this?

    TIA

  2. #2
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to avoid switch statements in item delegates

    How do you put the data into the model?
    If it's a derivative of QAbstractItemModel you could use setData with your own DataRole to save a value from an enum, something like

    Qt Code:
    1. enum EditorType {
    2. LineEditEditor,
    3. ComboBoxEditor,
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    Then you only need one switch

Similar Threads

  1. QtSql: Maximum number of PREPARE statements
    By kayssun in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2010, 09:51
  2. QtSql: execBatch() with SELECT statements
    By herrmarder in forum Qt Programming
    Replies: 2
    Last Post: 28th January 2010, 13:43
  3. How to avoid unnecessary chars in QStringList item?
    By crazymoonboy in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2009, 11:02
  4. Replies: 2
    Last Post: 12th June 2009, 10:55
  5. reading fprintf statements from console in qt
    By KrishnaKishan in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2007, 10:00

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.