QComboBox in the QTableView



  • Hi Guys,

    I want to directly display the QComboBox in the QTableView, not need to double click, Could anyone give me some ideas? thanks in advance.



  • Use QTableWidget and setCellWidget.
    A sample is shown below:
    @QComboBox *item = new QComboBox();
    ...
    table_widget->setCellWidget(row, column, item);@



  • good idea,but I use QTableView not QTableWidget, I create my own delegate and model, but by this way,the QComboBox can not be displayed and shown directly. welcome any other suggestions



  • QTableView is inherited by QTableWidget, so there should not be any incompatibilities.
    AFAIK you won't do what you are willing to do without hard codding a custom QTableView.



  • excellent, If I have to use QTableView to display QComboBox directly? what should I do?


  • Moderators



  • @void ImportWellsDialog::setImportTableModel(QObject *obj)
    {
    QAbstractItemModel *model = qobject_cast<QAbstractItemModel *>(obj);
    delete this->m_importTableModel;
    m_importTableModel = model;
    if(model)
    {
    for( int i = 0; i < model->rowCount(); ++i )
    {
    // ui->importTableView->setIndexWidget(model->index(i, 3, QModelIndex()), new QComboBox);
    // ui->importTableView->openPersistentEditor(model->index(i, 3, QModelIndex()) );
    if(i == 1)
    {
    ui->importTableView->openPersistentEditor(model->index(i, 5, QModelIndex()));
    // ui->importTableView->setIndexWidget(model->index(i, 3, QModelIndex()), new QComboBox);
    }
    }

    }
    ui->importTableView->setModel(model);
    new MyHeader(ui->importTableView->horizontalHeader(),ui->importTableView);
    

    }@



  • @ImportWellsDialog::ImportWellsDialog(QWidget *parent) :
    DialogBase(parent, false),
    m_importTableModel(0),
    ui(new Ui::ImportWellsDialog)
    {
    ui->setupUi(this);
    setObjectName("ImportWellsDialog");
    ui->importTableView->setShowGrid(false);
    ui->importTableView->verticalHeader()->hide();
    ui->importTableView->setItemDelegate(new CustomDelegate(ui->importTableView));
    ui->importTableView->setItemDelegateForColumn(3,new WellsComboBoxDelegate(ui->importTableView));
    ui->importTableView->setItemDelegateForColumn(5,new WellsComboBoxDelegate(ui->importTableView));
    }
    @



  • @QWidget *WellsComboBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {
    Q_UNUSED(option);

    //    if(index.column() == 2)
    

    // {
    // for(int i = 0; i < list.size(); i++)
    // {
    // editor->addItem(list.at(i)->data(0).toString());//abbreviation's role is 0
    // }
    // Guard::checkIfTrue(QObject::connect(editor, SIGNAL(activated(int )),
    // this, SLOT(comboBoxdataChanged(int))),HERE);
    // }
    if(((index.column() == 3)&&(index.row() != 0)) ||
    ((index.column() == 5)&&(index.row() == 1)))
    {
    QComboBox *editor = new QComboBox(parent);
    editor->setEditable(false);
    editor->setAutoFillBackground(true);
    return editor;

    }
    return 0;
    

    }@



  • What do you expect us to do with your code? Does it work now or not?


Log in to reply
 

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