Getting QLineedit values to qt header files
-
wrote on 12 Sept 2017, 17:37 last edited by Lasith 9 Dec 2017, 17:41
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?
-
wrote on 12 Sept 2017, 17:52 last edited by
@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);
} -
@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);
}wrote on 12 Sept 2017, 18:12 last edited by@Vinod-Kuntoji Thanx alot! for the 5 string inputs do we need to have connect methods?
-
wrote on 12 Sept 2017, 18:14 last edited by
@Lasith ,
Yes. -
@Lasith ,
Yes.wrote on 12 Sept 2017, 18:16 last edited by@Vinod-Kuntoji Do I have to place the code you put in the header file or the cpp?
-
@Vinod-Kuntoji Do I have to place the code you put in the header file or the cpp?
wrote on 12 Sept 2017, 18:20 last edited byIn cpp file
-
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.
1/7