How to set a values in present in one combobox to the combobox in a tableview ?



  • I have a source file "mainwindow.cpp" and I have a combobox in it whose values depend upon the pushbutton clicked.When a pushbutton is clicked an item named "Item 1" is added and when pressed again "Item 2" is added to combobox and so on..
    I have another source file "comboboxdelegate.cpp"
    comboboxdelegate.cpp

    #include <QtGui>
    
    #include "ComboBoxDelegate.h"
    
    ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
        : QItemDelegate(parent)
    {
    }
    
    QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
        const QStyleOptionViewItem &/* option */,
        const QModelIndex &/* index */) const
    {
        QComboBox *editor = new QComboBox(parent);
        editor->addItem("TR");
        editor->addItem("T");
        editor->addItem("R");
        editor->addItem("...");
        return editor;
    }
    
    void ComboBoxDelegate::setEditorData(QWidget *editor,
                                        const QModelIndex &index) const
    {
        QString value = index.model()->data(index, Qt::EditRole).toString();
    
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        comboBox->showPopup();
        if(value == "TR")
        {
            comboBox->setCurrentIndex(0);
        }
        else if(value == "T")
        {
            comboBox->setCurrentIndex(1);
        }
        else if(value == "R")
        {
            comboBox->setCurrentIndex(2);
        }
        else
        {
            comboBox->setCurrentIndex(3);
        }
    }
    
    void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                       const QModelIndex &index) const
    {
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        int value = comboBox->currentIndex();
    
        if(value == 0)
        {
            model->setData(index, Qt::white, Qt::BackgroundRole);
            model->setData(index, Qt::black, Qt::ForegroundRole);
            model->setData(index, "TR", Qt::EditRole);
        }
        if(value == 1)
        {
            QColor color;
            color.setRgb(0,102,102);
            model->setData(index, color, Qt::BackgroundRole);
            model->setData(index, Qt::white, Qt::ForegroundRole);
            model->setData(index, "T", Qt::EditRole);
        }
        if(value == 2)
        {
            QColor color;
            color.setRgb(102,51,0);
            model->setData(index, color, Qt::BackgroundRole);
            model->setData(index, Qt::white, Qt::ForegroundRole);
            model->setData(index, "R", Qt::EditRole);
        }
        if(value == 3)
        {
            QColor color;
            color.setRgb(160,160,160);
            model->setData(index, color, Qt::BackgroundRole);
            model->setData(index, Qt::white, Qt::ForegroundRole);
            model->setData(index, "...", Qt::EditRole);
        }    
    }
    
    void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
        const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    {
        editor->setGeometry(option.rect);
    }
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "comboboxdelegate.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        i=0;
        ui->setupUi(this);
        QStandardItemModel *model= new QStandardItemModel(4,2,this);
        ui->tableView->setModel(model);
        ComboBoxDelegate *comboDelegate = new ComboBoxDelegate();
       for(int row = 0; row < 4; row++)
                {
                    for(int col = 0; col < 2; col++)
                    {
                        QModelIndex index= model->index(row,col,QModelIndex());
                        model->setData(index,"0") ;
    
                    }
                }
        ui->tableView->setItemDelegateForColumn(2,comboDelegate);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        if(i<31)
            {
            ui->comboBox->addItem("Item"+QString::number(i));
            i=i+1;
            }
        else
            {
                QMessageBox msg(QMessageBox::Information,"Warning!","Items exceeded 32");
                 msg.exec();
            }
    }
    

    My column 2 of a tableview is a combobox i want to set the combobox values (item0,item1) to the second column of the combobox(in the tableview).
    What is the way to do it?


  • Lifetime Qt Champion

    Hi,

    Do you mean you want to set the current value of the combo box with the value contained in the cell of the model column ?



  • @SGaist
    Hi , yes exactly..where cell in a model is a combobox .


  • Lifetime Qt Champion

    That's what setEditorData should do


Log in to reply
 

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