PDA

View Full Version : Setting a widget for an item in the MV framework



quantumpilgim
27th March 2017, 20:01
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.

Santosh Reddy
28th March 2017, 04:55
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.

quantumpilgim
28th March 2017, 07:48
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).

Santosh Reddy
28th March 2017, 08:35
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.

quantumpilgim
28th March 2017, 19:51
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!