Qt问题之 告别软件因系统默认中文输入法导致错误退出的烦恼

1. 问题

使用Qt进行研发时,遇到一个问题,当在系统默认输入法中文(英文输入法或者搜狗就不会触发闪退)的情况下,选中QTableWidget控件(QTableWidgetItem有焦点,但是不双击), 进行输入,会导致软件错误退出。

这个Bug在Qt的Bug记录里也存在:QTableWidgetItem cause stack overflow

2. 问题复现

输入以下代码,按照问题操作步骤,可复现问题。

cpp 复制代码
    ui->tableWidget->setCellWidget(0, 1, new QLineEdit);

3. 解决方案1 设置输入方式,紧急解决方案

cpp 复制代码
ui->tableWidget->setInputMethodHints(Qt::ImhHiddenText);

4. 解决方案2 每个设置Item的地方使用setCellWidget,非最优解

cpp 复制代码
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i)
    {
        for (int j = 0; j < ui->tableWidget->columnCount(); ++j)
        {
            ui->tableWidget->setCellWidget(i, j, new QLineEdit);
        }
    }

5. 解决方案3 使用代理,首选建议

示例代码
.h

cpp 复制代码
// 自定义代理类,继承自 QStyledItemDelegate
class SpinBoxDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    SpinBoxDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}

    // 创建编辑器,当用户开始编辑单元格时调用
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const override
    {
        QSpinBox *editor = new QSpinBox(parent);
        editor->setMinimum(0);
        editor->setMaximum(100);
        return editor;
    }

    // 设置编辑器的数据,将模型中的数据设置到编辑器中
    void setEditorData(QWidget *editor, const QModelIndex &index) const override
    {
        int value = index.model()->data(index, Qt::EditRole).toInt();
        QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
        spinBox->setValue(value);
    }

    // 将编辑器中的数据保存到模型中
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const override
    {
        QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
        int value = spinBox->value();
        model->setData(index, value, Qt::EditRole);
    }

    // 更新编辑器的几何形状,确保编辑器显示在正确位置
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                              const QModelIndex &index) const override
    {
        editor->setGeometry(option.rect);
        QStyledItemDelegate::updateEditorGeometry(editor, option, index);
    }
};

调用:

cpp 复制代码
#include <QtWidgets>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // 创建一个表格视图
    QTableView tableView;

    // 创建一个标准的表格数据模型
    QStandardItemModel model(4, 2);
    model.setHeaderData(0, Qt::Horizontal, "Integer");
    model.setHeaderData(1, Qt::Horizontal, "Square");

    // 为表格设置数据模型
    tableView.setModel(&model);

    // 创建一个自定义代理实例
    SpinBoxDelegate delegate;
    // 将代理应用到第一列
    tableView.setItemDelegateForColumn(0, &delegate);

    // 填充表格数据
    for (int row = 0; row < 4; ++row) {
        for (int column = 0; column < 2; ++column) {
            QModelIndex index = model.index(row, column);
            if (column == 0) {
                model.setData(index, row);
            } else {
                int value = row * row;
                model.setData(index, value);
            }
        }
    }

    tableView.show();

    return app.exec();
}
相关推荐
h39741 小时前
DirectShow过滤器开发-视频源过滤器
c++·windows·音视频·directshow
夏幻灵2 小时前
JavaScript this 面试:五种绑定规则、优先级与常见面试陷阱
开发语言·javascript·面试
程序猿编码2 小时前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码
开发语言·c++·大模型推理·qwen3·多模态推理
西飘客3 小时前
vs 怎么根据dll 生成lib文件
c++·qt
bkspiderx4 小时前
Qt 插件机制:动态扩展应用功能的核心框架
开发语言·qt·元数据·qt 插件·qpluginloader
程序员老陆4 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
沐晓时光4 小时前
C语言入门,深入理解指针(3)
c语言·开发语言
码云数智-园园4 小时前
unique_ptr 还是 shared_ptr?C++ 智能指针选型与内存泄漏实战分析
java·开发语言
青绿蓝LCA低碳研究院5 小时前
FSLCI首次在印度举办会议,我们需要学习什么? · 青绿蓝
经验分享
禾小西5 小时前
07丨Redis 哨兵机制:主库故障后,如何恢复服务?
java·开发语言·redis