Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create a listbox with custom listbox items?
Forum Updated to NodeBB v4.3 + New Features

How to create a listbox with custom listbox items?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 6.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    avidamic
    wrote on last edited by
    #1

    I need to create a (vertical) listbox of custom listbox items (a frame around 2 push buttons and a label). I'm having trouble doing this with QListWidget as it needs a QListWidget item, which itself can't draw my special grouping of UI widgets (2 buttons and a label). I ended up creating a sub-class of QListWidgetItem and have it also inherit from QFrame (because my UI is a frame with 2 buttons and a label inside it) so I can initialize my UI (via setupUI). However, QListWidget only offers ViewModes ListMode and Icon Mode, which I don't believe will work for my scenario.

    Can anyone suggest a better way to setup a listbox with custom listbox items like I want? I tried to search through forums for something similar but didn't find it so apologies if there is an existing thread out there.

    Thanks.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      Would QML and a ListView be an acceptable solution?

      Another alternative is a custom delegate with QListView.

      Or how about a simple QVBoxLayout inside a QScrollArea?

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • A Offline
        A Offline
        avidamic
        wrote on last edited by
        #3

        Thanks for the suggestions, ZapB. I'm not familiar with QML so not sure if that is a good choice.

        I'm reading up on the custom delegate with QListView method now. The alternative of a QVBoxLayout in a scroll area is more appealing because there's not much of a learning curve there but then I would have to implement my own listbox class.

        Just to reiterate, I need to create a list box with custom list box items. The listbox items will consist of a frame around 2 push buttons and a label so I will need to override the input. There can be up to 256 of these items in a list box and users will be able to add, remove and select them freely. I'll probably need to incorporate a context menu for the "selected" item. Also, I will need to be able to support drag into these list box items.

        So with the above info, do you know if there is a better choice between the last 2 options? Any unnecessary overhead with one vs the other, etc.?

        If I go with the delegate solution, would QItemDelegate sub-class for the list box items be the way to go? What kind model do I setup - standard? Sorry, I'm not terribly familiar with how model/view system works (which is why the last suggestion was appealing).

        Thanks again for your help.

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZapB
          wrote on last edited by
          #4

          For QML it depends on how much you want your items to match the system theme. QML is probably the easiest option but there are not yet pre-defined desktop components available. However, it is very easy to do and to add animations etc. It also comes with support for current item highlighting etc.

          If QML is a no-go for you then I would go for the custom delegate approach I think since 256 is quite a lot of widgets to display at once. Using a delegate, the QListView will handle creation and deletion of the delegates for you.

          To see how ot use your own delegate take a look at this "example":http://doc.qt.nokia.com/latest/itemviews-stardelegate.html. How you map your model onto the delegate is up to you but normally you would provide a different Role for each piece of data to be shown within a single delegate.

          HTH

          Nokia Certified Qt Specialist
          Interested in hearing about Qt related work

          1 Reply Last reply
          0
          • A Offline
            A Offline
            avidamic
            wrote on last edited by
            #5

            Thanks again, ZapB!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved