Results 1 to 10 of 10

Thread: How to add QPushButton to QTreeWidget

  1. #1
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default How to add QPushButton to QTreeWidget

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to add QPushButton to QTreeWidget

    You can use QAbstractItemView::setIndexWIdget() but before you do, you should read all the discussions about drawbacks of using widgets in ItemViews.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to add QPushButton to QTreeWidget

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to add QPushButton to QTreeWidget

    Quote Originally Posted by shamas View Post
    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++?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to add QPushButton to QTreeWidget

    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?
    Last edited by shamas; 15th March 2012 at 05:59.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to add QPushButton to QTreeWidget

    Quote Originally Posted by shamas View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to add QPushButton to QTreeWidget

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to add QPushButton to QTreeWidget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    shamas (23rd March 2012)

  10. #9
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to add QPushButton to QTreeWidget

    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.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to add QPushButton to QTreeWidget

    Quote Originally Posted by shamas View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 22nd February 2010, 10:30
  2. QPushButton
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 21st October 2009, 17:31
  3. Replies: 2
    Last Post: 17th March 2008, 13:53
  4. Replies: 1
    Last Post: 19th October 2007, 02:29
  5. Replies: 3
    Last Post: 26th September 2006, 13: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.