Using QInputDialog with a checkbox



  • Are you able to use a checkbox inside an QInputDialog window? I want to prompt the user to be able to choose from about 25 different options that will be used as mutators elsewhere in the program.

    This is what I have right now. It doesn't compile at all, but basically I'm trying to use the combobox inside the QInputDIalog box.

    QCheckBox sounds;
    QInputDialog showSounds;
    QStringList soundList;
    soundList << sounds.setText("Sound 1");
    
    showSounds.setOptions(QInputDialog::UseListViewForComboBoxItems);
    showSounds.setComboBoxItems(soundList);
    showSounds.setWindowTitle("Select sounds");
    showSounds.exec();
    

    Thanks for the answer ahead of time.



  • Hi friend. welcome!
    void setText(const QString &text) return void. and you used soundList << sounds.setText("Sound 1"); I didn't understand it. It is wrong.

    And I saw the qt help about the QInputDialog

    The QInputDialog class provides a simple convenience dialog to get a single value from the user.
    The input value can be a string, a number or an item from a list ...

    About the an item from list

    void setComboBoxItems(const QStringList &items); ///< It is the ComboBox item. Not the CheckBox.

    so, if you want to have the checkbox.

    1. First solution: You can subclass QDialog. i think it is easy and good solution.
    2. Second solution: I saw the QComboBox, and you can try using void setItemDelegate(QAbstractItemDelegate *delegate)
      to delegate the checkbox. It is complex i think.

    maybe someone has the better answer. best wish for you.


  • Moderators

    @Onikouzou What is this checkbox used for?
    If you only want to use a combobox then do:

    QInputDialog showSounds;
    QStringList soundList;
    soundList << "Sound 1" << "Sound 2";
    showSounds.setOptions(QInputDialog::UseListViewForComboBoxItems); // Remove this if you want a combobox instead of a list
    showSounds.setComboBoxItems(soundList);
    showSounds.setWindowTitle("Select sounds");
    showSounds.exec();
    

Log in to reply
 

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