How to set add some custom controls in the QListWidgetItem?



  • 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?


  • Lifetime Qt Champion

    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



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


  • Lifetime Qt Champion

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



  • ok, thanks very much



  • 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?



  • 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?



  • 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
    }
    }


  • Lifetime Qt Champion

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

    What do you want to achieve ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.