Qt-interest Archive, February 2007
Wrong encoding from MySQL
Message 1 in thread
Hello trolls,
Recently I got a problem of getting data from MySQL (5.0.16) table, that
in Windows-1251 encoding. I'm using Qt 4.2.2 and VS2005. As I'm trying
to get the values from DB, i got either ??? or characters of another
encoding.
Hope on your help!
Below is the code of my program:
QtSQL::QtSQL(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setUserName("root");
db.setPassword("root");
db.setHostName("localhost");
db.setDatabaseName("test");
bool opened = db.open();
if (!opened) QMessageBox::information(this, "title", "problem",
QMessageBox::Ok);
QSqlQuery q("SET NAMES cp1251;");
}
void QtSQL::on_btn_2_clicked()
{
QSqlQuery q("SELECT * FROM `encoding` WHERE ID = 10;");
q.next();
QSqlRecord rec = q.record();
// it works fine if I uncomment the following lines, but I believe it
should not be done this way! :)
//QByteArray encodedString = rec.value("Text").toByteArray();
//QTextCodec *codec = QTextCodec::codecForName("Windows-1251");
//QString string = codec->toUnicode(encodedString);
QString string = rec.value("Text").toString();
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("windows-1251"));
ui.txt->setText(string);
}
--
[ signature omitted ]
Message 2 in thread
Hi,
> Recently I got a problem of getting data from MySQL (5.0.16) table, that
> in Windows-1251 encoding. I'm using Qt 4.2.2 and VS2005. As I'm trying
> to get the values from DB, i got either ??? or characters of another
> encoding.
I'm not really a specialist, but whatever the internal MySQL encoding,
you shouldn't have to worry about it with recent MySQL versions. From
the Qt 4.2 MySQL driver source file:
bool QMYSQLDriver::open(...)
{
[...]
#if MYSQL_VERSION_ID >= 50007
// force the communication to be utf8
mysql_set_character_set(d->mysql, "utf8");
#endif
Since you have MySQL 5.0.16, Qt should be able to retrieve strings
without further decoding, whatever the internal encoding of the database.
--
[ signature omitted ]
Message 3 in thread
Hello everyone,
I am reading a classic article: A Byte of Python, which says
a Python class has two types of fields - class variables and object
variables.
class Person:
population = 0 # class variable
def __init__(self, name):
self.name = name # object variable
Person.population += 1
“Class variables are shared in the sense that they are accessed by all
objects (instances) of that class. There is only copy of the class
variable and when any one object makes a change to a class variable, the
change is reflected in all the other instances as well.”
“Object variables are owned by each individual object/instance of the
class. In this case, each object has its own copy of the field i.e. they
are not shared and are not related in any way to the field by the same
name in a different instance of the same class. An example will make
this easy to understand.”
(see
http://swaroopch.info/text/Byte_of_Python:Object_oriented_programming )
I know all Qt developers are C++ experts, and they are object-orientated.
I wonder how to implement the class and object variables in C++.
Can anyone help to translate the above 5-line Python class into a C++
class?
Thanks in advance,
Lingfa
--
[ signature omitted ]
Message 4 in thread
On 06.02.07 17:57:27, lingfa wrote:
> I am reading a classic article: A Byte of Python, which says
> a Python class has two types of fields - class variables and object variables.
>
> I know all Qt developers are C++ experts,
Certainly not, there are bindings for Qt for Python and Ruby. Maybe have
a look into those, then you don't need to learn C++.
> and they are object-orientated.
> I wonder how to implement the class and object variables in C++.
> Can anyone help to translate the above 5-line Python class into a C++ class?
C++ has the same thing, the difference is that its defined at compile
time, all class variables have the static keyword in front and all other
variables are automatically object variables.
Andreas
--
[ signature omitted ]
Message 5 in thread
Hi,
as Andreas said there are some Python binding for C++, e.g.:
Elmer (free): http://elmer.sourceforge.net/
PythonQt (free): http://pythonqt.sourceforge.net/index.html
PyQt (commercial): http://www.riverbankcomputing.co.uk/pyqt/
Your class would be in C++:
class Person {
public:
// __init__ / constructor
Person(string name) : m_Name(name) { population++; }
protected:
static int population; // class variable
};
// statics must be initialized outside the class
static int Person.population = 0;
Just a simple example - there are many good C++ tutorials in the net.
Best Regards,
Christian
--
[ signature omitted ]
Message 6 in thread
Thanks, Christian for your references and code, and
thanks, Andreas for your comments.
The binding seems more interesting.
Lingfa
--
[ signature omitted ]
Message 7 in thread
Hi Christian,
allow me to nitpick for clarity's sake; Python code doesn't really mix
with C++ sources ;-)
Christian Dähn schrieb:
> Your class would be in C++:
>
> class Person {
> public:
> // __init__ / constructor
> Person(string name) : m_Name(name) { population++; }
>
> protected:
> static int population; // class variable
string m_Name; //Declaration missing
> };
>
> // statics must be initialized outside the class
> static int Person.population = 0;
int Person::population = 0; //Static scope resolution
Martin
--
[ signature omitted ]