Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Difficulty in getting value
Forum Updated to NodeBB v4.3 + New Features

Difficulty in getting value

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MrErfanM Offline
    MrErfanM Offline
    MrErfan
    wrote on last edited by MrErfan
    #1

    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

    1 Reply Last reply
    0
    • MrErfanM Offline
      MrErfanM Offline
      MrErfan
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • MrErfanM Offline
          MrErfanM Offline
          MrErfan
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • MrErfanM Offline
            MrErfanM Offline
            MrErfan
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • MrErfanM Offline
              MrErfanM Offline
              MrErfan
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                MrErfanM 1 Reply Last reply
                0
                • jsulmJ jsulm

                  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.

                  MrErfanM Offline
                  MrErfanM Offline
                  MrErfan
                  wrote on last edited by MrErfan
                  #8

                  @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 ?

                  1 Reply Last reply
                  0
                  • jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved