Qt中的翻译问题

Problems with translation in Qt

本文关键字:问题 翻译 Qt      更新时间:2023-10-16

我在使用 Qt 翻译时遇到问题:

在表格视图中,我正在使用委托来获取组合框作为编辑功能:

this->gndDelegate = new GenderDelegate(this);
ui->tableView->setItemDelegateForColumn(AthleteModel::GENDER_COLUMN, this->gndDelegate);

组合框有值,我想用tr()命令翻译这些值。所有其他翻译工作正常,但这两个添加的项目未翻译:

QWidget *GenderDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem    &option, const QModelIndex &index) const
{
QComboBox *cmbBox = new QComboBox(parent);
cmbBox->addItem(tr("male"), "male");
cmbBox->addItem(tr("female"), "female");
return cmbBox;
}

对于这两个值,qm 文件中存在表示

形式

感谢您的帮助...

必须在委托实现中添加指令Q_OBJECT。例如:

class KeyConfigurationDelegate : public QItemDelegate
{
    Q_OBJECT //Add This directive !!!
public:
    explicit KeyConfigurationDelegate(QObject *parent = 0);
    ~KeyConfigurationDelegate();
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
private:
    QStringList list;
    QStringListModel model;
};