IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Qt Discussion :

QDataStream >> QString donne une chaîne vide


Sujet :

Qt

  1. #1
    Membre habitué
    Avatar de Nykoo
    Profil pro
    Inscrit en
    Février 2007
    Messages
    234
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 234
    Points : 166
    Points
    166
    Par défaut QDataStream >> QString donne une chaîne vide
    Quand je tente de lire une QString à partir d'un QDataStream j'obtiens une QString vide. Et pour cause, j'ai regardé dans le "qstring.cpp" l'opérateur >> : si la longueur de ma QString a le malheur d'être impaire, alors il considère cela comme une erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    if (bytes & 0x1) {
                    str.clear();
                    in.setStatus(QDataStream::ReadCorruptData);
                    return in;
                }
    Code de la fonction:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    QDataStream &operator>>(QDataStream &in, QString &str)
    {
    #ifdef QT_QSTRING_UCS_4
    #if defined(Q_CC_GNU)
    #warning "operator>> not working properly"
    #endif
    #endif
     
        if (in.version() == 1) {
            QByteArray l;
            in >> l;
            str = QString::fromLatin1(l);
        } else {
            quint32 bytes = 0;
            in >> bytes;                                  // read size of string
            if (bytes == 0xffffffff) {                    // null string
                str.clear();
            } else if (bytes > 0) {                       // not empty
                if (bytes & 0x1) {
                    str.clear();
                    in.setStatus(QDataStream::ReadCorruptData);
                    return in;
                }
     
                const quint32 Step = 1024 * 1024;
                quint32 len = bytes / 2;
                quint32 allocated = 0;
     
                while (allocated < len) {
                    int blockSize = qMin(Step, len - allocated);
                    str.resize(allocated + blockSize);
                    if (in.readRawData(reinterpret_cast<char *>(str.data()) + allocated * 2,
                                       blockSize * 2) != blockSize * 2) {
                        str.clear();
                        in.setStatus(QDataStream::ReadPastEnd);
                        return in;
                    }
                    allocated += blockSize;
                }
     
                if ((in.byteOrder() == QDataStream::BigEndian)
                        != (QSysInfo::ByteOrder == QSysInfo::BigEndian)) {
                    ushort *data = reinterpret_cast<ushort *>(str.data());
                    while (len--) {
                        *data = (*data >> 8) | (*data << 8);
                        ++data;
                    }
                }
            } else {
                str = QLatin1String("");
            }
        }
        return in;
    }
    J'ai loupé quelque chose là?

  2. #2
    Membre habitué
    Avatar de Nykoo
    Profil pro
    Inscrit en
    Février 2007
    Messages
    234
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 234
    Points : 166
    Points
    166
    Par défaut
    En fait les QString prennent 2 octects par caractère donc impossible d'avoir un "bytes" impaire. Mon QDataStream devait contenir une std::string à mon avis.

  3. #3
    Expert éminent
    Avatar de _skip
    Homme Profil pro
    Développeur d'applications
    Inscrit en
    Novembre 2005
    Messages
    2 898
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur d'applications
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Novembre 2005
    Messages : 2 898
    Points : 7 752
    Points
    7 752
    Par défaut
    En même temps, pourquoi utiliser un QDataStream Pour lire du texte?

  4. #4
    Membre habitué
    Avatar de Nykoo
    Profil pro
    Inscrit en
    Février 2007
    Messages
    234
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 234
    Points : 166
    Points
    166
    Par défaut
    J'ai simplifié le problème au maximum, bien sur j'utilise le QDataStream pour une classe qui contient autre chose que du texte. Plus précisément c'est un arbre dont les noeux contiennent une classe quelconque (template) que j'enregistre.

Discussions similaires

  1. [T-SQL] Comment égaliser un tuple et une chaîne vide en SQL ?
    Par clementratel dans le forum Langage SQL
    Réponses: 2
    Dernier message: 02/07/2008, 12h39
  2. SQL server 2005: conversion d'une chaîne vide en numerique.
    Par Marco24 dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 23/04/2008, 11h35
  3. Erreur : Le champ ne peut pas être une chaîne vide
    Par supertoms dans le forum VBA Access
    Réponses: 5
    Dernier message: 23/04/2008, 07h05
  4. qu'est ce qu'une chaîne vide?
    Par ladygtk dans le forum C
    Réponses: 34
    Dernier message: 14/03/2007, 14h47
  5. copie de string donne une chaine vide
    Par zmatz dans le forum SL & STL
    Réponses: 5
    Dernier message: 15/10/2005, 15h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo