PDA

View Full Version : How to add QPushButton to QTreeWidget



shamas
12th March 2012, 04:40
Hey Guys,

I am trying to make a jsonEditor in QtScript. I am able to show it on UI but this is where I am stuck. I want to add a button, to the right side of certain QTreeWidgetItem which will be used to delete that row.

Thanks.

wysota
12th March 2012, 10:18
You can use QAbstractItemView::setIndexWIdget() but before you do, you should read all the discussions about drawbacks of using widgets in ItemViews.

shamas
12th March 2012, 11:09
Hi,

So they tell me that although QTreeWidget is very easy to learn/use, it doesn't provide adding your own widget. So there I would have to use QTreeView for this probably. From reading through different forums and posts, it appears that it can be done by two ways.

1. Implementing delegates for paint. (For this I probably will have to use setModel(). Done that. But don't know how to hook it up with paint.
2. setIndexWidget. (For this I would probably have to reject the idea of a Model and add QAbstractView to my QListView)

To make matters worst, I have to do this all in QtScript, for which there are pretty much no sample codes available, which technically shouldn't be different, but is a bit of a pain in the neck.

Could you please tell me which path should I take and where should I go from there?

wysota
12th March 2012, 14:48
So there I would have to use QTreeView for this probably.
No, QTreeView suffers from the same "problems".


1. Implementing delegates for paint. (For this I probably will have to use setModel(). Done that. But don't know how to hook it up with paint.
Yes, that's correct. You need to implement a custom delegate that will use QStyle API to draw the button and will handle events on it through editorEvent()


To make matters worst, I have to do this all in QtScript
Why so? Can't you expose some easy interface for it from within C++?

shamas
15th March 2012, 03:20
So let me get this straight. I should make an object of QAbstractItemView. then make an object of QStandardItemModel and populate it. then set it as setModel for my abstractItemView.

I have treid that, but for some reason my view doesn't show up.
var myView = new QAbstractItemView;
myView.setMode(myPopulatedModel);
myWidget.layout().addChild(myViw);
//nothing shows up.

Also, I am using QStandardItemModel which shows up just fine in a QTreeView. I am also facing problem in QStandardItemModel.
var myStrdModel = new QStandardItemModel();
var myItem1 = new QStandardItem("hello");
var myItem2 = new QStandardItem("hello1");
myStrdMode.invisibleRoot().appendRow([myItem1,myItem2])

Till now looks ok. but I want to add a row to this row as a child. How can I do that?

Edit: Also, when using these two, is it compulsory to use QAbstractItemDelegate?

wysota
15th March 2012, 08:49
I should make an object of QAbstractItemView
No. This is an abstract class. You need a tree view or a tree widget.

Till now looks ok. but I want to add a row to this row as a child. How can I do that?
Use QStandardItem API. It has an appendRow() method.


Edit: Also, when using these two, is it compulsory to use QAbstractItemDelegate?
There is always a delegate involved. It's just a matter of using the default one or a custom one. If you want to emulate push buttons then you need a custom delegate.

shamas
15th March 2012, 09:13
Hi wysota-san,

Although very happy to hear back from you, I am now more confused than ever.

Here is what I now think so far.

I can make object of QTreeWidget and use QStandardItem. On QTreeWidgetItem object, I can call setIndexWidget to set a QPushButton where I wish. But that doesn't make sense since you initially discouraged saying that I should use QAbstractItemView::setIndexWIdget(), but I can have QAbstractItemView display since it's an Abstract Class. :(

Please. SOS.

wysota
15th March 2012, 11:17
setIndexWidget() is a method defined in QAbstractItemView but it is inherited by its subclasses (such as QTreeView or QTreeWidget) acording to rules of object oriented programming. And you shouldn't use setIndexWidget.

shamas
23rd March 2012, 02:07
Hi,
So as it turns out that to choose which of my columns in which rows needed to be click-able/editable, I could do that by setData(columnNumber, Qt.UserRole, "isDisabled") and using QtItemDelegate for QtreeWidget's setItemDelegate. Each time a certain box is double clicked, I can see if its data has userRole defined is "isDisabled" in which case it would return null.

As far as the button was concerned, is set them as setData(columnNumber, Qt.UserRole, "shouldBehaveAsButton"), and when those are clicked, in QtItemDelegate, I would return null, and send command for button clicked. (Probably not a very user friendly idea, but will set colors or backgrounds in a way that it would look like a button and include this in my video demo)

This solves my problem. Thanks you. :)

wysota
23rd March 2012, 10:55
So as it turns out that to choose which of my columns in which rows needed to be click-able/editable, I could do that by setData(columnNumber, Qt.UserRole, "isDisabled") and using QtItemDelegate for QtreeWidget's setItemDelegate. Each time a certain box is double clicked, I can see if its data has userRole defined is "isDisabled" in which case it would return null.
There is an easier way. Each item in the model has a set of flags returned by QAbstractItemModel::flags(). Those flags include Qt::ItemIsEditable and Qt::ItemIsEnabled.