Search code examples
oracleqtqt4oracle-call-interface

Convert string input of linedit() to int for use as an integer variable


I designed a simple GUI to check Database connectivity. The DB connection parameters such as DB name, Host name, user name, password, port etc will be entered from GUI and the output will be a RED or GREEN image depending upon the connectivity.

I have set up oracle OCI plugin (DB is oracle 10g)

and done the following--

void MainWindow::on_GoButton_clicked()
{
    QString HostN = ui->HostNameEdit->text();
    QString DatabaseN = ui->DatabaseNameEdit->text();
    QString UserN = ui->UserNameEdit->text();
    QString PassWD = ui->PasswordEdit->text();
    QString PortNO = ui->PortEdit->text();

QSqlDatabase db = QSqlDatabase::addDatabase("QOCI");

db.setHostName(HostN);
db.setDatabaseName(DatabaseN);
db.setUserName(UserN);
db.setPassword(PassWD);
db.setPort(PortNO);

while(true)
{
    if (db.open())
    {
        // do this
    }
    else
    {
        //do that
    }
}

}

Now it is showing error-- /home/aj/MY_QT_WORK/DB_connection_test/mainwindow.cpp:19: error: no matching function for call to ‘QSqlDatabase::setPort(QString&)’

Any ideas ???


Solution

  • You could write it as:

    db.setPort(PortNO.toInt());
    

    However for much correctness you have to be sure that the PortNO string is really convertible to an integer value. Therefore you can use a flag that will indicate the successful conversion:

    bool ok;
    int portNumber = PortNO.toInt(&ok);
    
    if (!ok) {
        qDebug() << "The port number is incorrect";
        // return?
    }