Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Serialize QPixmap in console app



  • Hello,

    I am trying to serialize a QPixmap between a server and a client app. The server is a console app and the client a gui one. The server always crash when I serialize the pixmap while the client stay alive.. so I'm guessing it has something to do with being a console app.. do I need to enable something in the project file ? I have already added += gui to use QPixmap ...



  • Can you show us your code?



  • @VRonin I'm afraid it is way way way to big to post



  • Just the part where you serialise



  • @VRonin

    void Server::sendProfilePicture(QPixmap pProfilePicture, const QString & pPseudo)
    {
        QByteArray paquet;
        QDataStream out(&paquet, QIODevice::WriteOnly);
        out << (quint64) 0;
    
        Message message(PROFILEPICTURE);
        message.setOption(0);
        message.setProfilePicture(pProfilePicture);
    
        out << message;
        out.device()->seek(0);
        out << (quint64) (paquet.size() - sizeof(quint64));
        if (socketFromPseudo(pPseudo) != NULL) {
            socketFromPseudo(pPseudo)->write(paquet);
        }
    }
    

    This will probably be Chinese without the rest of the code but this is where the crash happens...


  • Moderators

    @Misty-River
    you need to create a QGuiApplication (instead of a QCoreApplication) in your server application.



  • QPixmap need to create in gui thread. I have had same problem with chat server application. You need to use QImage.
    From qdoc:

    Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. Finally, the QPicture class is a paint device that records and replays QPainter commands.

    So using QImage in server app is better than QPixmap.



  • @Hamed.Masafi Ok thank you. I have switched to QImage and it stopped crashing.


Log in to reply