Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Global Locales doesnt work for everything



  • Hey,

    to explain it shortly. I set this in my main.cpp File.

    QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
    

    So that my whole Project is in English. But there are some Party of my App that are still in German. Mostly the Dateformats like here.
    Unbenannt.png

    Normally it should show Dec and not Dez. I have to say that this is a special Widget and i already tried to set Locales in the Widget itself.

    Someone can help me? I thought Locales will be set Applicationwide.


  • Lifetime Qt Champion

    Hi,

    Can you provide a minimal compilable example that shows that issue ?

    What version of Qt are you using ?
    On what OS ?



  • I cant share everything at the Moment, but i will try to explain my Code. First, im using Qt 5.12.6 and im on Windows 10.

    In the Picture you see a QTableView Item and this TableView has a Model with this Data Method.

    QVariant DiaryTableModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        if (role == Qt::DisplayRole)
        {
            if (index.column() == 0)
                return mItems.at(index.row())->date.toString("d. MMM yyyy");
            else if (index.column() == 2)
                return mItems.at(index.row())->plain;
            else if (index.column() == 3)
                return mItems.at(index.row())->tags;
            else if (index.column() == 4)
                return mItems.at(index.row())->createdAt.toString("d. MMM yyyy");
            else if (index.column() == 5)
                return mItems.at(index.row())->updatedAt.toString("d. MMM yyyy");
        }
        if (index.column() == 1)
            return QVariant::fromValue(mItems.at(index.row())->rating);
    
        return QVariant();
    }
    

    The Model is set in a Widget like this.

    mDiaryTable = new QTableView(this);
        mDiaryTable->setStyleSheet("QTableView { margin-bottom: 5px; border: 1px solid black; }"
                                   "QTableView:active { color: black; }"
                                   "QHeaderView::section { background-color: rgba(22, 15, 42, 1); color: white; }");
        mDiaryModel = new DiaryTableModel(this);
        mDiaryTable->setModel(mDiaryModel);
    

    The Widget that im using is a Widget that inherits from QWidget. And i use the Widget like this in the MainWindow.

    this->_wDiary.reset(new DiaryWidget());
    

    wDiary is a SharedPointer.

    Maybe the Problem is that the DiaryWidget is not a Child from the Parent MainWindow? But why i can set the Locales in the DiaryWidget. When i pass this to DiaryWidget(this) then the Widget is not shown correctly.

    Update:

    I tried to set the QTableView to the Locales with

    mDiaryTable->setLocale(QLocale(QLocale::English, QLocale::UnitedStates));
    

    that also doesnt work.



  • Nobody has a Tipp what i can looking for? Dont know how to solve this or what i need to search for in the Internet. Its weird that my QTableView does not use the Applicationwide setup.


  • Qt Champions 2019

    Please provide a minimal testcase to reproduce the issue. The issue you're seeing can have many reasons.



  • I tried to create a new Project and removed everything unnecessary. Still the same Problem. I uploaded it in Github, so you can try it.
    At the Moment you cant insert Data to the Project in the Application. You need to start the Application so the Sqlite Database will be created and then you need to insert data.

    https://github.com/FuelKubitox/KubitoxLeartTest


  • Qt Champions 2019

    This doesn't look like a minimal example. Please try to reproduce it with a single view + model.



  • The Project is reduced to minimum. Without the other Things the Table doesnt work correctly. I can remove the Delegate and some Things from the Desing, but thats all. @Christian-Ehrlicher I mean, if you want to run the Code, why you dont clone the Repository and run it? Im happy that someone has the Will to help, but i already reduced the Project and create a new Repository. For a Single View with Model i have to create a new Project and code everything from Start. Maybe with copy and pasting, but why should i do that if i expose everything in a Repository?


  • Qt Champions 2019

    I was looking for something like this:

    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
        QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
        QDate d(QDate::currentDate());
        qDebug() << d.toString("d. MMM yyyy");;
        return app.exec();
    }
    

    QDate(Time)::toString() is using the system locale. If you want to use the default locale you have to use QLocale::toString()



  • Ah ok, didnt know that. I fixed my Model and now it works. Have to use something like this.

    QLocale locale;
    if (index.column() == 0)
         return locale.toString(mItems.at(index.row())->date, "d. MMM yyyy");
    

Log in to reply