[solved] Loading a translation file causes my app to crash...



  • The app is quite simple and there's no hackery involved. It loads the translation file in the standard way:
    @QTranslator translator;
    translator.load(QString("qtflux_") + QLocale::system().name());
    a.installTranslator(&translator);@

    Then, in a constructor of the main class I have a code which basically says this:
    @
    QAction *blah;
    (...)
    QFont df = qApp->font();
    df.setBold(1);
    blah->setFont(df);
    @

    This code crashes in setFont() If the translation file is loaded.

    No, blah in not 0.

    Any ideas would be very welcome.



  • That code doesn't look bad. Can you provide the complete source code or a complete sample project?



  • In provided code, QAction isn't inicialized.
    @QAction *blah = new QAction();@



  • Thanks for answering. I uploaded it here: http://www.mediafire.com/?63s2qux18xs7h68

    It crashes if I load the qtflux_pl.qm translation. The crash is in line 29 of qtflux.cpp



  • [quote author="Jake007" date="1330467930"]In provided code, QAction isn't inicialized.
    @QAction *blah = new QAction();@
    [/quote]

    I know it looks like that, that's why I said blah is not 0. I should have said it's initialized, as "not 0" not necesarily means it's valid ;) Let's say it's initialized inside the mysterious (...).



  • Crap, I figured it out. Fuck I'm stupid. Thanks for the answers either way.


  • Moderators

    What was the problem?



  • I have this @QMap<QString, QAction*> actions;@

    I initialized it like this:

    @ QStringList an = QStringList()<<tr("Enable")<<tr("Settings")<<tr("About")<<tr("Quit");
    foreach (QString s, an){
    actions[s] = cmenu->addAction(s);
    }@
    And used like this:
    @actions["Settings"]->setFont(df);@

    That is, I initialized it with a translated string and later used it with original string.



  • For using as the key into a hashtable I would advocate against translated strings. Use untranslated strings here. Do you have any reasons for using a map at all? If the number of actions is limited, why not use real member variables in your class?



  • bq. For using as the key into a hashtable I would advocate against translated strings.

    Yeah, I know. I heaven't really thought this through... :P

    bq. Do you have any reasons for using a map at all? If the number of actions is limited, why not use real member variables in your class?

    The reason is that I spent 3 or 4 days once hunting down a bug caused by copy-pasting similar code 10 times instead doing it in a loop. That experience turned me into a vicious code duplication hunter ;)



  • In that cases I use a hybrid approach:

    • distinct member variables (a QAction pointer) in case I need particular action
    • a QList<QAction *> holding them all if I need to loop


  • Yeah, you're probably right. Thanks again ;)


Log in to reply
 

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