Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Accessing objects into another class
Forum Updated to NodeBB v4.3 + New Features

Accessing objects into another class

Scheduled Pinned Locked Moved Language Bindings
6 Posts 2 Posters 2.2k 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.
  • S Offline
    S Offline
    smannari
    wrote on last edited by
    #1

    Hello ,

    I am new to QT. And started developing very recently. I am stuck with this error. I am creating a Login page using SQLite.

    1. I created a DB using SQLite and using the using the credentials from there , I successfully creted a login page with all checks (mathing uname and pword or wrong etc.) (Login.cpp)

    2. I now want to create a second dialog box which assists the user in changing the password for the user who entered his/her credentials on the first page (ChangePword.cpp)

    3. I created an object of Login.cpp in the ChangePword.h file . So that I can check against the credentials entered in the first page by the user.
      I am not sure if this is the right thing to do, the code does not work and causes an abrupt end to the program

    Can anybody help me with this

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

      Hi and welcome to devnet,

      What abrupt end do you have ?

      Did you run your software through the debugger ?

      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
      • S Offline
        S Offline
        smannari
        wrote on last edited by
        #3

        Hello SGaist,
        Thanks for the response:

        I have 2 files one loginfromdb.c and another dialogpword.c.

        The loginfromdb is the main window that asks for user login and password and all worked correctly. I have declared the variables uname and pword in the public section LoginfromDB class

        @
        LoginfromDB::LoginfromDB(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::LoginfromDB)
        {
        ui->setupUi(this);

        myDB = QSqlDatabase::addDatabase("QSQLITE");
        myDB.setDatabaseName("C:/SQLite/EmployeeInfo.sqlite");
        
        if(!myDB.open())
            ui->label->setText("Failed connection to the DataBase");
        else
            ui->label->setText("Connected.........");
        

        }

        LoginfromDB::~LoginfromDB()
        {
        delete ui;
        }

        void LoginfromDB::on_pushButton_clicked()
        {
        // QString uname, pword;
        uname = ui->lineEdit_uname->text();
        pword = ui->lineEdit_pword->text();

        QMessageBox::StandardButton reply;
        DialogPword mdialog;
        
        if(!myDB.isOpen())
        {
            qDebug() << "Failed to open DataBase";
            return;
        }
        
        QSqlQuery qry;
        if(qry.exec&#40;"select * from employee where Username = '"+uname+"' and Password = '"+pword+"'"&#41;&#41;
        {
            int count = 0;
            while(qry.next())
            {
                count++;
            }
            if(count == 1)
            {
                ui->label->setText("Username and Password is correct");
        
                reply = QMessageBox::question(this,"Ask","Do you want to change your password ?",QMessageBox::Yes | QMessageBox::No);
        
                if(QMessageBox::Yes == reply)
                {
                    mdialog.setModal(true);
                    mdialog.exec&#40;&#41;;
        
                }
                else
                    QMessageBox::information(this,"Close","No Problem"&#41;;
        
            }
            if(count > 1)
                ui->label->setText("Duplicate Username and Password");
            if(count == 0)
                ui->label->setText("Username and Password is incorrect");
        
        
        }
        

        }
        @

        As a second step I added another page (dialog) so as to ask user to change password.

        I declared a object of the LoginfromDb class in the private section of the file dialogpword.h

        LoginfromDB loginDB;

        I have commented the rest of the code in dialogpword.c (changing passwords etc)

        The program compiles fine, but when I enter the details of Username and password in the first window and click login I get this error

        QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
        QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
        Failed to open DataBase

        and the a window appears with this mesage

        A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available

        [edit: added missing coding tags @ SGaist]

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

          It should not crash for that (the run through the debugger should tell you where exactly it happened)

          For the connection error. You are recreating the database setup each time you create a LoginfromDB thus the error. The database initialization should only be done once e.g. in the main.cpp. Have a look at the SQL example to see how to use QSqlDatabase

          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
          • S Offline
            S Offline
            smannari
            wrote on last edited by
            #5

            Hello SGaist,

            I started all over again and it is working a bit better than last attempt.

            I have created a login page (Login.cpp) and a change password page (password.cpp) .

            The first login page works fine asking and verifying user credentials with SQLite data base. I have created the open and close functions for the data base in the public section of login.h, so that I can access the same in the password.cpp

            Now in the second password.h file I have created a Login login object , which gives this error ,
            'Login does not name a type' ,

            I have included the necessary header files in the .h files of each of these files. But still the error

            How to create an object of a class in another class?

            Can you please suggest something ?

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

              Just setup your database and open it once e.g. in your main function. There's no need to close/open it every time you want to access it unless you have a very good reason

              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

              • Login

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