Results 1 to 14 of 14

Thread: Help using the QTreeWidget persistent editor.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2007
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help using the QTreeWidget persistent editor.

    Well, let me try to describe:

    I have a QTreeWidget and I need to use the persistent editor to edit the text of it´s items because it is better in an aplication than opening a small dialog for the user to edit the item.

    What I need to do, is check every time the user edits the text of an item if it isn´t equal to the text of the other items. In order to do that, I have to catch when the editor gets closed (know when the user has finished editing the item). So, if there is another item with the same text, than I show a QMessageBox warning the user the item must have a different text, so I open the persistent editor again for the user to change the text.


    That´s about it.

    Thanx for all your help people!

  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: Help using the QTreeWidget persistent editor.

    Quote Originally Posted by Billy Lee Black View Post
    What I need to do, is check every time the user edits the text of an item if it isn´t equal to the text of the other items. In order to do that, I have to catch when the editor gets closed (know when the user has finished editing the item).
    QLineEdit is used as editor provided that it's textual data. Perhaps you could make use of QLineEdit::editingFinished()?
    Qt Code:
    1. class ItemDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ItemDelegate(QObject* parent = 0)
    7. : QItemDelegate(parent) { }
    8.  
    9. QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    10. {
    11. QWidget* editor = QItemDelegate::createEditor(parent, option, index);
    12. // following cast & check aren't actually needed for the connect
    13. // you'll get a warning if it's not a QLineEdit (in case of other than textual data)
    14. QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(editor);
    15. if (lineEdit)
    16. connect(lineEdit, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
    17. return editor;
    18. }
    19.  
    20. signals:
    21. void editingFinished();
    22. };
    23.  
    24. // usage:
    25. treeWidget->setItemDelegate(new ItemDelegate(treeWidget));
    26. connect(treeWidget->itemDelegate(), SIGNAL(editingFinished()), ...);
    To copy to clipboard, switch view to plain text mode 
    You could also use QSignalMapper if you want to pass the editor widget as signal parameter.

    So, if there is another item with the same text, than I show a QMessageBox warning the user the item must have a different text, so I open the persistent editor again for the user to change the text.
    Idea: make corresponding editor backgrounds red or something instead of popping up annoying message boxes
    J-P Nurmi

  3. #3
    Join Date
    Jun 2007
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help using the QTreeWidget persistent editor.

    Well, thanx man, now I advanced a lot with my problem.

    But now I have another.

    I do this connect you said, but my dialog doesn´t catch that signal.

    I did like this in my dialog:

    treeWidget->setItemDelegate(new ItemDelegate(treewidget));
    connect(treeWidget->itemDelegate(), SIGNAL(finishedEditing()), this, SLOT(closedEditor()));

    I checked to see if the delegate is really emiting the signal and it is emiting alright. But this connect simply doesn´t catch the signal.

    And in the output window, there is no message form QT warning that the signal or the slot do not exist.

    Do you have any idea why is this happening?

  4. #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: Help using the QTreeWidget persistent editor.

    Just to assure, did you rename the signal to "finishedEditing" or could it be a typo? Can you see a warning by QObject::connect() if you make it fail on purpose?
    J-P Nurmi

  5. #5
    Join Date
    Jun 2007
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help using the QTreeWidget persistent editor.

    Yes, I just renamed the signal.

    And no, that´s the strange part. I don´t get any warning about the signal or the slot, not even when my application is running.

  6. #6
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help using the QTreeWidget persistent editor.

    Billy Lee, I implemented the custom delegate just like jpn suggested and I'm able to successfully connect the editingFinished() signal to a suitable slot in my application. I'm not sure what is happening with your implementation. Are you sure that the MOC is being run on your custom delegate class? Did you define it in it's own header? Have you checked the response from connect to make sure it's true?

  7. #7
    Join Date
    Jun 2007
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help using the QTreeWidget persistent editor.

    Sorry for replying so later.

    Well, I couldn´t make it work here, so I ended up doing things in another way. I check the items only when I click the window´s apply button.


    But thanks for your help, people.
    Perhaps later I will try making a new code just to test this delegate better. Than I will post the results here.


    Thanks again.

Similar Threads

  1. Model/View: Custom Persistent Editor
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 7th February 2007, 14:55
  2. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32

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.