Results 1 to 15 of 15

Thread: How to make QListWidget items editable?

  1. #1
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to make QListWidget items editable?

    I am using Qt 4.3.4. I have created a list widget having some items. I want the items to be editable so that user can modify the contents of the list widget dynamically by double clicking on the list widget item.

    Here's what i am doing:

    Qt Code:
    1. connect (sampleList, SIGNAL (itemDoubleClicked (QListWidgetItem *)), this, SLOT (test (QListWidgetItem *)));
    2.  
    3. void test (QListWidget *item)
    4. {
    5. sampleList->editItem (item);
    6. }
    To copy to clipboard, switch view to plain text mode 

    While creating the list widget items, i am setting the item flags as:

    Qt Code:
    1. item->setFlags (item->flags () & Qt::ItemIsEditable);
    To copy to clipboard, switch view to plain text mode 


    Now, when i run my application, and double click on the list widget item to edit, i get the following error on the terminal:

    "edit: editing failed"
    Now, my query is that how can i make the list widget items editable? I tried modifying the test () function given above as:

    Qt Code:
    1. void test (QListWidget *item)
    2. {
    3. sampleList->openPersistentEditor (item);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Using this code, i am able to edit the item but i am not sure if this is the correct way of doing it. Also, i am not sure about the usage of openPersistentEditor ().
    Last edited by montylee; 29th September 2008 at 10:51.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to make QListWidget items editable?

    Quote Originally Posted by montylee View Post
    Here's what i am doing:

    Qt Code:
    1. connect (sampleList, SIGNAL (itemDoubleClicked (QListWidgetItem *)), this, SLOT (test (QListWidgetItem *)));
    2.  
    3. void test (QListWidget *item)
    4. {
    5. sampleList->editItem (item);
    6. }
    To copy to clipboard, switch view to plain text mode 
    This is not needed, you can safely throw this piece of code to trash. See QAbstractItemView::editTriggers.

    While creating the list widget items, i am setting the item flags as:

    Qt Code:
    1. item->setFlags (item->flags () & Qt::ItemIsEditable);
    To copy to clipboard, switch view to plain text mode 
    It should be:
    Qt Code:
    1. item->setFlags (item->flags () | Qt::ItemIsEditable);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following 3 users say thank you to jpn for this useful post:

    montylee (30th September 2008), TheIndependentAquarius (23rd February 2012), TMan (16th December 2009)

  4. #3
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Cool Re: How to make QListWidget items editable?

    wow, thanks a lot!!!
    The solution you gave worked I just had to replace "&" with "|" and the code worked.

    Strangely, even if i don't give edittrigger, i am able to edit it but i am still using edit triggers as "QAbstractItemView::AllEditTriggers".

    Thanks a lot for your help!!!

    Now, i have to check whether the data entered by the user in the list box item is a number or not. If it is not a number, i have to revert it to the original contents. Other option is to allow the user to enter only numeric values... if you have any idea about that, then please post...i think Qt has a built-in class for validating the input. I'll try to search the QT documentation.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to make QListWidget items editable?

    Quote Originally Posted by montylee View Post
    Now, i have to check whether the data entered by the user in the list box item is a number or not. If it is not a number, i have to revert it to the original contents. Other option is to allow the user to enter only numeric values... if you have any idea about that, then please post...i think Qt has a built-in class for validating the input.
    The delegate is responsible for creating editors. You could reimplement QItemDelegate::createEditor() or QStyledItemDelegate::createEditor(), call the base class implementation to create the editor, check if it's a QLineEdit, and install a validator if it is.
    J-P Nurmi

  6. #5
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make QListWidget items editable?

    is it necessary that i used a delegate for this?
    Can i use the existing code and add number validation to it?

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to make QListWidget items editable?

    What's wrong with delegates? Well, QStandardItemEditorCreator is the other way but then you'll have to subclass QLineEdit (and you'll loose the "ExpandingLineEdit" which is provided by the editor factory by default). I'd definitely go with delegates myself.
    J-P Nurmi

  8. #7
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make QListWidget items editable?

    my doubt is whether i can use delegates with my existing code or do i have to modify my existing code?
    The existing code just uses "IsEditable" flag and i am able to get a line edit when i click on the editable field. I am not attaching any line edit explicitly with the list widget. Can i use delegate with my existing code?

    Actually i have no idea about delegates, so where should i start? Is Qt documentation enough or should i use any other good online tutorial?
    I think there is a input validation example given along with Qt, i'll try to go through it.

  9. #8
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make QListWidget items editable?

    Just checked the LineEdit example that came with Qt. For allowing only integer values in a line edit, following can be used:

    Qt Code:
    1. QValidator *validator = new QIntValidator(100, 999, this);
    2. QLineEdit *edit = new QLineEdit(this);
    3.  
    4. // the edit lineedit will only accept integers between 100 and 999
    5. edit->setValidator(validator);
    To copy to clipboard, switch view to plain text mode 

    Now, since i don't have an explicit Line edit attached to the List widget item, i am trying to find out if i can set the validator for the defaul line edit i am getting when i edit the list box item.

    Is it possible?

  10. #9
    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: How to make QListWidget items editable?

    use a delegate, Qt Assistant is enough for this. look also an example in QTDIR/examples/itemviews/spinboxdelegate/. you don't need to change your code, you only need to create a new class inherits from QStandartItemDelegate and set it for a view using
    Qt Code:
    1. void QAbstractItemView::setItemDelegate ( QAbstractItemDelegate * delegate )
    To copy to clipboard, switch view to plain text mode 

    so, in you case (of cource if you already created a delegate) setting the delegate should look like:
    Qt Code:
    1. ....
    2. m_myListWidget = new QListWidget(this);
    3. m_myListWidget->setItemDelegate(new MyItemDelegate());
    4. ...
    To copy to clipboard, switch view to plain text mode 

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

    montylee (1st October 2008)

  12. #10
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: How to make QListWidget items editable?

    Thanks for the replies!

    I was able to use the delegate in a small application having a QListWidget but the same code is not working in my application

    I am using same code as given in the QTDIR/examples/itemviews/spinboxdelegate example. I have made changes to the main.cpp of the example. Here are the contents:

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QString>
    4. #include <QListWidget>
    5. #include <QListWidgetItem>
    6.  
    7. #include "delegate.h"
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication app(argc, argv);
    12. QListWidget *list = new QListWidget;
    13.  
    14. SpinBoxDelegate delegate;
    15. list->setItemDelegate(&delegate);
    16.  
    17. QListWidgetItem *Item1 = new QListWidgetItem(list);
    18. Item1->setText("1");
    19. Item1->setFlags (Item1->flags () | Qt::ItemIsEditable);
    20. QListWidgetItem *Item2 = new QListWidgetItem(list);
    21. Item2->setText("2");
    22. QListWidgetItem *Item3 = new QListWidgetItem(list);
    23. Item3->setText("3");
    24.  
    25. list->setWindowTitle(QObject::tr("List Box Delegate"));
    26. list->show();
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

    Now, this code is working properly i.e. when i double click on the 1st list widget item, a combo box appears which allows me to choose or enter only integer values.

    When i used the same code in my application, it didn't work. In my application i created a list widget having 3 items. The list widget items are visible normally but when i set the delegate using "setItemDelegate", the list widget comes out as empty i.e. no contents are getting displayed in the list widget.

    Here's a part of my code:
    The header consists of several classes, i have added the delegate class to the same header.

    test.h:
    Qt Code:
    1. // Class for custom spin box delegate
    2. class SpinBoxDelegate : public QItemDelegate
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. SpinBoxDelegate(QObject *parent = 0);
    8.  
    9. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    10. const QModelIndex &index) const;
    11.  
    12. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    13. void setModelData(QWidget *editor, QAbstractItemModel *model,
    14. const QModelIndex &index) const;
    15.  
    16. void updateEditorGeometry(QWidget *editor,
    17. const QStyleOptionViewItem &option, const QModelIndex &index) const;
    18. };
    19.  
    20. // Class for "Test" Screen Dialog
    21. class Test: public QDialog
    22. {
    23. Q_OBJECT
    24.  
    25. public:
    26. Test();
    27.  
    28. private:
    29. QListWidget *m_pList;
    30. };
    To copy to clipboard, switch view to plain text mode 

    test.cpp

    Qt Code:
    1. Test::Test ()
    2. {
    3. m_pList = new QListWidget;
    4. m_pList->setAlternatingRowColors (TRUE);
    5.  
    6. // Set delegate to combo box while editing the scenario events list widget
    7. SpinBoxDelegate delegate;
    8. m_pList->setItemDelegate (&delegate);
    9.  
    10. QListWidgetItem *Item1 = new QListWidgetItem (m_pList);
    11. Item1->setText (tr ("1"));
    12. Item1->setFlags (Item1->flags () | Qt::ItemIsEditable);
    13. QListWidgetItem *Item2 = new QListWidgetItem (m_pList);
    14. Item2->setText (tr ("2"));
    15. QListWidgetItem *Item3 = new QListWidgetItem (m_pList);
    16. Item3->setText (tr ("3"));
    17.  
    18. m_pList->setCurrentRow (0);
    19.  
    20. QVBoxLayout *verLayout1 = new QVBoxLayout;
    21. verLayout1->addWidget (someLabel);
    22. verLayout1->addWidget (m_pList);
    23. }
    To copy to clipboard, switch view to plain text mode 


    As i mentioned, my code works normally and all items inside the list widget are visible. But when i use setItemDelegate(), everything disappears from the list widget. Only the List widget is visible.

    Any ideas?

  13. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to make QListWidget items editable?

    The delegate goes out of scope. Allocate it on the heap:
    Qt Code:
    1. SpinBoxDelegate* delegate = new SpinBoxDelegate(m_pList);
    2. m_pList->setItemDelegate(delegate);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  14. The following user says thank you to jpn for this useful post:

    montylee (1st October 2008)

  15. #12
    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: How to make QListWidget items editable?

    try to create a delegate in the heap, i.e. using new operator.
    so, code should like this
    Qt Code:
    1. m_pList->setItemDelegate (new SpinBoxDelegate);
    To copy to clipboard, switch view to plain text mode 

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

    montylee (1st October 2008)

  17. #13
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make QListWidget items editable?

    wow!!! it worked...
    my problem is now resolved.
    thanks a lot to both of you!

    I have one more query:
    I am not sure why the delegate goes out of scope...is it because the List widget variable is not local?

  18. #14
    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: How to make QListWidget items editable?

    because, QListWidget has global scope in a class and the delegate local scope in ctor of the class.
    Last edited by spirit; 1st October 2008 at 12:30. Reason: updated contents

  19. #15
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make QListWidget items editable?

    thanks for the clarification

Similar Threads

  1. Replies: 1
    Last Post: 5th September 2008, 23:54
  2. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 05:57
  3. Get Visible Items from the QListWidget
    By srj in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 20:13
  4. Qt 4.2: QListWidget changes size of its items
    By KingFish in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 11:06
  5. [Qt4]: Adding centered items in QListWidget
    By Jojo in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2006, 20:04

Tags for this Thread

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.