Results 1 to 5 of 5

Thread: Setting a widget for an item in the MV framework

  1. #1
    Join Date
    Sep 2016
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Setting a widget for an item in the MV framework

    Hi all,

    I am struggling somehow with the MV framework in the past month or so. As the topic suggest what I want to do is place widgets (actuals QWidget s not QTreewidgetitems and so on) on a tree-style view. I followed the SpinBoxDelegate example and managed to get any custom widget i create as and editor. But that works only if i double click on an entry. Maybe I could get the thing to "look like" my custom widget if i use a paint event but even that is not good because i want that at each place of the treeview thing the actual widget will be placed, so for example it can receive keyboard shortcuts, mouse events, right clicks and so on. Is it possible and how? I dont need a complete answer but maybe some hints.

    I ll give a short explanation of what i am trying to do. I want each entry on the tree to represent an "entity" of some programming language, e.g. packages and classes in java; libraries, headers, source files in C/C++ and so on. Usually those things are structured in some tree-like structure. I want to use QT's MV framework to work with and represent them. So at each case say i have a default widget which receives right clicks (with say Qt::CustomContextMenu) and shows a list of pissibilities for each entry (say compile everything in a package of java, or put together the package in some java, put together a shared library of C and so on). Of course I can easily create such a widget the problem i have is

    "How do I put it in a tree view form USING the MV framework?"
    (cleary i can do it by hand without the MV framework but its rather annoying haveing to rewrite all the functionality of the MV that I need like collapsing, showing and so on)

    I have got as far as installing such a widget as an editor, so if i double click at an entry i can then use it my custom widget. Apart from the annoyance of having to double click, it is also wrong in a principle, I DO NOT WANT to have some special editor for each entry i want to have the widgets placed in a tree view.

    I am sorry for the long exmplanation. I thank you for your answer in advance.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Setting a widget for an item in the MV framework

    Looks like you have couple of problems, if I understood properly

    1. Double click to enable edit widget: You can use setEditTriggers() to set to other triggers.

    2. Right click actions menu: TreeView can give you the item / cell clicked on then you have take care of setting up the menu and handling it, TreeView does not support it.

    I DO NOT WANT to have some special editor for each entry i want to have the widgets placed in a tree view.
    Set the parent of the new editor widget properly, it will be placed in the cell of TreeView.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    quantumpilgim (28th March 2017)

  4. #3
    Join Date
    Sep 2016
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Setting a widget for an item in the MV framework

    Thanks!
    I might give a try to what you are suggesting although I found out what I was looking for.

    QAbstractItemView::setIndexWidget()

    although i am a bit scared about performance issues but for now it seems to be the way to go for now.

    Set the parent of the new editor widget properly, it will be placed in the cell of TreeView.
    I am following the example of SpinBoxDelegate. All I changed was the widget to be used from SpinBox to my widget. Here I try to make the widgets be QPushButton s:

    QWidget *MyDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
    {
    QPushButton * label = new QPushButton(parent);
    label->setText("Button To Click");
    connect(label, SIGNAL(clicked()), this, SLOT(printScream()));
    return label;
    }

    void MyDelegate::setEditorData(QWidget *editor,
    const QModelIndex &index) const
    {
    int value = index.model()->data(index, Qt::EditRole).toInt();

    QPushButton *spinBox = static_cast<QPushButton*>(editor);
    spinBox->setText(QString("%1").arg(value));
    }

    (the printScream is a simple slot just printing sth on stdout)
    it works as I said before. Say I use a QTableView, with some values normally everything appears as a simple text thing. when i double click at an entry then i get this QPushButton and i click it and the slot works but NOT before I double click at an entry.
    So where am I setting the parent wrong?

    But after looking some stuff aroung i think what i am looking for is this indexWidget(). most likely it is why the treewidget accepts entries which are an icon and a text, they use some internally defined widget which gets this properties from treewidgetitem ( i didnt have time to test this theory).

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Setting a widget for an item in the MV framework

    Setting a widget on the cell and setting a editor widget are different things.

    when i double click at an entry then i get this QPushButton and i click it and the slot works but NOT before I double click at an entry.
    Do you want the push button to appear after single click ? if yes take a look at setEditTriggers() on view.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #5
    Join Date
    Sep 2016
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Setting a widget for an item in the MV framework

    Setting a widget on the cell and setting a editor widget are different things.
    you are correct. I want to have a custom widget at each entry and not just a custom editor for each entry.

    so i think the only way is to use indexwidget. I may also want to further have a custom editor but thats for later.

    About the setEditTriggers, i ve seen them but didnt try them . I tried to use the editorEvent but couldnt get it to work.

    I think the indexwidget is what i was looking for all along but somehow the MV reference or examples dont advertise it very much for some reason, even though it is one of the most powerful customizing techniques for the view model.
    Anyway thank you for you answers. Appreciate it!

Similar Threads

  1. Setting default item in a QCombobox
    By graffy in forum Qt Programming
    Replies: 4
    Last Post: 14th August 2013, 12:13
  2. Setting text item in QTreeView
    By adrianoleal in forum Newbie
    Replies: 7
    Last Post: 12th July 2011, 13:48
  3. Replies: 7
    Last Post: 3rd December 2010, 05:02
  4. Setting an icon to an Item in a QlistWidget
    By LordQt in forum Qt Programming
    Replies: 18
    Last Post: 23rd August 2007, 22:24
  5. Replies: 14
    Last Post: 9th November 2006, 08:35

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.