首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QItemDelegate中编辑器的大小

QItemDelegate中编辑器的大小
EN

Stack Overflow用户
提问于 2017-01-02 22:51:57
回答 1查看 425关注 0票数 0

我有一个自定义委托,从QItemDelegate继承而来,它在第一列中提供一个QComboBox,在所有其他列中提供一个QLineEdit。

代码语言:javascript
复制
SensorDisplayDelegate::SensorDisplayDelegate(QObject *parent) :
    QItemDelegate(parent)
{}

QWidget *SensorDisplayDelegate::createEditor(QWidget *parent,
                                             const QStyleOptionViewItem &option,
                                             const QModelIndex &index) const
{
    int col = index.column();
    if(col == 0)
    {
        QComboBox *comboBox = new QComboBox(parent);
        connect(comboBox, SIGNAL(activated(int)), this, SLOT(setData(int)));
        comboBox->setEditable(false);
        //comboBox->setMaximumSize(editorSize);
        comboBox->setInsertPolicy(QComboBox::NoInsert);
        currentComboBox = comboBox;
        return comboBox;
    }
    else       
    {
        QLineEdit *lineEdit = new QLineEdit(parent);
        return lineEdit;
    }

    return NULL;
}

void SensorDisplayDelegate::setEditorData(QWidget *editor,
                                          const QModelIndex &index) const
{
    int col = index.column();
    if(col == 0)
    {
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        QStringList comboItems = index.data(Qt::EditRole).toStringList();

        comboBox->addItem("Add New Sensor");
        comboBox->addItems(comboItems);

        QCompleter *completer = new QCompleter(comboItems);
        completer->setCaseSensitivity(Qt::CaseInsensitive);
        comboBox->setCompleter(completer);
        comboBox->showPopup();
    }
    else
    {
        QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
        lineEdit->setText(index.data(Qt::EditRole).toString());
        lineEdit->show();
    }
}

void SensorDisplayDelegate::setModelData(QWidget *editor,
                                         QAbstractItemModel *model,
                                         const QModelIndex &index) const
{
    int col = index.column();
    if(col == 0)
    {
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        if(comboBox->currentIndex() == 0)
            emit addNewSensor();
        else
            emit populateSensorView(comboBox->currentText());
    }
    else
    {
        QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
        model->setData(index, QVariant(lineEdit->text()));
    }

}

void SensorDisplayDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}

QSize SensorDisplayDelegate::sizeHint(const QStyleOptionViewItem &option,
                                      const QModelIndex &index) const
{
    return editorSize;
}


void SensorDisplayDelegate::setData(int option)
{
    emit commitData(currentComboBox);
    emit closeEditor(currentComboBox);
}

editTrigger已设置为selectClicked。我希望组合框覆盖QTableView中的整个单元格。然而,现在它只是在左上角显示为斑点。我尝试通过在QTableView上侦听mousePressed的事件过滤器传递单元格大小来设置最小大小。但是,永远不会调用委托中的相应槽。代码如下:

MultiEventFilter.cpp:

代码语言:javascript
复制
bool MultiEventFilter::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        if(obj->objectName() == "sensorlocationTableView")
        {
            QTableView *sensorView = static_cast<QTableView*>(obj);
            QModelIndexList idxs = sensorView->selectionModel()->selectedIndexes();
            if(!idxs.empty())
            {
                QModelIndex idx = idxs.at(0);
                emit passCellSize(QSize(sensorView->columnWidth(idx.column()),
                                        sensorView->rowHeight(idx.row())));
            }
        }
    }

    return false;
}

安装在qApp上。

MainWindow.cpp:

代码语言:javascript
复制
eFilter = new MultiEventFilter();
    connect(eFilter, SIGNAL(passCellSize(QSize)),
            sensor_display_delegate, SLOT(setEditorSize(QSize)));

SensorDisplayDelegate.cpp插槽:

代码语言:javascript
复制
void SensorDisplayDelegate::setEditorSize(const QSize &size)
{
    editorSize = size;
}

其中QSize editorSize是私有成员。如何正确设置编辑器的大小?我需要一些通用的东西,可以应用于QLineEdit编辑器以及。另外,是否有必要在编辑器关闭时显式发出commitData()?我在任何涉及QComboBox的示例代码中都没有看到过这一点。

EN

回答 1

Stack Overflow用户

发布于 2017-01-02 23:10:15

我怀疑你的eventFilter在设置选择索引之前拦截了点击事件。所以,你有效地总是命中一个空的idxs IndexList?

尝试将该循环替换为以下内容:

代码语言:javascript
复制
bool MultiEventFilter::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        if(obj->objectName() == "sensorlocationTableView")
        {
            emit locationTableViewClicked();
        }
    }

    return false;
}

....

connect(eFilter, SIGNAL(locationTableViewClicked()),
        sensor_display_delegate, SLOT(setEditorSize()));
...

void SensorDisplayDelegate::setEditorSize()
{
    QModelIndexList idxs = sensorView->selectionModel()->selectedIndexes();
    if(!idxs.empty())
    {
        QModelIndex idx = idxs.at(0);
        editorSize = QSize(sensorView->columnWidth(idx.column()),
                           sensorView->rowHeight(idx.row()));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41428951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档