2
votes

For instance if the header displayed "ColumnName" in English I have tried to change it to a new language by handling the language change event:

QApplication::instance()->installTranslator( translator );
ui->retranslateUi(this);
ui->tableView->retranslate();

and then calling

model->setHeaderData(0, Qt::Horizontal, tr("ColumnName"), Qt::DisplayRole);
model->headerDataChanged(Qt::Horizontal, 0, 1);

But this does not seem to trigger the view to update. All the other widgets do display in the new language.

In the derived model class I have also overridden the QAbstractTableModel headerData() function:

QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
    {
    if (orientation == Qt::Horizontal) {
      switch (section)
      {
      case Priority:
        return tr("ColumnName");
      case FileName:
        return tr("Filename");
      default:
        return QString("");
      }
    }
  }  
  return QVariant();
}
1
I already looked at those entries. The first is outdated. There is no such method in qt 5 anymore. Not in the view form of QTable. The second is only applicable when using a QStandardItem model, which I am not using. - Willeman
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); - Mohammad Kanan
@Willeman, I am not sure why non of the answers mentions it, but you should override headerData() in your custom model and provide your headers there... - Mike

1 Answers

0
votes

Thank you for the insights. It turns out there was a simple mistake in my derived model header file. The class needs to have the Q_OBJECT macro present for the translation process to work correctly. It now updates the headers correctly.