Results 1 to 3 of 3

Thread: QItemDelegate use in QTableWidget

  1. #1
    Join Date
    Apr 2008
    Posts
    35
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QItemDelegate use in QTableWidget

    Hi,

    delegate class
    Qt Code:
    1. QCLDelegate::QCLDelegate(QObject *parent)
    2. : QItemDelegate(parent)
    3. {
    4. }
    5.  
    6. QWidget *QCLDelegate::createEditor(QWidget *parent,
    7. const QStyleOptionViewItem & /* option */,
    8. const QModelIndex &index) const
    9. {
    10. QComboBox *comboBox = new QComboBox(parent);
    11. if (index.column() == 2) {
    12. for(int i = 1970; i < 2014 ; i++){
    13. QVariant v(i);
    14. comboBox->addItem(v.toString());
    15. }
    16. } else if (index.column() == 3) {
    17. comboBox->addItem(tr("BRRip"));
    18. comboBox->addItem(tr("DVDRip"));
    19. comboBox->addItem(tr("Blu-Ray"));
    20. comboBox->addItem(tr("Other"));
    21. } else if (index.column() == 4) {
    22. comboBox->addItem(tr("Yes"));
    23. comboBox->addItem(tr("No"));
    24. }
    25. connect(comboBox, SIGNAL(activated(int)), this, SLOT(emitCommitData()));
    26.  
    27. return comboBox;
    28. }
    To copy to clipboard, switch view to plain text mode 

    and here is how i add to the table widget

    Qt Code:
    1. bool QCLMovies::parseElements(QDomNode &c, int row)
    2. {
    3. while(!c.isNull()){
    4. QDomElement k = c.toElement();
    5. if(k.text().isEmpty())
    6. return false;
    7. if(labels.indexOf(k.tagName()) == -1)
    8. return false;
    9. //Adding to table widget
    10. setItem(row, labels.indexOf(k.tagName()), new QTableWidgetItem(k.text()));
    11. c = c.nextSibling();
    12. }
    13.  
    14. return true;
    15. }
    To copy to clipboard, switch view to plain text mode 

    the class declaration is QCLMovies: public QTableWidget

    I am trying to read an XML file into the QTableWidget, with columns 2,3 and 4 as combo boxed and columns 0 and 1 as Qtablewidgetitem (QString), but when i start the application it shows all combo boxes..

    Please help me out

    Thanks
    Last edited by arpspatel; 26th October 2009 at 22:46. Reason: updated contents

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QItemDelegate use in QTableWidget

    in QCLDelegate::createEditor you should write additional condition
    Qt Code:
    1. QWidget *QCLDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem & /* option */,
    3. const QModelIndex &index) const
    4. {
    5. if (index.column() == 2 || index.column() == 3 || index.column() == 4) {
    6. //creating and customization of comboboxes
    7. return comboBox;
    8. }
    9.  
    10. return new QLineEdit(parent);
    11. }
    To copy to clipboard, switch view to plain text mode 
    you also should be careful in other Delegate's method where an editor is used as method's parameter, you should cast each editor to concert type, because now you have two different editor's type -- QComboBox and QLineEdit.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    arpspatel (27th October 2009)

  4. #3
    Join Date
    Apr 2008
    Posts
    35
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QItemDelegate use in QTableWidget

    Thanks it worked perfectly....

Similar Threads

  1. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 23:29
  2. QTableWidget issues
    By Djony in forum Qt Programming
    Replies: 42
    Last Post: 19th December 2006, 23:27
  3. print QTableWidget
    By chak_med in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 18:46
  4. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

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.