Accessing objects into another class

  • 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

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What abrupt end do you have ?

    Did you run your software through the debugger ?

  • 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) :
    ui(new Ui::LoginfromDB)

    myDB = QSqlDatabase::addDatabase("QSQLITE");
        ui->label->setText("Failed connection to the DataBase");


    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;
        qDebug() << "Failed to open DataBase";
    QSqlQuery qry;
    if(qry.exec&#40;"select * from employee where Username = '"+uname+"' and Password = '"+pword+"'"&#41;&#41;
        int count = 0;
        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)
                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]

  • Lifetime Qt Champion

    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

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

  • Lifetime Qt Champion

    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

Log in to reply