Getting QLineedit values to qt header files
-
I want to create a database connection method named connectionOpen in my header file inorder to connect to a database when user enters user name, password, hostname and port number! I can create the method by assigning values manually but when I try to give from the UI inputs it is impossible!
The followin code works!public: QSqlDatabase mydb;
bool connectionOpen(){
mydb.setDatabaseName("XE");
mydb.setHostName(123.145.123.456);
mydb.setUserName("uname");
mydb.setPassword("21213");
mydb.setPort(1521);
}But the following code returns errors!
public: QSqlDatabase mydb;
bool connectionOpen(){
mydb.setDatabaseName(ui->lineEdit->setText());
mydb.setHostName(ui->lineEdit2->setText());
mydb.setUserName(ui->lineEdit3->setText());
mydb.setPassword(ui->lineEdit4->setText());
mydb.setPort(ui->lineEdit5->setText());
}How can I correct this?
-
@Lasith ,
QLineEdit is having returnpressed() signal,
connect(ui->lineEdit,SIGNAL(returnPressed()),this,SLOT(catchString()));
void Widget::catchString() {
QString name = ui->lineEdit->text();
mydb.setDatabaseName(name);
} -
@Vinod-Kuntoji Thanx alot! for the 5 string inputs do we need to have connect methods?
-
@Lasith ,
Yes. -
@Vinod-Kuntoji Do I have to place the code you put in the header file or the cpp?
-
Hi,
Your code won't work, your QSqlDatabase object is invalid.
Please take the time to look at the code example in QSqlDatabase's documentation as well as the example provided.
It' also discouraged to store database object like that locally. QSqlDatabase allows for easy connection handling without the need for these local copies.