始终在QTableView

时间:2017-11-30 14:56:29

标签: c++ qt qcombobox

我有一个带有相关模型的QTableView。我想在第三列的每个单元格中都有一个QComboBox。

我使用了QItemDelegate,如本页所示:https://wiki.qt.io/Combo_Boxes_in_Item_Views。 它工作但组合框仅在单击单元格后显示,用户必须再次单击以显示可能的值列表。这有点不方便。

有没有办法让组合框始终可见?

我尝试使用openPersistentEditor方法,但它现在不起作用...

我的代码附近有一些代码(ComboBoxItemDelegate与以前链接的示例相同):

MonWidget::MonWidget() : _ui(new Ui::MonWidget())
{
    // ...

    // Links Model
    _linksModel = new QStandardItemModel(this);

    // Links Headers
    QStringList linksTableViewHeader  << "Name" << "Path" << "Version";
    _linksModel->setHorizontalHeaderLabels(linksTableViewHeader) ;

    // Create itemDelegate for linksView
    _itemDelegate = new ComboBoxItemDelegate(_ui->_linksView);

    // Set the links model on the links table view
    _ui->_linksView->setModel(_linksModel);
    _ui->_linksView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
    _ui->_linksView->horizontalHeader()->setStretchLastSection(true);
    _ui->_linksView->horizontalHeader()->setMinimumSectionSize(100);
    _ui->_linksView->setSelectionBehavior(QAbstractItemView::SelectRows);
    _ui->_linksView->setItemDelegate(_itemDelegate);
}
// AddLinksInListView : method called when I add some rows ...
void MonWidget::AddLinksInListView(QList<DataItem*> listLinks)
{
    int j=0;
    int initialLinksNumber = _linksModel->rowCount();

    // For each link, add a row with the link information
    for (int row=initialLinksNumber; row<(initialLinksNumber + listLinks.size()) ; row++)
    {
        for (int column = 0; column < _linksModel->columnCount(); column++) {
            //item used to display information for each column in the table view of links contained in the collection
            QStandardItem *item=0;

            switch(column)
            {
                case MonWidget::NAME:
                {
                    item = new QStandardItem(listLinks.at(j)->data(MonWidget::NAME).toString());
                    _linksModel->setItem(row, column, item);
                    break;
                }
                case MonWidget::PATH:
                {
                    item = new QStandardItem(listLinks.at(j)->data(MonWidget::PATH).toString());
                    _linksModel->setItem(row, column, item);
                    break;
                }
                case MonWidget::VERSION:
                {
                    item = new QStandardItem(listLinks.at(j)->data(MonWidget::PATH).toString());
                    _linksModel->setItem(row, column, item);
                    _ui->_linksView->openPersistentEditor(_linksModel->index(row, column));
                    break;
                }
            }
        }
        j++;
    }
}

1 个答案:

答案 0 :(得分:0)

是的,有一种方法,使用openPersistentEditor。假设您的模型名为myModel

YourDelegate* comboDelegate = new YourDelegate(this);
setItemDelegateForColumn(2, comboDelegate); // delegate set for the third column

...
// when adding a new line, use this.
// The combo box will be always visible
openPersistentEditor(myModel->index(iRow, 1));