How to create session and call it in QT

  • Hi, please how can I create a session and call it in QT!!

  • Moderators

    Hi jiji,

    Could you be more specific about your question?
    What kind of session are you talking about?


  • I want to store my user credentials, so I got to use a session

  • Moderators

    Is it to access a database, a network, your own app that you want to protect?

    Please tell us more about your use case.

  • In my application I have to manage user , so every time after authenticating I want to store the user details in a place that is easy to access.

  • Lifetime Qt Champion


    So... Do you have any central database to store these details ?
    Should it be only locally ?

    You have to give details about your application.

  • Hi,
    I store my data base in a mongoDB server, and I want to store user details locally, because when I close the application the user disconnect, but I want it remain connected.

  • Moderators


    This topic might have links to sample code you need.

    You could also search for other posts of course.

  • I managed to connect mongodb with QT and I recovered the data but the problem is when I closed the application the user will disconnect by default and when I open the application always the authentication interface is displayed.
    So I want to save the user data locally from mongoDB so that it remains logged in

  • Moderators

    You could store the user data in Qsettings. When opening the db check if there is a username filled in already.
    If it is don't show the authentification display but use the credentials from Qsettings.

  • Lifetime Qt Champion

    @Eddy I'd avoid plain QSettings for that, the credentials would be stored in plain text. Credentials are sensitive information that should be encrypted.

  • Moderators

    @SGaist Yes, you're right, my bad.

    It depends how strict the teacher will be for this school assignment ;-)

  • In the main method I use : engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); to display the authentification. So where I should put Qsettings!!??

  • Moderators

    In the main method I use : engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));

    Thanks for letting us know you are using QML...

    Have a look at this link if you are not concerned with encryption yet.

  • I want to create session because I have list that I declare in the main using context property as following:
    int main(int argc, char argv[])
    qputenv("QT_QUICK_CONTROLS_STYLE", "material");
    QGuiApplication app(argc, argv);
    ApplicationUI appui;
    QQmlApplicationEngine engine;
    context = engine.rootContext();
    context->setContextProperty("myApp", &appui);

    //Users list
    QList<User*> listuser;
    listuser=u.getallUser() ;
    UserEntityModel modeluser;
    for(int i =0;i<listuser.size();i++ )
    QString fullname = listuser[i]->fullname();
    QString email= listuser[i]->Email();
    QString grade= listuser[i]->grade();
    QString evaluation = listuser[i]->evaluation();
    QString photo=listuser[i]->photo();
    context->setContextProperty("userModel", &modeluser);
    return app.exec();
    For now I want to display the lists according to the logged user saved in session
    But I can't refresh the list declared in the main, and it always shows me the lists of the last connection .
    So I want to know how can refresh a list in real time.

  • Lifetime Qt Champion

    You should implement a class that does proper user management and updates the model as needed.

  • @SGaist please can you explain to me better, I didn't understand what I should to do!!!
    And in QML I use the list model as following:
    ListView {
    id: listViewuser
    anchors.bottomMargin: -3
    anchors.leftMargin: -18
    anchors.topMargin: -3
    anchors.rightMargin: 5
    focus: true
    clip: true
    anchors.fill: parent
    delegate: userComponent "sortGroup"
    section.criteria: ViewSection.FullString
    section.delegate: stateChanged

  • Lifetime Qt Champion

    Since it seems that your user may change while your application is running, you should have a class which goal is to handle user management and part of its job would be to update that list.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.