Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to dynamically translate QAbstractTableModel headers?
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to dynamically translate QAbstractTableModel headers?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved