2

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();
}
Willeman
  • 720
  • 10
  • 24
  • 1
    Possible duplicate of [How do I add a header with data to a QTableWidget in Qt?](https://stackoverflow.com/questions/1793037/how-do-i-add-a-header-with-data-to-a-qtablewidget-in-qt) – Mohammad Kanan Jun 29 '18 at 16:22
  • [How to change header's title of a QTableView](https://stackoverflow.com/questions/17478993/how-to-change-headers-title-of-a-qtableview) – Mohammad Kanan Jun 29 '18 at 16:24
  • 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 Jun 29 '18 at 16:26
  • `model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));` – Mohammad Kanan Jun 29 '18 at 16:27
  • @Willeman, I am not sure why non of the answers mentions it, but you should override [`headerData()`](https://doc.qt.io/qt-5/qabstractitemmodel.html#headerData) in your custom model and provide your headers there... – Mike Jun 30 '18 at 11:27
  • @Mike thanks. I am actually overriding it. Will edit my question to reflect that. – Willeman Jul 02 '18 at 08:11

1 Answers1

0

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.

Willeman
  • 720
  • 10
  • 24