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 set add some custom controls in the QListWidgetItem?
Qt 6.11 is out! See what's new in the release blog

How to set add some custom controls in the QListWidgetItem?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 4.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.
  • O Offline
    O Offline
    opengpu
    wrote on last edited by
    #1

    i want to add various kinds of controls in each QListWidgetItem,
    but i thought that it's not possible to get this effect through inherite QListWidgetItem...
    how should i do this?

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

      Hi,

      QListBoxItem ? Are you aware that it's a Qt 3 class ? The first thing you should do is port the code to at least Qt 4 if not 5

      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
      • O Offline
        O Offline
        opengpu
        wrote on last edited by
        #3

        type error...
        it should be QListWidgetItem...

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

          Ok, then you will have to use QListWidget::setItemWidget

          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
          • O Offline
            O Offline
            opengpu
            wrote on last edited by
            #5

            ok, thanks very much

            1 Reply Last reply
            0
            • O Offline
              O Offline
              opengpu
              wrote on last edited by
              #6

              This function should only be used to display static content in the place of a list widget item. If you want to display custom dynamic content or implement a custom editor widget, use QListView and subclass QItemDelegate instead.
              ///
              is combox, lineedit dynamic?

              1 Reply Last reply
              0
              • O Offline
                O Offline
                opengpu
                wrote on last edited by
                #7

                i just tried, and it displayed right, but there is another small problem.
                cause the widget i want to set to the item is much more bigger than the item is, so i have to set the item's size, but i only found an API setSizeHint in the QListWidgetItem, and it seems only can be set fixed size...is there other API to more flexiblly set the item's size, such as make it auto-adjusted to the widget's size?

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ankitlive
                  wrote on last edited by
                  #8

                  This is not that simple, however it is doable. The problem is that when you click on the button the press event is not propagated to the list widget. So you have to find a way to propagate the signal back to the list widget.

                  When the button is pressed the pressed signal is emitted. When it is released the released signal is emitted. I will show you how to do it for one of them, you should do the same for the other.

                  The most straight forward way to do what you want is to add a connection between the pressed signal of the button and the slot which will modify the background of your list. What you have is a QListWidget in which there are some UserDetails widgets each of which has a QPushButton. So we have to go all the way up from the QPushButton to the QListWidget.

                  In your UserDetails class create a new signal, eg buttonPressed() and connect your button's pressed() signal with it. This way every time the button is clicked in your widget the widget itself will notify the world that its button has been clicked.

                  connect(ui->button, SIGNAL(pressed()), this, SIGNAL(buttonPressed())

                  Now we want to notify the QListWidget that the button has been pressed. In order to achieve this we have to connect the buttonPressed signal from the UserDetails with a slot, let's call the slot buttonPressedSlot()

                  connect(myItem, SIGNAL(pressed()), this, SLOT(buttonPressedSlot())

                  Now the slot should detect which is the sender (since all UserDetails objects will get connected to the same slot, find the corresponding QListWidgetItem and call the slot that will update the background of this item.

                  void LiwstWidgetClick::buttonPressedSlot()
                  {
                  QObject* signalSender = QObject::sender();

                  UserDetails* p = qobject_cast<UserDetails *>(signalSender);

                    if (p == NULL)
                       return;
                  

                  // Get Position in list widget
                  for (unsigned i=0; i<ui->listWidget->count(); i++)
                  {
                  QListWidgetItem* item = ui->listWidget->item(i);
                  if (ui->listWidget->itemWidget(item) == p)
                  cellClicked(item); // THIS IS YOUR FUNCTION THAT CHANGES THE
                  // BACKGROUND OF THE GIVEN ITEM
                  }
                  }

                  [url=http://www.mygatenow.com/2014/10/shoe-in-money-system-review.html]shoe in money system review[/url]

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

                    Then are you sure that a QListWidget is the right tool for what you want to do ?

                    What do you want to achieve ?

                    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

                    • Login

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