Difficulty in getting value



  • Hey guys ,
    Im created a login form for the user to login to the program that contains the following code,

    //MyClass.h
    class MyClass : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString loginsts READ loginsts WRITE setloginsts NOTIFY loginstsChanged)
    
    public :
        MyClass();
        ~MyClass();
    
    
    public slots :
        QString loginsts() const{
            return m_loginsts;
        }
        void run2();
        void onfinished();
        void login(QString usr,QString psw);
        void connecttodb();
        void setloginsts(QString loginsts)
        {
            if (m_loginsts == loginsts)
                return;
    
            m_loginsts = loginsts;
            emit loginstsChanged(loginsts);
        }
    
    signals :
        void started();
        void finished();
        void loginstsChanged(QString loginsts);
    private :
        QString m_loginsts;
    
    };
    #endif // MYCLASS_H
    
    
    //MyClass.cpp
    MyClass::MyClass()
    {
        QThread *thread = new QThread( );
        this->moveToThread(thread);
        connect( thread, SIGNAL(started()), this, SLOT(onfinished()) );
        thread->start();
    }
    
    MyClass::~MyClass()
    {
    }
    
    void MyClass::run2()
    {
        QThread *thread = new QThread( );
        this->moveToThread(thread);
        connect( thread, SIGNAL(started()), this, SLOT(onfinished()) );
        thread->start();
    }
    
    void MyClass::onfinished()
    {
    ..........
    }
    
    void MyClass::login(QString usr2,QString psw2)
    {
        emit started();
            QSqlQuery qry;
            if(......))
            {
                int c=0;
                while (qry.next())
                {
                    c++;
                }
                if(c >= 1)
                {
                    setloginsts("true");
                }
                else if(c < 1)
                {
                    setloginsts("false");
                }
            }
            else
            {
    		.......................
            }
    }
    
    void MyClass::connecttodb()
    {
    .......
    }
    
    //qml button click
            onClicked: {
                loginsts = MyClass.loginsts
                MyClass.run2()
                lbl.text = MyClass.loginsts
    		//After double-clicking the correct amount is variable
                if(loginsts == "true")
                {
                    anim1.start()
                }
    

    Hong apps moment because I have to use the thread, now my problem is that I was wrong if a user name and password to change the status variable value, I now have to double click the button to change the variable, What is wrong ?
    Thank



  • Link Vedio Please watch the video and tell us what's the problem?


  • Lifetime Qt Champion

    Hi,

    Why are you trying to use a thread for that part ?



  • Hello ,
    Because if I do not use the thread, then click the program to be paused for a few seconds, if you do not use thread hangs just a few seconds program would not create a problem getting the value of variables, please see this video



  • This post is deleted!


  • A second problem was solved using Qmutex :(( But now the problem is back, please do guide me


  • Moderators

    Why do you start two threads? One is in the constructor another one in run2().
    Where is login() called?
    Why do you emit finished() when the thread is started? What is the thread supposed to execute?
    If you really need to execute login() in a thread use QtConcurrent::run().
    Sorry, but the code is really a mess, so it is hard to understand what you're trying to do.



  • @jsulm
    I can login to the place I run, but due freezing moment does this have to do with thread , To run the login() function thread What should I do ?


  • Moderators

    You can use http://doc.qt.io/qt-5.5/qtconcurrentrun.html to call login() in run2():
    QtConcurrent::run(login, str1, str2);


Log in to reply
 

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