Using config files to load user name ,hostname, database name and port in qt c++
-
wrote on 1 Oct 2017, 06:14 last edited by
In my Qt application I provide a login by allowing the user to enter username,pasword, hostname , databasename and port! The login has been made successfully and the the part of the application is currently being developed but in order to test the functionality of currently developing part of the application I always have to enter the login details!
Is there any way to load them through a config file each time I run the Qt application?
-
In my Qt application I provide a login by allowing the user to enter username,pasword, hostname , databasename and port! The login has been made successfully and the the part of the application is currently being developed but in order to test the functionality of currently developing part of the application I always have to enter the login details!
Is there any way to load them through a config file each time I run the Qt application?
wrote on 1 Oct 2017, 06:21 last edited by ambershark 10 Jan 2017, 06:21@Lasith Sure many ways... easiest is probably just a plain text file, but you could use JSON, xml, whatever.
Keep in mind unless you encrypt it the details would be insecure and easily viewed. If it's just for development no big deal though.
Some example code:
QFile f("myconfigfile"); if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) return; // fail QTextStream fs(&f); // assumes 1 field per line and minimum of 4 fields QString host = fs.readLine(); QString database = fs.readLine(); QString user = fs.readLine(); QString pass = fs.readLine();
Edit: your
myconfigfile
would look like this:myhost mydb myuser mypass
-
@Lasith Sure many ways... easiest is probably just a plain text file, but you could use JSON, xml, whatever.
Keep in mind unless you encrypt it the details would be insecure and easily viewed. If it's just for development no big deal though.
Some example code:
QFile f("myconfigfile"); if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) return; // fail QTextStream fs(&f); // assumes 1 field per line and minimum of 4 fields QString host = fs.readLine(); QString database = fs.readLine(); QString user = fs.readLine(); QString pass = fs.readLine();
Edit: your
myconfigfile
would look like this:myhost mydb myuser mypass
wrote on 1 Oct 2017, 06:44 last edited byThis post is deleted!
2/3