Results 1 to 8 of 8

Thread: popup editors for QTreeView and QAbstractItemModel

  1. #1
    Join Date
    Mar 2011
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6

    Default popup editors for QTreeView and QAbstractItemModel

    Hi,

    I am doing fine with QTreeView, abstract models, and delegates.
    What I need now is the following: In the tree there is a string and if I would like to edit it there should come something like the ... button. When I click on it the file dialog pops up. Similar things for self written dialogs as table editors ...
    I need some hints (signals to check, how to transfer data and so on). A link to a working example would also be enough. But this should be an example where Qt MVC concepts are used.

    Thanks a lot!
    Thomas

  2. #2
    Join Date
    May 2012
    Posts
    136
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 27 Times in 24 Posts

    Default Re: popup editors for QTreeView and QAbstractItemModel

    You can try doing this (didn't test it)
    add this to the delegate class you already have
    it does not change the apearance of the string but it will open a filedialog on edit
    Qt Code:
    1. void ItemDelegate::setModelData(QWidget *editor,
    2. QAbstractItemModel *model, const QModelIndex &index) const
    3. {
    4. //this is for all items in the item model, if you want it only for one column check if index.column() == the column you want the filedialog for
    5.  
    6. mode.setData(index,QFileDialog::getOpenFileName(),Qt::EditRole) //you need to include <QFileDialog>
    7. }
    To copy to clipboard, switch view to plain text mode 

    if you want to change the apreance too then you need to add the special editor in your item delegate createEditor function
    Qt Code:
    1. QWidget *ItemDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. return new SpecialEditor(parent); //the special editor needs to be a QWidget or derivative from it
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6

    Default Re: popup editors for QTreeView and QAbstractItemModel

    I have the feeling that this solution is at least not complete.
    Code 1: ?? setModelData: the QWidget is not used at all. Is this the right position to open up a popup??
    Code 2: ?? createEditor: this creates a widget within the tree view. I would like to have it popup.

  4. #4
    Join Date
    May 2012
    Posts
    136
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 27 Times in 24 Posts

    Default Re: popup editors for QTreeView and QAbstractItemModel

    Code 1: yes this is the right position to create a filedialog (if you dont want to change the string appearance in the tree view), the dialog will be created if you start editing a string in the treeview.
    Code 2: if you use this method you will have to create a widget that takes care of creating a popup
    setModelData will have to call a function in the widget to open the pop up

  5. #5
    Join Date
    Mar 2011
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6

    Default Re: popup editors for QTreeView and QAbstractItemModel

    Code 1: Isn't setModelData called after editing was done? So I must set isEditable and then only after editing (using editor within the tree) the popup comes up?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: popup editors for QTreeView and QAbstractItemModel

    Quote Originally Posted by ctgrund View Post
    am doing fine with QTreeView, abstract models, and delegates.
    What I need now is the following: In the tree there is a string and if I would like to edit it there should come something like the ... button.
    Then your delegate will create a button as the editor widget.

    Quote Originally Posted by ctgrund View Post
    When I click on it the file dialog pops up.
    Then you need to connect your button's clicked() signal to a slot that runs the file dialog.

    You could have your own button class that does that internally and has a member for the file name, which is set in setEditorData and read in setModelData.
    Or using a dynamic property on a normal QPushButton or storing the name elsewhere and accessing it from the slot that shows the dialog.

    I would go for a base class that encapsulates the dialog step

    Qt Code:
    1. class EditorButton : public QPushButton
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit EditorButton( QWidget *parent ) : QPushButton( parent )
    7. {
    8. connect( this, SIGNAL(clicked()), this, SLOT(onClicked()) );
    9. }
    10.  
    11. void setEditorData( const QVariant &data ); // store data delegate gets in setEditorData
    12. QVariant modelData() const; // return data that setModelData should store.
    13.  
    14. protected slots:
    15. virtual void onClicked() = 0; // implemented by sub classes
    16. }
    To copy to clipboard, switch view to plain text mode 

    EditorButton::setEditorData() could also set the button's text, but you can also do that in the delegate.
    It could also be subclass specific, i.e. using QString as the type for file names. depends on where you want to know about which class

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    ctgrund (22nd January 2014)

  8. #7
    Join Date
    May 2012
    Posts
    136
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 27 Times in 24 Posts

    Default Re: popup editors for QTreeView and QAbstractItemModel

    Yes sorry you are right i copied the wrong part
    should have copied

    bool QAbstractItemDelegate::editorEvent ( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index ) [virtual]
    When editing of an item starts, this function is called with the event that triggered the editing, the model, the index of the item, and the option used for rendering the item.

    Mouse events are sent to editorEvent() even if they don't start editing of the item. This can, for instance, be useful if you wish to open a context menu when the right mouse button is pressed on an item.

    The base implementation returns false (indicating that it has not handled the event).

    for more information read the documentation on QAbstractItemDelegate

    did a quick test and this is working
    Qt Code:
    1. bool ItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
    2. {
    3. model->setData(index,QFileDialog::getOpenFileName(),Qt::EditRole); //you need to include <QFileDialog>
    4. return false;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by StrikeByte; 22nd January 2014 at 13:03.

  9. The following user says thank you to StrikeByte for this useful post:

    ctgrund (22nd January 2014)

  10. #8
    Join Date
    Mar 2011
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6

    Default Re: popup editors for QTreeView and QAbstractItemModel

    Button will be the editor widget! Everything else I think I can do.
    Thanks!

Similar Threads

  1. QTreeView with QAbstractItemModel slow
    By Qiieha in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2012, 09:06
  2. QTreeView with QAbstractItemModel
    By Fletcher in forum Qt Programming
    Replies: 4
    Last Post: 10th December 2009, 23:02
  3. Creating a QAbstractItemModel for QTreeView
    By hbill in forum Qt Programming
    Replies: 13
    Last Post: 14th August 2008, 16:01
  4. Need help with QTreeView/QAbstractItemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 28th August 2006, 15:09
  5. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 20:16

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
  •  
Qt is a trademark of The Qt Company.