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

Serializing a widget



  • Hey, everybody,
    As part of my chatt software, I will know how whatsapp(on PC) does to play back the message as soon as the client is online?
    here's a picture:
    0_1534333365390_Capturew.PNG

    so I thought about serializing my QWidget (which is used to display conversations between customers) in a database.

    here are my questions:

    1. Is it possible to serialize a QWidget and its children in a database?
    2. if that is possible how can I do it?

    Thanks in advance for your help


  • Qt Champions 2017

    You can take all the properties of widget and save them. You can use QSettings for the same as well. There is nothing like serialising the Widget.



  • Thank you for your answer,

    But where can you get these properties? and how can you save them in a database?

    I prefer to save the QWidget properties in a database



  • @EL-jos
    Up to you. But whatsapp is not going to be serializing widgets or even saving their properties. Don't know what you want to achieve exactly, but can't you just save whatever data you need to reconstruct a message (content, datetime), and resurrect from that?



  • @JonB said in Serializing a widget:

    Up to you. But whatsapp is not going to be serializing widgets or even saving their properties. Don't know what you want to achieve exactly, but can't you just save whatever data you need to reconstruct a message (content, datetime), and resurrect from that?

    So if I understand very well you ask me to save just the elements I need such as(message, date and time,); but how to do for positioning?

    1. if you know better or a little bit about whatsapp restoration, please explain it to me .

    Thank you



  • @EL-jos
    OK, sorry, I know nothing about whatsapp. I'd be very surprised if it "serialized widgets", but that's all I can say.


  • Lifetime Qt Champion

    Hi
    For each message , date and time, you would save and index number telling
    where in conversation it comes and some sort of ID for the message .
    and most likely also a conversation id.

    So you store the messages in one table
    TABLE: messages
    msgID, message text,
    msgID, message text, etc
    and make a table to list
    conversation id, msgID
    In other table
    and most likely the persons also in other table with reference to conversations they
    have taken part in.

    But how are your data structured now ?



  • in fact for the data structure when I try to create a table and its values it does not work, here is my query:

        base = QSqlDatabase::addDatabase("QSQLITE");
        base.setDatabaseName("AMIS");
        if(base.open()){
    
            QSqlQuery query(base);
            qDebug() << query.exec("CREATE TABLE `test`.`amis` ( `id` INT NOT NULL AUTO_INCREMENT , `nom` VARCHAR(20) NOT NULL , `image` BLOB NOT NULL , PRIMARY KEY (`id`)) ");
        }
    

    I always have false as the answer to the console output so my table is not created,

    I even tried to remove the apostrophes on name, id and image but it still doesn't work.

    Anybody got any ideas?



  • Well, first I'd say:
    General advice is always use prepared statements.
    Prepare: https://stackoverflow.com/questions/5609245/qsqlquery-prepared-statements-proper-usage

    More general advice is figure out the actual query you sent the engine by firing a verbatim copy of the query generated in your application directly @ the engine without the application.

    So, how I see it:

    • Determine the actual statement
    • run it at the engine (using with command line or query / db engine tools and see the response - independent from your application anyhow).
    • Witness the engine response as a result of your statement (I'm expecting a syntax error as it seems you are too)
    • This will lead you to the reasons what is not being correctly done in your application.
    • Fix the application statement
    • Try again

    I suspect it would be a parsing error as you're playing with the single quotes but cannot say. I also won't troubleshoot what seems just syntax/parsing... that's for you to determine / observe.



  • Hey, family,

    After a lot of testing, I understood that my database and my amis table are created but except that the amis table contains no columns so no fields.
    I tried to add the plugin qsqlited.dll and qsqlmysqld.dll since I compiled in debug mode but my amis table is still empty so no amis.
    Here's my code:

     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("base_de_donnees2.db");
        db.open();
        if(!db.isOpen())
            qDebug() << "ERREUR d'ouverture de la base de données; cause: " << db.lastError().text();
        QSqlQuery q;
        q.exec("CREATE TABLE IF NOT EXISTS amis (id INTEGER PRIMARY KEY AUTOINCREMENT, nom TEXT NOT NULL, photo BLOB NOT NULL) ");
    
        QSqlQuery q2;
        q2.prepare("INSERT INTO amis (id,nom,photo) VALUES (:id,:nom,:photo) ");
    
        int identifiant = 1;
        QString pseudo("EL-chrino");
        QFile fichier(QFileDialog::getOpenFileName());
        fichier.open(QIODevice::ReadOnly);
        QByteArray byte;
        if(!fichier.isOpen())
            qDebug() << "Echec d'ouverture du fichier";
        byte = fichier.readAll();
    
        q2.bindValue(":id",identifiant);
        q2.bindValue(":nom",pseudo);
        q2.bindValue(":photo",byte);
    
        if(!q2.exec())
            qDebug() << "ERREUR lors de l'exécution de la requête; cause: " << q2.lastError().text();
    
    

    Note that I have never used databases with Qt so I don't know how it works, please tell me what I need to do to use databases with Qt as it will be very important for my chatt software.
    Thank you in advance



  • I found in a forum, they say you need to recompile Qt and SQL Drive but it doesn't explain how to do it.

    Please if there is someone who knows how to make it watch you manifest


  • Lifetime Qt Champion

    @EL-jos
    Hi
    For QSQLITE
    you need to compile nothing or add any libraries.
    just
    QT += sql
    in your .pro file



  • Thank you very much for your advice, but I have two questions;
    first how to update a QscrollArea that already contains elements beforehand?
    and the second is how to make a message waiting list like this if a client "A" has just logged out and another client "B" sends him a message, that the message is in a list to wait for the client "A" to read it.
    like whatsapp or facebook notification when someone sends us a message while we're away, the message is kept until we read it?


  • Lifetime Qt Champion

    Hi,

    You just append new widgets to the layout that already contains the others.

    As for the messages, if you are thinking on how it’s working on mobile platform, you’ll have several things to take into consideration.

    So some questions for you:

    • On what platform(s) is your application going to run ?
    • How are the client going to talk to each other ?


  • Regarding the update of my scroollArea, I don't want to add elements but rather delete completely what it already contains.

    then for the messages I want my application to run all over the platform but first on Pc using the windows operating system.

    for my clients, I use TCP/ip protocol so with QTcpSocket class


  • Lifetime Qt Champion

    The most elegant would likely be to have the messages stored in a model and then have a custom QStyledItemDelegate to paint whatever you want.

    Again: how are you client talking to each other ? Through a central server ? How do they know each other ?



  • Hi,

    Yes I use a server for the dialog between client, this means that if a client'A' sends a message to client'B', it will first go through the server then the server will send me to the destination so client'B'.



  • please someone has an idea.


  • Lifetime Qt Champion

    @EL-jos
    About ?



  • In fact I want to make sure that if we send a message to a client that is not connected, that this client sees the message that has been sent as soon as it is connected.


  • Moderators

    @EL-jos said in Serializing a widget:

    I want to make sure that if we send a message to a client that is not connected, that this client sees the message that has been sent as soon as it is connected.

    Then the central server must store the message. When the client comes online, the server can deliver the message to the client.

    Note: This concept is about communications architecture. This is not related to widgets at all.



  • ahhhhh yes you're right, so I can store the message in a database at the server level then as soon as the concerned client is connected the server will be able to restore the message then send it to the client, thank you very much.

    then a second unresolved problem so far;
    here I have a scrollArea which first contains many widgets of different type, so I want to delete either empty all these widgets and left the scrollArea blank so without element,
    how do I do that?


  • Moderators

    @EL-jos said in Serializing a widget:

    here I have a scrollArea which first contains many widgets of different type, so I want to delete either empty all these widgets and left the scrollArea blank so without element,
    how do I do that?

    Have a read through http://doc.qt.io/qt-5/qscrollarea.html . Do you see anything that might do what you want?



  • I just read and reread the Qt documentation for the QScrollArea class or even its mother classes but I can't find the solution to my problem because there is not a function that allows to remove all widgets contained in a scrollAreea


  • Lifetime Qt Champion

    @EL-jos
    Hi
    Are the widgets not in a layout ?
    You can clear layout with

    void clearLayout(QLayout *layout)
        QLayoutItem *item;
        while((item = layout->takeAt(0))) {
            if (item->layout()) {
                clearLayout(item->layout());
                delete item->layout();
            }
            if (item->widget()) {
                delete item->widget();
            }
            delete item;
        }
    }
    


  • Thank you for your code.
    theoretically it works but not in practice according to my example, here is a little project I created to test your code:

    [7_1535474695539_fenetre1.cpp](Uploading 100%) [6_1535474695538_fenetre1.h](Uploading 100%) [5_1535474695538_fenetresecond.cpp](Uploading 100%) [4_1535474695537_fenetresecond.h](Uploading 100%) [3_1535474695537_fenetresecond.ui](Uploading 100%) [2_1535474695537_main.cpp](Uploading 100%) [1_1535474695535_Oc.pro](Uploading 100%) [0_1535474695483_Oc.pro.user](Uploading 100%)



  • sorry I can't send my project on topic, send me your E-mail address so I can send it to you by E-mail


  • Lifetime Qt Champion

    @EL-jos
    Sorry i dont have email i use for Qt forum.
    You can use dropbox or google drive.
    Are you sure u give it the correct layout ?
    Im sure code works so maybe its other layout u clear that u think.
    also use the debugger to see what happens.



  • Yes thank you sorry it was a small mistake on my part and there it works correctly, here is the code:

    void Controleur::on_pushButton_clicked()
    {
        QLayoutItem *item;
    
        int nb = 3;
        int a = 0;
        qDebug() << nb;
    
        while(ui->scrollAreaWidgetContents->layout()->count() > 0){
    
        for(int i = 0; i < ui->scrollAreaWidgetContents->layout()->count(); ++i){
    
            item = ui->scrollAreaWidgetContents->layout()->takeAt(i);
            if(item->layout()){
    
                qDebug("It's a layout");
                delete item->layout();
            }
    
            if(item->widget()){
    
                qDebug("It's a widget");
                delete item->widget();
            }
    
            delete item;
        }
    
        qDebug() << "suppression N°" << a;
        ++a;
        }
    }
    

  • Lifetime Qt Champion

    @EL-jos
    Hi
    Ok super.
    You also want to remove any layout it has ?
    Normally one wish to keep layout and only remove widgets.



  • Hi,
    I'm sorry I'm not here;
    Yes, I just wanted to remove the widgets, not the layout.

    I take this opportunity to ask a question I don't understand.
    here is the server and client code:

    • Server
         QTcpServer *serveur = new QTcpServer(this);
        iserveur->listen(QHostAddress::Any, 50885);
    
    • client
    QTcpSocket *socket  = new QTcpSocket(this); 
    socket->abort();
        socket->connectToHost("127.0.0.1", 50885);
    

    so we understand that my server accepts any type of address, such as: Local IP, Internal IP and Internet IP.
    but strangely as soon as I execute my code, the server accepts that the local IP: 127.0.0.1 and the internal IP: 192.168.X.X, as far as the internet IP is concerned this does not pass and there is a message telling me time is out.

    do you know how I can connect to the server via Internet IP?


  • Lifetime Qt Champion

    Check your network and firewall settings.



  • so since I'm on windows 10 how should my firewall be?


  • Lifetime Qt Champion


  • Moderators

    @EL-jos said in Serializing a widget:

    the server accepts that the local IP: 127.0.0.1 and the internal IP: 192.168.X.X, as far as the internet IP is concerned this does not pass and there is a message telling me time is out.

    That's because:

    • When your client tries to connect to a local/internal IP address, the connection stays within the same PC.
    • When your client tries to connect to an "Internet IP" address, the connection reaches your server's Router. You must tell your Router to forward the connection to your Server PC.

    If you want to create an application that communicates over the Internet, you must first understand the topic of Port Forwarding (e.g. https://en.wikipedia.org/wiki/Port_forwarding ). And like @SGaist said, you must configure your Server's firewall to accept the connection.

    Note: Port Forwarding and Firewall configuration are topics outside the scope of Qt. Please do your homework and research these topics online.



  • Yes you are right but except I just allowed my application to communicate through the windows firewall (public and private) but the client still can't connect to the server via ip/internet.

    Please help me.

    here is my example code:

    
    Controleur::Controleur(QWidget *parent) :
        QDialog(parent)
    {
        setupUi(this);
    
        serveur = new QTcpServer(this);
        QObject::connect(serveur,SIGNAL(newConnection()),this,SLOT(on_newConnection()));
    
        client = new QTcpSocket(this);
        QObject::connect(client,SIGNAL(connected()),this,SLOT(on_connected()));
        QObject::connect(client,SIGNAL(stateChanged(QAbstractSocket::SocketState socketState)),this,SLOT(on_stateChanged(QAbstractSocket::SocketState socketState)));
        QObject::connect(client,SIGNAL(error(QAbstractSocket::SocketError socketError)),this,SLOT(on_error(QAbstractSocket::SocketError socketError)));
    }
    
    
    void Controleur::on_newConnection(){
    
        qDebug("New connection");
    }
    
    void Controleur::on_pushButton_clicked()
    {
        // dem. serveur
    
        serveur->listen(QHostAddress::AnyIPv4,40885);
    
        if(serveur->isListening())
            qDebug("The server is ready for listening");
        if(!serveur->isListening())
            qDebug("The server is not listening");
    
    }
    
    void Controleur::on_pushButton_2_clicked()
    {
        // con. client
    
        client->abort();
        client->connectToHost("196.92.6.87",50885);
        qDebug("Attempt to connect to the server...");
    
    }
    
    

  • Moderators

    @EL-jos said in Serializing a widget:

    I just allowed my application to communicate through the windows firewall (public and private) but the client still can't connect to the server via ip/internet.

    Read my previous post again about Routers and Port Forwarding. The problem is not in your code.


Log in to reply