query.execute("SET NAMES 1251");
#include "vwdb.h"#include <QSqlDatabase>#include <QSqlQuery>#include <QSqlError>#include <QTextStream>#include <QTranslator>ViewDB::ViewDB(QWidget *parent): QWidget(parent){ setupUi(this); connectionDB(); model = new QSqlTableModel(this); model->setTable("TestTable"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); model->setHeaderData(0, Qt::Horizontal, tr("Идентификатор")); model->setHeaderData(1, Qt::Horizontal, tr("Статус")); tvwView->setModel(model);}void ViewDB::connectionDB(){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("TestDB"); db.setUserName("root"); db.setPassword("rootpass"); db.open(); QSqlQuery query; query.exec("SET NAMES utf8"); query.exec("SELECT* FROM TestTable"); query.next(); codec=QTextCodec::codecForName("utf8"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); query.prepare("INSERT INTO person (id, status) " "VALUES (:id, :status)"); query.bindValue(":id", 40); query.bindValue(":status", "Гость"); query.exec(); setWindowTitle("Текст в ср1251");}