Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QRadioButton elements inside a QlistWidget



  • Hello everyone,

    I'm a new QT developer.
    What I'd like to have as end result is a QListWidget where the list's items are QRadioButtons.
    Is that possible? And if it is, how could I implement that? Are there any examples to look from?

    Thank you


  • Moderators

    Hi @CodeVisio,
    Yes it is possible

    • Create new QListWidgetItem
    • Create new QRadioButton
    • Use setItemWidget to add that RadioButton as a List item in QListWidget
      For eg:
    QListWidget *list = new QListWidget();
    QListWidgetItem *listitem = new QListWidgetItem();
    list->setItemWidget(listitem,new QRadioButton("Radio Button"));
    


  • Hi p3c0,

    thank you for the answer.

    How about the click notifications? How can I be notified about the user click?

    Thanks


  • Moderators

    @CodeVisio
    Use itemClicked(QListWidgetItem*) signal
    and then for that item use itemWidget() method to access widget inside it.



  • Thanks a lot @p3c0.

    One more thing.
    I can use QListWidget with check boxes (shown like QCheckBox) as elements inside it without
    this approach list->setItemWidget( listitem, new QCheckBox() ) , while for QRadioBoxes I have to explicitly create those object before inserting them inside the list.
    Is that a design choice? or is there a way to show QRadioBoxes inside the list as I can do with QCheckBoxes without creating them?

    Thanks again


  • Moderators

    @CodeVisio
    For checkboxes I think you must have set Qt::ItemIsUserCheckable flag for QListWidgetItem. So there's a way to do so. But for radiobutton theres none and thus have to create the widget explicitly.

    Edit: Links


Log in to reply