Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6672711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:32:26+00:00 2026-05-26T03:32:26+00:00

I’m writing a API to be used in the creation of Interfaces at work.

  • 0

I’m writing a API to be used in the creation of Interfaces at work. The API allows the user to pick from a set of pre-built widgets and layouts so that multiple interfaces can be created for different units in a short period of time. There will be two translation files; one for the API widgets (which come with the library) and another one that the developer will create for custom data in the interface. To make it easier on the developer I wanted the API to handle all translation by passing the name of the data to the API, but I’ve come to a stumbling block; I can’t get the translator to recognize translated text sent to it, but it will recognize local literal strings.

Here’s a short example of what I’m talking about.

class Object
{
    public:
        Object(QString name) { m_Name = name; };
        QString name() { return m_Name; };
    private:
        QString m_Name;
};

class MyWidget : public QPushButton, public Object
{
    Q_OBJECT

    public:
        MyWidget(QString name);
        ~MyWidget();
        void retranslate();

    protected slots:
        void buttonPressed();
        void changeEvent(QEvent* event);

    private:
        enum Language { ENGLISH, JAPANESE };
        Language m_Language;
        QTranslator* m_pTranslator;
};

MyWidget::MyWidget(QString name)
:Object(name) // this does not work, but :Object(tr("TEST")) does
{
    m_pTranslator = new QTranslator();
    m_Language = ENGLISH;
    connect(this, SIGNAL(pressed()), this, SLOT(buttonPressed()));
    retranslate();
}

MyWidget::~MyWidget()
{
    delete m_pTranslator();
}

void MyWidget::buttonPressed()
{
    std::string qm;

    m_Language == ENGLISH ? m_Language = JAPANESE : m_Language = ENGLISH;
    m_Language == ENGLISH ? qm = "lang_en" : qm = "lang_jp";

    qApp->removeTranslator(m_pTranslator);

    if(!m_pTranslator->load(qm.c_str()))
        std::cout << "Error loading translation file\n";

    qApp->installTranslator(m_pTranslator);
}

void MyWidget::retranslate()
{
    setText(tr(name().toStdString().c_str()));
}

void MyWidget::changeEvent(QEvent* event)
{
    if(event->type() == QEvent::LanguageChange)
        retranslate();
    else
        QWidget::changeEvent(event);
}


class MainWindow : public QMainWindow
{
    Q_OBJECT

    public:
        MainWindow();
        ~MainWindow();

    private:
        MyWidget* m_pButton;
};

MainWindow::MainWindow()
{
    m_pButton = new MyWidget(tr("TEST")); // this is what I want to do, but this will not translate
    setCentralWidget(m_pButton);
}

MainWindow::~MainWindow()
{
    delete m_pButton;
}

// main.cpp
int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
 }

I typed this out by hand so there might be a couple of typos, but the concept still holds true – you have to call setText in the same class your set your literal string. If you pass a literal to the class, like I’m doing here, it will be ignored. If I make the literal in the class it works fine. This is a problem because I want the developer to pass a literal to the class and then let it do the translation. The developer will still need to make their own translations, but I don’t want them to worry about handling the translations.

Am I doing something wrong or is this a Qt limitation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:32:27+00:00Added an answer on May 26, 2026 at 3:32 am

    I suspect this is due to the fact that:

    m_pButton = new MyWidget(tr("TEST"));
    

    defines a string in the context of MainWindow, and you try to translate TEST in the context of MyWidget. You can circumvent this by using the static tr() method on QObject. This will define TEST as a translation in the global QObject context. This can be done by adapting the button widget creation code as follows:

    m_pButton = new MyWidget(QObject::tr("TEST"));
    

    and in MyWidget::retranslate():

    setText(QObject::tr(name().toStdString().c_str()));
    

    Note that you need to regenerate your translation files for this to work!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.