Using config files to load user name ,hostname, database name and port in qt c++
Solved
General and Desktop
-
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?
-
@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
-
This post is deleted!