用自定义信号(不仅仅是点击)触发QTableView的带有图标的单元格处于编辑状态

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GengWenhui123/article/details/88056246

有时可能不想通过鼠标点击QTableView的单元格使其处于编辑状态,而是想通过绑定槽函数的方式去实现。

下面进行说明:

openPersistentEditor(Index);打开编辑

closePersistentEditor(Index);关闭编辑

我的表格第一列的单元格需要编辑,并且单元格内有图标,编辑时图标消失,完成时图标显示。

打开编辑比较好处理,主要是关闭编辑,需要重写鼠标事件和键盘事件。

绑定打开编辑槽函数

connect(m_pRenameAction, &QAction::triggered, this, &MaterialTable::slotOpenPersistentEditor);
void MaterialTable::slotOpenPersistentEditor()
{
    if (currentR >= 0)
    {
        m_pLastItem = m_pStdTableModel->item(currentR, 0);
        m_lastIndex = m_pLastItem->index();
        m_icon = m_pLastItem->icon(); //保存原有图标
        m_pLastItem->setIcon(QIcon());//设置空图标

        openPersistentEditor(m_lastIndex);
        m_pEditCell = static_cast< QLineEdit* >(indexWidget(m_lastIndex));
        m_pEditCell->setStyleSheet("color:white");
        m_pEditCell->setFocus();
        //如果是文件名,默认选择文件名,不选择后缀
        if (TXT != m_currentType)
        {
            QString text = m_pLastItem->text();
            int index = text.lastIndexOf('.');
            index = index > 0 ? index : text.size();
            m_pEditCell->setSelection(0, index);
        }
    }
}

关闭编辑框函数

void MaterialTable::slotClosePersistentEditor()
{
    if (nullptr != m_pEditCell)
    {
        closePersistentEditor(m_lastIndex);
        m_pEditCell = nullptr;
    }
    if (!m_icon.isNull())
        m_pLastItem->setIcon(m_icon); //加载原有图标
}

主要是重写鼠标按下函数和键盘事件函数

MaterialTable 继承ResourceTable, RescourceTable 继承QTableView。

void MaterialTable::mousePressEvent(QMouseEvent *event)
{
    auto index = indexAt(event->pos());
    int tempRow = index.row();
    int tempCol = index.column();
    qDebug() << tempRow << "=" << currentR << "," << tempCol << "=" << currentC;
    if (tempRow < 0 || tempRow != currentR || tempCol != 0)
    {
        slotClosePersistentEditor();
    }
    if (event->button() == Qt::RightButton)
    {
        //这里代码主要是右键弹出菜单
        if (tempRow >=0 /*&& tempRow != currentR && tempCol != currentC*/)
        {
            m_currentType = m_pStdTableModel->data(index).toString().toStdString();
            if (TXT != m_currentType)
            {
                m_pMediaMenu->move(cursor().pos());
                m_pMediaMenu->show();
            }
            else
            {
                m_pTxtMenu->move(cursor().pos());
                m_pTxtMenu->show();
            }
        }
        currentR = tempRow;
        currentC = tempCol;
        if ( -1 == currentR || -1 == currentC)
        {
            emit signMouseRight();
        }
    }
   
    ResourceTable::mousePressEvent(event);
}
void MaterialTable::keyPressEvent(QKeyEvent *event)
{
   
    if (event->key() == Qt::Key_Enter || event->key() ==  Qt::Key_Escape || event->key() == Qt::Key_Return)
    {
        slotClosePersistentEditor();
    }
    ResourceTable::keyPressEvent(event);
}

注意!

关闭编辑框之前设置图标,回车后编辑的文本消失,还是原来的旧文本。

如果你在关闭编辑框之前(调用closePersistentEditor()之前)设置了原有图标(调用 m_pLastItem->setIcon(m_icon);),此时该Item的原有文本会覆盖掉你现在编辑框中的内容(看起来你的编辑框内容没有发生变化,其实如果你调用LineEdit::text()返回的就是你编辑之前的文本),这里要特别注意,setIcon()函数会用原来的文本覆盖掉编辑框内新编辑的文本(不仅仅是设置图标)。

猜你喜欢

转载自blog.csdn.net/GengWenhui123/article/details/88056246
今日推荐