[Solved] How to dynamically translate QAbstractTableModel headers?



  • I've a QAbstractTableModel subclass and a QTableView already working together. I've also implemented a dynamic translation by demand, which is working but not with the headers of my QAbstractTableModel subclass. How to get this dinamically translated if I can't use "ui->retranslateUi(this)" ?

    My QAbstractTableModel subclass looks like this:

    @
    MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent)
    {

    header << tr("Column 01") << tr("Column 02") << tr("Column 03");
    

    }// Constructor

    QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const {

    if(orientation == Qt::Horizontal) {
    
        if(role == Qt::DisplayRole) {
    
            return header.at(section);
    
        }// if
    
    }// if
    
    return QAbstractTableModel::headerData(section, orientation, role);
    

    }// headerData

    ...

    @

    ...some suggestion about this?

    Thanks in advance!



  • First of all: good question! Thanks for asking!

    I have never tried this myself, but I'd try this route: Qt has a QEvent::LanguageChange event. I am not sure what classes it is send to when a language change occurs. You could try and see if it is send to all objects (including your model) by reimplementing the virtual bool event(QEvent* e) method in your model, and see if you receive this event. If not, you might install an event filter on the application object instead to listen for this event. In that case, you'll receive the events in the virtual bool eventFilter( QObject* watched, QEvent* event) method.

    When you receive the LanguageChange event, you should simply re-set your headers.

    Don't forget to make sure normal event handling continues as well, otherwise I bet funny things will start happening.



  • I've used the QEvent::LanguageChange event in other parts of my code, as I said my program gets dynamically translated successfully but with the exception of the headers of my QAbstractTableModel subclass. Maybe my question wasn't the correct, I was looking for an equivalent to "ui->retranslateUi(this)”. As there is no equivalent, I followed your suggestion and I've used again the QEvent::LanguageChange event and everything is working fine now.

    Thanks for your help Andre!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.