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

有时可能不想通过鼠标点击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()函数会用原来的文本覆盖掉编辑框内新编辑的文本(不仅仅是设置图标)。

如果不想使用代理(QStyledItemDelegate)来实现禁止选择某个单元格的功能,可以通过设置 QTableView 的 itemSelectionChanged 信号来过滤不允许选择的单元格。 具体步骤如下: 1. 继承 QTableView 类,重写 itemSelectionChanged 信号函数: ```c++ class MyTableView : public QTableView { Q_OBJECT public: MyTableView(QWidget* parent = nullptr) : QTableView(parent) { } protected: void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) override { // 遍历所有已选择的单元格 for (QModelIndex index : selected.indexes()) { // 判断单元格是否允许被选择 if (!isCellSelectable(index.row(), index.column())) { // 如果单元格不允许被选择,则取消选择该单元格 QItemSelection selection(index, index); selectionModel()->select(selection, QItemSelectionModel::Deselect); } } // 调用父类的 selectionChanged 函数 QTableView::selectionChanged(selected, deselected); } private: bool isCellSelectable(int row, int column) const { // 在这里判断某个单元格是否允许被选择 // 如果允许被选择,返回 true;否则返回 false return !(row == 1 && column == 1); // 这里以第 2 行第 2 列的单元格为例,不允许被选择 } }; ``` 2. 在需要使用 QTableView 的地方,创建一个实例化的 MyTableView 对象: ```c++ // 创建 MyTableView 对象 MyTableView* tableView = new MyTableView(this); ``` 这样,就可以实现禁止选择某个单元格的功能了。在上面的例子中,第 2 行第 2 列的单元格将无法被选择。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xhh-cy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值