I'm just trying to learn how to connect to a SQL database from C++. I'm not actually getting errors at this team. Just when I run a boolean to test my connection it always come back false. I'm going to put the code I'm using below. I'm assuming there is an error in the in how I'm inputting the DB information.
#include "yslwidget.h"
#include <QApplication>
#include <QLocale>
#include <QTranslator>
#include <QSqlDatabase>
#include <iostream>
int main(int argc, char *argv[])
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("LAPTOP-BEKKETLP");
db.setDatabaseName("YSLDB");
db.setUserName("User");
db.setPassword("password");
bool ok = db.open();
qDebug()<<ok;
}
Related
I save data with sqlite,and one of the columns is Chinese text.I use QSqlTableModel+QTableView.But When I excute "Model->setSort(0, Qt::AscendingOrder)" and the data of column 0 is Chinese,it doesn't sort correctly.I know this may be a coding problem, but I don't know how to modify SQLite's coding,
Not a coding problem.
QSortFilterProxyModel can be used.
Below is the sample code:
#include <QApplication>
#include <QSortFilterProxyModel>
#include <QTableView>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQueryModel>
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QString dbPath = QCoreApplication::applicationDirPath() + "/test.db";
db.setDatabaseName(dbPath);
db.open();
QSqlQueryModel* model = new QSqlQueryModel();
model->setQuery("select * from test");
QSortFilterProxyModel* sort = new QSortFilterProxyModel();
sort->setSortLocaleAware(true);
sort->setSourceModel(model);
QTableView table;
table.setModel(sort);
table.setSortingEnabled(true);
table.show();
return a.exec();
}
I am trying to get a QGeoLocation. My version of Qt is 5.7.1, btw, and I am running it on Debian.
I saw this post how to get latitude/longitude from one geo address using Qt c++ on windows?
I copied and pasted the working solution from Scheff's answer, but still got not error and 0 locations. Does this have to do with my setup/environment?
This shorter code has the same effect:
#include <QApplication>
#include <QGeoAddress>
#include <QGeoCodingManager>
#include <QGeoCoordinate>
#include <QGeoLocation>
#include <QGeoServiceProvider>
#include <QtDebug>
int main( int argc, char **argv)
{
QCoreApplication app( argc, argv );
QGeoServiceProvider geoSrv( "osm" );
QGeoCodingManager *geoCoder = geoSrv.geocodingManager();
QGeoAddress addr;
addr.setCountry( "China" );
QGeoCodeReply *geoCode = geoCoder->geocode( addr );
if ( geoCode->error() )
qDebug() << "error";
qDebug() << geoCode->locations().length();
return app.exec();
}
I found your post while struggling with the same problem. For me the QGeoServiceProvider code suddenly stopped working with OpenStreetmap. I quickly tried out the "here" api and that seems to work with exactly the same code. With some quick inspection with wireshark I found the problem easily.
QGeoServiceProvider tries to connect to the OpenStreetMap api at this url: http://nominatim.openstreetmap.org where it gets a redirected via a HTTP 303 to https://nominatim.openstreetmap.org. Apparently, the QGeoServiceProvider isn't able to handle this redirection correctly. I fixed it by providing the new url in the osm.geocoding.host parameter. Using your code this would look like this:
#include <QApplication>
#include <QGeoAddress>
#include <QGeoCodingManager>
#include <QGeoCoordinate>
#include <QGeoLocation>
#include <QGeoServiceProvider>
#include <QtDebug>
int main( int argc, char **argv)
{
QCoreApplication app( argc, argv );
//Add this
QMap<QString,QVariant> params;
params["osm.geocoding.host"] = "https://nominatim.openstreetmap.org";
QGeoServiceProvider geoSrv( "osm", params );
QGeoCodingManager *geoCoder = geoSrv.geocodingManager();
QGeoAddress addr;
addr.setCountry( "China" );
QGeoCodeReply *geoCode = geoCoder->geocode( addr );
if ( geoCode->error() )
qDebug() << "error";
qDebug() << geoCode->locations().length();
return app.exec();
}
Hope this helps!
Instead of using
QGeoServiceProvider geoSrv( "osm", params );
QGeoCodingManager *geoCoder = geoSrv.geocodingManager();
If you use a pointer instead:
QGeoServiceProvider* geoSrv = new QGeoServiceProvider( "osm", params );
QGeoCodingManager *geoCoder = geoSrv->geocodingManager();
It should work(did for me at least)
I am trying to grab the names of webcams plugged into my computer and shove them into a combobox, then access the name later. Here is my code:
#include <QApplication>
#include <QComboBox>
#include <QCameraInfo>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication app{ argc, argv };
QComboBox combo;
QList<QCameraInfo> info = QCameraInfo::availableCameras();
foreach(QCameraInfo i, info)
combo.addItem(i.description());
combo.show();
std::cout << combo.currentText().toStdString() << std::endl;
return app.exec();
}
The code creates and shows a combo box that has the name of a webcam that I have plugged in to the computer. It will then toss me an access violation exception in trying to print the combo box string to the console.
If I comment the cout line out, all is well, but on exit I get a Debug Assertion Failed! message:
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
which I take to mean I am deleting an object that has been deleted (the QString in the combobox???).
If I change the code to fill the combo box with dummies:
#include <QApplication>
#include <QComboBox>
#include <QCameraInfo>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication app{ argc, argv };
for(int i=0; i<2; i++)
combo.addItem(QString("la la la");
combo.show();
std::cout << combo.currentText().toStdString() << std::endl;
return app.exec();
}
I get the same error on the cout, but if I comment that line out, the application exits correctly. I am using Visual Studio 2013, Windows 7, and Qt5.
Now it works. I kept the same source code, but completely scrapped the existing project and started a new one from scratch.
I've discovered that if I set the Runtime Library flag to Multi-Threaded DLL Debug, I will get access violation errors. If I set it to Multi-Threaded DLL, it is fine.
There may have been some other project settings that contributed, but this seems to be the main culprit.
I want use access database in my project, this is my code:
#include <QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QDebug>
#include <QStringList>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase m_db = QSqlDatabase::addDatabase("QODBC");
m_db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=c:\\db.mdb;");
bool ok = m_db.open();
if(ok)
qDebug()<<"ok";
else
qDebug()<<"not ok";
return a.exec();
}
When I run it it shows mt ‘not ok’ I think I use wrong connection because QODBC driver in available,
another question is where ‘db.mdb’ file must be located? in debug folder or it must be attached to the project and how the connection string should change ?
Usually you don't need to install anything regarding drivers If you need to connect to an MS Access database with Qt. But if you don't have the necessary driver you should build it on your own.
You can connect to and MS Access database with something like this :
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\path\\to\\myDB.mdb");
if(db.open())
qDebug() << "oK";
else
qDebug() << db.lastError().text();
I am updating the printer list using availablePrinters(). But it fails to list the new printer added while running application. It is working fine with Qt 4.
The code can be seen below:
#include <QCoreApplication>
#include <QtPrintSupport/QPrinterInfo>
#include <QThread>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while (1) {
QThread::msleep(3000);
qDebug()<<"List of printers";
QList<QPrinterInfo> printerList=QPrinterInfo::availablePrinters();
foreach (QPrinterInfo printerInfo, printerList) {
qDebug()<<printerInfo.printerName();
}
}
return a.exec();
}
That was a bug with the existing Qt version, and It got fixed on the next version