QComboBox in QListWidget



  • I am able to get a ComboBox in my List Widget, but how can I get it to update my data? Whenever I change one of the comboboxes, i would like the appropriate VCsetting[].value to be updated.

    I am pretty new to QT. I have been looking at this for days. Read about delegates and everything, but I just can't get any of the examples to work. I simply do not understand them. I don't know what I am doing.

    Here is my VCsettings.h:

    #ifndef VCSETTINGS_H
    #define VCSETTINGS_H
    
    // Class for operate the settings of the VController
    #include <QDialog>
    #include <QTreeWidget>
    #include <QString>
    
    #define NUMBER_OF_SELECTABLE_COLOURS 10
    
    class VCsettings
    {
    public:
        VCsettings();
        void fillTreeWidget(QTreeWidget *my_tree);
        void showData();
    
    private slots:
        void comboBoxValueChanged(int);
    
    private:
        enum type_enum { HEADER, BOOL, VALUE };
        struct Setting_struct {
            QString name;
            type_enum type;
            int value;
            int min;
            int max;
            int sublist;
        };
    
    #define NUMBER_OF_SETTINGS 6
    
        Setting_struct VCsetting[NUMBER_OF_SETTINGS] = { // The values that are set here are the default settings of the application
            { "General Settings", HEADER, 0, 0, 0, 0 },
            { "Send tempo on patch change", BOOL, 1, 0, 1, 1 },
            { "US20 emulation active", BOOL, 0, 0, 1, 1 },
            { "Colour Settings", HEADER, 0, 0, 0, 0 },
            { "FX colour", VALUE, 1, 0, NUMBER_OF_SELECTABLE_COLOURS - 1, 4 },
            { "Default colour", VALUE, 3, 0, NUMBER_OF_SELECTABLE_COLOURS - 1, 4 },
        };
    
        QStringList menu_sublist = {
          // Sublist 1 - 3: Booleans
          "OFF", "ON", "DETECT",
    
          // Sublist 4 - 19: LED colours
          "OFF", "GREEN", "RED", "BLUE", "ORANGE", "TURQUOISE", "WHITE", "YELLOW", "PURPLE", "PINK", "", "", "", "", "", "",
    
          // Sublist 20 - 22: Main display modes
          "PAGE NAME", "PATCH NAME", "PATCHES COMBINED",
    
          // Sublist 23 - 30: MIDI ports
          "USB MIDI", "MIDI 1", "MIDI2/RRC", "MIDI 3", "ALL PORTS", "", "", "",
    
          // Sublist 31 - 34: Expression pedals
          "EXP PEDAL #1", "EXP PEDAL #2", "EXP PEDAL #3", "EXP PEDAL #4",
        };
    };
    
    #endif // VCSETTINGS_H
    

    Here is my VCsettings.cpp:

    #include "vcsettings.h"
    #include "comboboxdelegate.h"
    #include <QComboBox>
    #include <QDebug>
    
    VCsettings::VCsettings()
    {
    
    }
    
    void VCsettings::fillTreeWidget(QTreeWidget *my_tree)
    {
      my_tree->setColumnCount(2);
      my_tree->setHeaderLabels(QStringList() << "Parameter" << "Value");
      my_tree->setColumnWidth(0, 250);
    
      QTreeWidgetItem* parent_item = NULL;
    
      for (int i = 0; i < NUMBER_OF_SETTINGS; i++) {
          if (VCsetting[i].type == HEADER) {
              QTreeWidgetItem *item = new QTreeWidgetItem(my_tree);
              item->setText(0, VCsetting[i].name);
              my_tree->addTopLevelItem(item);
              parent_item = item;
          }
          else {
              if (parent_item) {
                QTreeWidgetItem *child = new QTreeWidgetItem(parent_item);
                child->setText(0, VCsetting[i].name);
                QComboBox *comboBox = new QComboBox(my_tree);
                //ComboBoxDelegate* comboBox = new ComboBoxDelegate(this);
                if (VCsetting[i].sublist > 0) { // Fill combobox with items from
                    int number_of_items = VCsetting[i].max - VCsetting[i].min + 1;
                    for (int j = 0; j < number_of_items; j++)
                      comboBox->addItem(menu_sublist.at(j + VCsetting[i].sublist - 1));
                    comboBox->setCurrentIndex(VCsetting[i].value);
                    //QAbstractItemDelegate::connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(comboBoxValueChanged(int)));
                }
                my_tree->setItemWidget(child, 1, comboBox);
                parent_item->addChild(child);
              }
          }
      }
    }
    
    void VCsettings::showData()
    {
        for (int i = 0; i < NUMBER_OF_SETTINGS; i++) {
            qDebug() << VCsetting[i].name << VCsetting[i].value ;
        }
    }
    
    void VCsettings::comboBoxValueChanged(int value)
    {
        qDebug() << value;
    }
    

    Relevant part of MainWindow.h:
    VCsettings *MyVCSettings;

    And in my MainWindows.cpp:

    MyVCSettings = new VCsettings();
        MyVCSettings->fillTreeWidget(ui->treeWidget);
    


  • Nope, setItemWidget is the enemy of the people! the correct way is subclass QStyledItemDelegate and create a combobox editor in createEditor


Log in to reply
 

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