0

I'm having problem when adding record to QSqlTableModel:

QString name = out.getName().left(384/6); //UTF-16 max bytes/char = 6
qDebug() << "name:" << name;
record.setValue("name", name); //VARCHAR(384)
record.setValue("data", out.getData());
if (!boardLayoutsModel->insertRecord(-1, record)) {
    qDebug() << "err:" << boardLayoutsModel->lastError().text();
}

If name contains only base characters, everything is fine: code returns

name: "Nowy uklad tablicy"

Name is taken from QLineEdit, so if it contains polish characters, e.g. "Nowy układ tablicy" is visible on the text field, it returns:

name: "Nowy uk³ad tablicy"

err: "Incorrect string value: '\xB3ad ta...' for column 'name' at row 1 QMYSQL3: Unable to execute statement"

I figured to use QString::toUtf8, and then there is no error and SELECT on the table returns good value (with "ł"), but what Qt is getting back from database is wrong and yet different:

Nowy ukÅad tablicy

Now I've changed the collation of database, from utf8_general_ci to utf16_unicode_ci as it's native collation of QString AFAIK. Still the same error appears. I would just use QString::fromUtf8 to read the value, but QSqlTableModel works on it's own. Weird thing is that it's not my first time doing MySQL/Qt integration via QSqlTableModel, but I've never had even similar problems before... I've just upgraded Qt, so that may be that. Any ideas?

Community
  • 1
  • 1
smsware
  • 429
  • 1
  • 13
  • 41

1 Answers1

0

Fixed with:

QLocale::setDefault(QLocale(QLocale::Polish, QLocale::Poland));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
smsware
  • 429
  • 1
  • 13
  • 41