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. [solved]: Simple View made out of QButtons

[solved]: Simple View made out of QButtons

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.2k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi there!
    I have a simple model comparable to a QStringList Model: It's 1-dimentional and does not have a tree stucture. The model is a QStandardItemModel filled with a custom subclass of QStandardItem where the data with DiaplayRole is simply a QString. No other roles are defined.
    Now, I want to display a vertical list of QButtons or QCommandLinkButtons in a container. The list has to correspond exactly to the model: For example, if the model contains three items with data->DisplayRole^="one","two" and "three", there need to be exactly three buttons with the texts "one", "two" and "three" appearing in an area of the screen.
    In order to keep things as simple as possible, I'd like to create a new view class to realize this list of buttons. The view Widget would have the following properties:
    -> There is no header.
    -> For each item in the model, there is exactly one button.
    -> Like any view, this view adjusts itself automatically at any change of the model (update, insert, delete).
    -> If necessary, scroll bars are displayed allow to scroll through the view.
    -> The view provides a clicked(QWidget) signal (or similar) which contains a QWidget reference to the one QStandardItem in the model whose corresponding button was clicked.
    -> The view cannot edit the model.
    -> The active item is the button that was last clicked.
    -> The view does not handle selections.
    -> The view does not support drag&drop.
    As you can see, this is an extremely simple type of view. Unfortunately, I have never gone past the 3 standard Qt view types, so I have no idea how to realize this.
    What would be the very easiest, most straightforward and most convenient way to implement such a view? Note that efficiency is not a problem, since the view will not contain more than like 20 items.
    Thx in advance!
    Kalsan

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You can make it in several ways, e.g. creating your own view based on QAbstractItemView. You can find an example of that in the Chart Example. Or go the widget way using QWidget + QScrollArea + QVBoxLayout and QPushButton. With this one, you'll have to connected to the model yourself but it's not extremely hard.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Thank you SGaist for your reply.
        I've already tried the first option and I broke my neck. Therefore I'd much rather go the second way.
        I have a few questions regarding the details, as I'm still very new to Qt and not yet familiar with its conventions:

        1. Which signals of the model will I have to listen to?
        2. Which functions of the model provide the state-of-the-art way to get information from it?
        3. Should I implement a general update function that regenerates the entire "view" widget at any change, or is it easier to have three distinct slots for insert / remove / update? If the second option is to be preferred, what is the right way to delete a button both from its containing QList and from the screen?
        4. Is a signal mapper the right way to emit a signal when a button is clicked?
          Cheers,
          Kalsan
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4
          • dataChanged
          • modelReset
          • rowsInserted
          • rowsMoved
          • rowsRemoved

          should be enough

          1. data
          2. if you connect and handle correctly the signals from 1, it will be automatically done when removing an entry from the model.
          3. That depends on what you want to happen when a button is clicked

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Thank you so much! I think with the information above I will be able to implement everything.
            Cheers,
            Kalsan

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You're welcome !

              If this answers your question, then please update the thread title prepending [solved] so other forum users may know a solution has been found :)

              Happy Coding !

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                If anyone wants to know the exact implementation: I finally didn't make a custom Widget, but I created a new class that takes a parent QWidget (for automatic Qt deletion) and a target QLayout. The call of setModel associates the model and adds the initial buttons to the QLayout. Then, the five signals above are each connected to a slot which alters the buttons in the QLayout.

                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