There are variations on how you can do this, but the basics are:

1. define database
2. define query

Sqlite example:

Qt Code:
  1. // define database
  2. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", <yourConnectionName>);
  3. db.setDatabaseName(dbName + ".sqlite");
  4. db.open();
  5.  
  6. // define query
  7. QSqlQuery query(db); // db ties the query to the defined, open database
  8. query.exec("SELECT * from yourTable");
To copy to clipboard, switch view to plain text mode 

If you split this code into two pieces then you can use the connectionName and don't have to re-define the database for each query.