Results 1 to 6 of 6

Thread: Using setCodecForCStrings(codec) in Constructor

  1. #1
    Join Date
    Jun 2007
    Posts
    5
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Using setCodecForCStrings(codec) in Constructor

    Hi,
    I want to save utf8 encoded QString's in a class called letter. I set the encoding in the constructor. Unfortunately, this means that the first object has already been initialized with the default encoding when the execution of the constructor reaches this statement. Thus, the first element has always the default (wrong) encoding. This is probably more of a C++ issue but I would nonetheless be very grateful if someone points me to a suitable solution.
    I know, it would be better to write the files in latin1 instead of utf8 but yet, I would like to do it this way ... for various reasond.

    Thanx in advance
    Daniel Betz

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using setCodecForCStrings(codec) in Constructor

    void QTextCodec::setCodeForCStrings is static.
    You can call this and set a codec in main.cpp, right after you create the QApplicationObject.

    Therefore, besides the QApplication, everything will use that codec.
    You could even try setting it before instantiating QApllication, but I'm not sure if it will work.

    Regards

  3. The following user says thank you to marcel for this useful post:

    danbetz (30th July 2007)

  4. #3
    Join Date
    Jun 2007
    Posts
    5
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Using setCodecForCStrings(codec) in Constructor

    I reduced the Problem as far as i could. The out commented part for setting the codec doesn't make a different.
    When i execute the Program it shows the first letter wrong, and the second right.
    I think the reason is that i need to set the codec for the class letter. But when it is done at first in the constructor - it is too late, because the QStrings are already initialized with the default latin1 standard- codec. How can I easily solve this without too much modification to the source as it is now ?
    Attached Files Attached Files

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using setCodecForCStrings(codec) in Constructor

    This is all I could do so far.
    I am not even sure it works OK, since it gives a strange char for alpha.

    Anyway, it didn't work for you since you were using statics, which actually got initialized before main.

    Regards
    Attached Files Attached Files

  6. The following user says thank you to marcel for this useful post:

    danbetz (30th July 2007)

  7. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using setCodecForCStrings(codec) in Constructor

    It is working now.
    The idea is not to instantiate Letter objects before you get to set the codec.

    GreekAlphabet should behave like a Letter factory and must be instantiated before all Letter objects.

    So I removed the statics and created two(for now) static getters that return alpha and beta.

    You could improve it further and do it more general.

    Regards
    Attached Files Attached Files

  8. The following user says thank you to marcel for this useful post:

    danbetz (30th July 2007)

  9. #6
    Join Date
    Jun 2007
    Posts
    5
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Using setCodecForCStrings(codec) in Constructor

    Thank you very much - that is exactly what i wanted.

Similar Threads

  1. QDialog: show() and exec() together in constructor?
    By Teuniz in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2007, 11:43
  2. Killing a Window in its constructor
    By hardgeus in forum Qt Programming
    Replies: 6
    Last Post: 15th December 2006, 18:31
  3. Replies: 2
    Last Post: 17th October 2006, 19:25
  4. use libs under qt4
    By raphaelf in forum Qt Programming
    Replies: 6
    Last Post: 27th February 2006, 17:59
  5. What default constructor?
    By bitChanger in forum General Programming
    Replies: 5
    Last Post: 15th February 2006, 19:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.