Results 1 to 5 of 5

Thread: Chat client QPixmap

  1. #1
    Join Date
    Apr 2015
    Posts
    3

    Default Chat client QPixmap

    Hello,

    I would like to paint an avatar on a QTextEdit using a given QPixmap, before the pseudonym and the message.

    The QTextEdit will be used to see conversations between 2 or more clients.

    Here is the code in relation with the problem :


    Qt Code:
    1. // On prépare le paquet à envoyer
    2. QByteArray paquet;
    3. QDataStream out(&paquet, QIODevice::WriteOnly);
    4.  
    5.  
    6.  
    7. QString messageAEnvoyer = "<strong>" + lineEditPseudo->text() +"</strong> : " + lineEditMessage->text();
    8.  
    9. out << (quint16) 0;// On écrit 0 au début du paquet pour réserver la place pour écrire la taille
    10.  
    11. out <<messageAEnvoyer;// On ajoute le message à la suite
    12. out.device()->seek(0);// On se replace au début du paquet
    13. out << (quint16) (paquet.size() - sizeof(quint16)); // On écrase le 0 qu'on avait réservé par la longueur du message
    14.  
    15. socket->write(paquet); // On envoie le paquet
    To copy to clipboard, switch view to plain text mode 

    Any help will be appreciated.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Chat client QPixmap

    How is a snippet of code serializing a couple of strings and sending them through a socket relevant to your stated objective of painting a QPixmap on a QTextEdit? And, more importantly, what is your question?

  3. #3
    Join Date
    Apr 2015
    Posts
    3

    Default Re: Chat client QPixmap

    My question is how to draw a QPixmap into a QTextEDit.

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Chat client QPixmap

    Get a cursor at the right position in the QTextEdit, and use QTextCursor::insertImage(). You will first need to register the image with the resource system.

  5. #5
    Join Date
    Apr 2015
    Posts
    3

    Default Re: Chat client QPixmap

    Hello,

    I succeeded to draw the image at the right position with QTextCursor like this:

    Qt Code:
    1. QDataStream in(socket);
    2.  
    3. if (tailleMessage==0)//Si on ne connaît pas encore la taille du message, on essaie de la récupérer
    4. {
    5. if (socket->bytesAvailable()<(int)sizeof(quint16))//On n'a pas reçu la taille du message en entier
    6. return;
    7.  
    8. in>>tailleMessage;//Si on a reçu la taille du message en entier, on la récupère
    9. }
    10.  
    11. //Si on connaît la taille du message, on vérifie si on a reçu le message en entier
    12. if (socket->bytesAvailable()<tailleMessage)//Si on n'a pas encore tout reçu, on arrête la méthode
    13. return;
    14.  
    15. //Si on arrive jusqu'à cette ligne, on peut récupérer le message entier
    16. QString messageRecu;
    17.  
    18. in>>messageRecu;
    19.  
    20. QPixmap pixmap("puzzle.png");
    21.  
    22. QImage image(pixmap.toImage());
    23.  
    24.  
    25. //On affiche le message sur la zone de Chat
    26.  
    27. QTextCursor textCursor=textEditListeMessages->textCursor();
    28. textEditListeMessages->append("");//Pour le saut de ligne
    29.  
    30. textCursor.insertImage(image);//Colle l'avatar approprié
    31.  
    32. textCursor.insertHtml(messageRecu);
    33.  
    34.  
    35. QPixmap pixmapbis;
    36. QByteArray bytes;
    37. QBuffer buffer(&bytes);
    38. buffer.open(QIODevice::WriteOnly);
    39. pixmapbis.save(&buffer, "PNG"); // writes pixmap into bytes in PNG format
    40.  
    41.  
    42. //On remet la taille du message à 0 pour pouvoir recevoir de futurs messages
    43. tailleMessage=0;
    To copy to clipboard, switch view to plain text mode 

    The point is that the tutorial (in openclassrooms) said that we had to use a QPixmap directly.
    Thanks for your answer anyway.


    Added after 6 minutes:


    Sorry the code doesn't include the following lines :

    QPixmap pixmapbis;
    QByteArray bytes;
    QBuffer buffer(&bytes);
    buffer.open(QIODevice::WriteOnly);
    pixmapbis.save(&buffer, "PNG"); // writes pixmap into bytes in PNG format
    So the correct code is :

    QDataStream in(socket);

    if (tailleMessage==0)//Si on ne connaît pas encore la taille du message, on essaie de la récupérer
    {
    if (socket->bytesAvailable()<(int)sizeof(quint16))//On n'a pas reçu la taille du message en entier
    return;

    in>>tailleMessage;//Si on a reçu la taille du message en entier, on la récupère
    }

    //Si on connaît la taille du message, on vérifie si on a reçu le message en entier
    if (socket->bytesAvailable()<tailleMessage)//Si on n'a pas encore tout reçu, on arrête la méthode
    return;

    //Si on arrive jusqu'à cette ligne, on peut récupérer le message entier
    QString messageRecu;

    in>>messageRecu;

    QPixmap pixmap("puzzle.png");

    QImage image(pixmap.toImage());


    //On affiche le message sur la zone de Chat

    QTextCursor textCursor=textEditListeMessages->textCursor();
    textEditListeMessages->append("");//Pour le saut de ligne

    textCursor.insertImage(image);//Colle l'avatar approprié

    textCursor.insertHtml(messageRecu);


    //On remet la taille du message à 0 pour pouvoir recevoir de futurs messages
    tailleMessage=0;
    Last edited by sgu35; 25th April 2015 at 10:05.

Similar Threads

  1. QTcpSocket Chat : Mutiple Client using FD_SET and select()
    By cooler123 in forum Qt Programming
    Replies: 20
    Last Post: 15th January 2012, 19:57
  2. Examples for chat program, server-client?
    By Gokulnathvc in forum Newbie
    Replies: 1
    Last Post: 8th September 2011, 10:53
  3. Network Chat Client in a multihomed environment.
    By drescherjm in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2010, 17:28
  4. Replies: 1
    Last Post: 5th October 2010, 16:54
  5. Chat client!!!!
    By galleeg in forum Qt Tools
    Replies: 1
    Last Post: 16th May 2006, 09:05

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.