Results 1 to 9 of 9

Thread: Custom selector widget

  1. #1

    Default Custom selector widget

    Hello,

    I'm pretty new to Qt, and I'm wondering if anyone can give me ideas or tips on how to implement the following:

    I want to make some kind of selector widget that I can eventually add to a QTreeView (I'll worry about that later). The widget will basically be a horizontal array of maybe 16 or 32 small toggle-able buttons or boxes or something (maybe QPushButtons with setCheckable(true)). I want to be able to select or deselect an arbitrary amount of them, then be able to drag the selected ones and drop them into another QTreeView in the same window (at which point that sub-window will deal with the dropped items, which I'll also worry about later).

    I guess I'm wondering if the PushButton way above is a good way to do it, or should I just make the thing from scratch (which would mean dealing with what each little box looks like selected and unselected, figuring out how to make them drag-and-droppable, etc.). I figured an array of PushButtons seemed to make sense because it already has most of that functionality and I shouldn't reinvent the wheel if I don't have to. I also don't want to run into problems when I have to make delegates for the thing to be displayed in the TreeView. Any advice would be appreciated.

  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: Custom selector widget

    Could you provide a graphical mockup of what you want?
    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

    Default Re: Custom selector widget

    Sure thing, I attached a partial screen of what I have so far. The blue array of rectangles would be the things that are individually selectable. Ideally, I would like to be able to select things from multiple levels (e.g., boxes 1, 2, 3 in level 4-1 and 4-2 and 4-3) and be able to drag just the selected ones, like when you select multiple items in the QTreeView and drag them.

    The model is already set up for the tree, so I'm just providing a different visualization at the lowest level, rather than just have the 32 items in that level lined up vertically. When I click on a rectangle, I want that small rectangle to be selected.

    I used the Qt Star Delegate example as a reference. Basically I just have a rectangle and paint it 32 times. I guess my problem at this point is how to handle dealing with selecting individual boxes, and how to make them draggable. Also based on reading other threads in the forum, I'll probably run into problems later because I don't want to have to double click to trigger the edit event, then click once more to toggle the switch.

    edit: I should probably say everything in the list is selectable and draggable already, and doing that essentially means "select this item, and everything below it in the tree". Every item's flag is Qt::ItemIsDragEnabled.
    Attached Images Attached Images
    Last edited by Strongoloid; 17th July 2009 at 20:22.

  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: Custom selector widget

    Selecting is easy. Override QAbstractItemDelegate::editorEvent(). Dragging will be much harder - you need to come up with an idea how to perform the drag itself.

    There is another thing you could try - subclass both the view and the delegate. Make each box a separate item in the model. In the view modify proper methods so that they return proper rectangles for your items. The delegate for the lowest level would just paint a single box. Then the drag should be quite easy as well, you'd just drag every selected item. The hardest part would be to reimplement parts of the view.
    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

    Default Re: Custom selector widget

    There is another thing you could try - subclass both the view and the delegate. Make each box a separate item in the model. In the view modify proper methods so that they return proper rectangles for your items. The delegate for the lowest level would just paint a single box. Then the drag should be quite easy as well, you'd just drag every selected item. The hardest part would be to reimplement parts of the view.
    I'm a little confused on what you mean. Could you elaborate? Do you mean to have, for instance, each of the 32 items as a separate item, with a delegate for the item? What would I have to reimplement in the QTreeView?

  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: Custom selector widget

    Quote Originally Posted by Strongoloid View Post
    Do you mean to have, for instance, each of the 32 items as a separate item, with a delegate for the item?
    Yes.
    What would I have to reimplement in the QTreeView?
    Methods responsible for layout of items and their size (indexAt, visualRect, etc.). This is a bit of work and you have to think how you are going to implement it before you start.
    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

    Default Re: Custom selector widget

    Sorry for the long gap, I've been away but now I'm back to working on this stuff.

    Having each box be a separate item in the model is ok, and I understand that I will need to reimplement some methods like visualRect and indexAt to be able to select the correct boxes. However, I'm unsure how to get the boxes to show up in the same row in the first place.

    If I subclass the view, I would basically maintain the same behavior as a normal QTreeView, except when it gets down to that lowest level. I guess I'm just confused as to how to get 32 rows to change to a row of 32 items. I'm not sure how to change the layout.

    I'm guessing I would have to reimplement drawRow and paintEvent as one of the first things to do. Am I on the right track?

  8. #8
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom selector widget

    Excuse my denseness, but what is wrong with the default drag n drop behaviour?

  9. #9

    Default Re: Custom selector widget

    Excuse my denseness, but what is wrong with the default drag n drop behaviour?
    Nothing. I want to maintain the default drag and drop behavior at all levels. My hang up is with how to subclass QTreeView to show (at the lowest level, or the leaves or whatever they'd be called) a row of individually selectable boxes. Each of those items on the row is basically a QStandardItem, just with a different visual representation (rather than have 32 rows under the parent node, I want 32 little boxes in one row under the parent node).

Similar Threads

  1. Replies: 12
    Last Post: 5th July 2009, 17:03
  2. Replies: 2
    Last Post: 16th May 2008, 15:39
  3. Custom widget
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 28th January 2008, 15:06
  4. Custom proxy model issue
    By Khal Drogo in forum Qt Programming
    Replies: 13
    Last Post: 30th November 2007, 13:41
  5. custom plug-in widget in another custom plug-in widget.
    By MrGarbage in forum Qt Programming
    Replies: 6
    Last Post: 27th August 2007, 16:38

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.