Connecting to SQL Server with a QT application in windows - qt

I am trying to connect to SQL Server 2008R2 with my qt application in windows but I am getting errors. Here's what I am doing:
#include "ui_test1.h";
#include "QtSql/QtSql";
void Test1::on_btnsnd_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("ODBC");
db.setHostName("ITPL_PC1");
db.setDatabaseName("Test");
db.setUserName("sa");
db.setPassword("insforia");
db.open();
QSqlQueryModel *model = new QSqlQueryModel;
QString query = "insert into qttable(PID) values('ARUP')";
model->setQuery(query, db);
db.close();
}
i am getting this error 27 times :
test1.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase##QAE#XZ) referenced in function "private: void __thiscall Test1::on_btnsnd_clicked(void)" (?on_btnsnd_clicked#Test1##AAEXXZ)
I don't know how to do this (I found this in google.)
What should I do to fix it?

If you use MSVC, you should add %QTDIR%/lib/QtSql4.lib to Release configuration of your project and %QTDIR%/lib/QtSqld4.lib to Debug one. You should change 4 in file names to 500, if you use Qt 5.0. So, file names would be %QTDIR%/lib/QtSql500.lib and %QTDIR%/lib/QtSqld500.lib
If you use QtCreator you should add the next line into your .pro file
QT += sql
Upd: added description for QtCreator's .pro file

Related

How to reference a Qt Application?

In order to test some functions of a Qt application (named qtapp), I build the Qt application as lib/dll library. The ctor and dtor have been correctly exported in the library. However compiling the test project (a Qt console project named consoleTest) is always running to the following link errors:
Link:
1> Bibliothek "C:\Users\gmbh\test\x64\Debug\consoleTest.lib" und Objekt C:\Users\gmbh\test\x64\Debug\consoleTest.exp" werden erstellt.
1>moc_testFour.obj : error LNK2001: Nicht aufgelöstes externes Symbol ""public: static struct QMetaObject const qtapp::staticMetaObject" (?staticMetaObject#qtapp##2UQMetaObject##B)".
1>testFour.obj : error LNK2001: Nicht aufgelöstes externes Symbol ""public: static struct QMetaObject const qtapp::staticMetaObject" (?staticMetaObject#qtapp##2UQMetaObject##B)".
1>C:\Users\gmbh\test\x64\Debug\consoleTest.exe : fatal error LNK1120: 1 nicht aufgelöste Externe
The Qt application is quite simple:
#ifndef QTAPP_H
#define QTAPP_H
#include <QtWidgets/QMainWindow>
#include "ui_qtapp.h"
class __declspec(dllexport) qtapp : public QMainWindow
{
Q_OBJECT
public:
qtapp(QWidget* parent = 0);
~qtapp();
private:
Ui::qtappClass ui;
};
#endif // QTAPP_H
Implementation:
#include "qtapp.h"
__declspec(dllexport) qtapp::qtapp(QWidget* parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
__declspec(dllexport) qtapp::~qtapp()
{
}
If I simply include #include "qtapp.h" in the test project, the compiler yields the above error.
My questions:
What are the causes for the errors?
How to correctly deploy a Qt
application as lib?
My compiler is visual studio 2013, qt has the version of 5.4.2
That's actually a question independent of Qt. When linking an application against the library you need to have __declspec(dllimport) instead of __declspec(dllexport).
To achieve that, one usually uses defines that change when linking the library vs linking the application against the library. Microsoft has a help page on that "Importing into an Application Using __declspec(dllimport)".
The Qt documentation provides another one. The one thing where Qt can help, is replacing the __declspec by a cross-platform Q_DECL_IMPORT.

CMake: new subclass of dependency "unresolved external symbol"

I have a problem subclassing a VTK class, but I think the problem could be linked to any kind of library.
I did a vtkInteractorStyleImage subclass, just overriding a method:
#ifndef dcmInteractorStyleImage_h
#define dcmInteractorStyleImage_h
#include "vtkInteractionStyleModule.h" // For export macro
#include "vtkInteractorStyleImage.h"
class VTKINTERACTIONSTYLE_EXPORT dcmInteractorStyle : public vtkInteractorStyleImage
{
public:
static dcmInteractorStyle *New();
vtkTypeMacro(dcmInteractorStyle, vtkInteractorStyleImage);
virtual void OnLeftButtonDown();
void PrintSelf(ostream& os, vtkIndent indent);
};
#endif
The thing is I use it in a Qt class. So I include this in my CMakeLists.txt (simplified):
set(GENERIC_VIEW_CPP
View/UI/dcminteractorstyle.cpp # my new class
)
SET(GENERIC_VIEW_H
View/UI/dcminteractorstyle.h # my new class
)
add_library(generic ${GENERIC_VIEW_CPP} ${GENERIC_VIEW_H})
...
add_library(ui_qt ${UI_QT_CXX}
${UI_FORM_HEADERS} ${UI_RESOURCES_RCC}
${MODEL_WRAPPED_HEADERS}
${GENERIC_VIEW_CPP} ${GENERIC_VIEW_H} #necesary here?
)
qt5_use_modules(ui_qt Core Gui Widgets)
----------------------------------------------------------
# VTK
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
# ITK
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
else()
find_package(ItkVtkGlue REQUIRED)
include(${ItkVtkGlue_USE_FILE})
set(Glue ItkVtkGlue)
endif()
------------------------------------------------
set_source_files_properties(${UI_RESOURCES_RCC} PROPERTIES GENERATED ON)
add_executable(UtilidadDICOM WIN32 main.cpp ${UI_RESOURCES_RCC})
target_link_libraries(UtilidadDICOM
model
ui_qt
generic
${Glue}
${VTK_LIBRARIES}
${ITK_LIBRARIES}
)
The error trying to compile:
ui_qt.lib(viewerwidget.cpp.obj):-1: error: LNK2019: unresolved
external symbol "public: static class dcmInteractorStyle * __cdecl
dcmInteractorStyle::New(void)" (?New#dcmInteractorStyle##SAPAV1#XZ)
referenced in function "public: static class vtkSmartPointer __cdecl vtkSmartPointer::New(void)"
(?New#?$vtkSmartPointer#VdcmInteractorStyle####SA?AV1#XZ)
ui_qt is a library declared in CMake, as you can see above.
Any ideas? Thank you.
Short answer is that your class is not being linked. Try seeing the value in generic just before linking it to your executable. Do a message just before target_link_libraries:
MESSAGE(${generic})
and see if the generic.lib is actually being linked.

QLibrary does not load

I'm trying to write a simple example of loading a dll file, and the load fails.
I'm using qt 5.1.1 qt creator 3.0.1.
my compiler is: Microsoft Visual C++ Compiler 11.0 (amd64)
the pro file:
QT += core
TARGET = rrrr
SOURCES += main.cpp
the main file is:
#include <QLibrary>
int main()
{
bool is;
QLibrary lib("C:\\A.dll");
is = lib.load();
is = lib.isLoaded();
return 0;
}
The A.dll file of-course exists inside C.
I get the value false for the variable "is" while debbuging.

How to link to qaxserver.def entities properly

all
I'm trying to create ActiveX server with Qt5.2.0.
According to some pieces of information I have, to create a simple ActiveX server (which exports one object) it's enough to:
have such .pro file
TEMPLATE = lib
CONFIG += dll axserver
TARGET = simpleServer
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT += core
RC_FILE = qaxserver.rc
DEF_FILE = qaxserver.def
HEADERS += myserver.h
SOURCES += main.cpp
SOURCES += myserver.cpp
have such main.cpp file
#include "ActiveQt/QAxFactory"
#include "myserver.h"
QAXFACTORY_DEFAULT( MyServer, 5 id's )
the class should inherit QWidget and QAxBindable
there should be file qaxserver.rc. in my case it's content is 1 TYPELIB "simpleServer.pro"
there should be fie qaxserver.def with default contents:
; mfc_test.def : Declares the module parameters.
EXPORTS
DllCanUnloadNow PRIVATE
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
DumpIDL PRIVATE
the problem is that these 5 names are undefined for Visual Studio's linker.
How to solve the problem?
the error is:
error LNK2001: unresolved external symbol DllCanUnloadNow
need to add QT += axserver in .pro file

qt odbc plugin build unsuccessful

I developed an application along the lines of the SQL Browser example provided with QT in the Demos and Examples section. My development machine is Windows XP (visual studio compiler was used) and the application works well on it. It is able to connect to an external database (MySQL), and I am able browse through tables. I used the QODBC driver for connections. However when I deploy the executable (with all required .dll files) in another computer without QT, it says that I need to provide for the database drivers. I read the documentation and realized that I need to build a PlugIn for QODBC drivers.
First I looked at an example Plugin (Echo Plugin Example) given at http://doc.qt.digia.com/4.6/tools-echoplugin.html. Then I followed the instructions in http://doc.qt.digia.com/4.6/sql-driver.html#qodbc.
cd %QTDIR%\src\plugins\sqldrivers\odbc
qmake odbc.pro
nmake
The above commands built qsqlodbc4.dll. However, I am not successful in developing a Plugin for my application. Here are my steps, and the compilation output:
Created project odbcplugin.pro (see script below) under directory /odbcpluginTest
TEMPLATE = subdirs
SUBDIRS = sqlbrowser \
odbc
CONFIG += release
# install
target.path = $$PWD
sources.path = $$PWD
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS
INSTALLS += target sources
Created subdirectories: /odbc, /sqlbrowser, /plugins
Inside the directroy /odbcpluginTest /odbc/
(i). Copied odbc.pro and suitably modified the paths and file names (for example, a file originally named as main.cpp has been renamed as mainODBC.cpp to avoid confusing with the file named main.cpp inside /sqlbrowser). See the scripts below:
TEMPLATE = lib
INCLUDEPATH += ../sqlbrowser
SOURCES = mainODBC.cpp
TARGET = qsqlodbc
DESTDIR = ../plugins
CONFIG += release
include(qsql_odbc.pri)
include(qsqldriverbase.pri)
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS
INSTALLS += target sources
(ii). The file odbcinterface.h that describes the plugin interface is included as a header in odbc.pro. However, it is actually placed inside the directory /sqlbrowser. Hence, the line INCLUDEPATH += ../sqlbrowser is included in the above script.
(iii). Also, copied all related project files (qsql_odbc.pri, qsqldriverbase.pri, qpluginbase.pri, qt_targets.pri). Suitably modified the paths in all project files (there may be mistakes in here).
(iv). The header (qsql_odbc.h) and source (qsql_odbc.cpp) files of qsql_odbc.pri have also been copied.
Inside the directory /odbcpluginTest /sqlbrowser/
(i). Copied sqlbrowser.pro and all related files.
(ii). Created the header file odbcinterface.h that describes the plugin interface (see below) and added it to the HEADERS in sqlbrowser.pro.
#ifndef ODBCINTERFACE_H
#define ODBCINTERFACE_H
#include <QString>
#include <QtSql/qsqldriver.h>
class OdbcInterface
{
public:
virtual ~OdbcInterface() {}
virtual QSqlDriver* create(const QString &) = 0;
virtual QStringList keys() const = 0;
};
QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(OdbcInterface,
"developed similar to com.trolltech.Plugin.EchoInterface/1.0");
QT_END_NAMESPACE
#endif // ODBCINTERFACE_H
iii. Also, modified the browser.h file by adding the lines
#include "odbcinterface.h"
private:
bool loadPlugin();
OdbcInterface *odbcInterface;
Public:
void TestCase1();
iv. Also, modified the browser.cpp file by adding the function definitions:
bool Browser::loadPlugin()
{
QDir pluginsDir(qApp->applicationDirPath());
#if defined(Q_OS_WIN)
pluginsDir.cdUp();
pluginsDir.cdUp();
#endif
pluginsDir.cd("plugins");
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *pluginI = pluginLoader.instance();
if (pluginI) {
odbcInterface = qobject_cast<OdbcInterface *>(pluginI);
if (odbcInterface)
return true;
}
}
return false;
}
void Browser::TestCase1()
{
loadPlugin();
QStringList list;
list = odbcInterface->keys();
QMessageBox msgBox;
if(list.length() >0)
{
msgBox.setText("Test1 success");
}
else
{
msgBox.setText("Test1 failure");
}
msgBox.exec();
}
Testing:
In browser.cpp file, the constructor Browser::Browser(QWidget *parent) was modified by appending a call to void Browser::TestCase1()
Compile Output:
15:09:18: Running build steps for project odbcplugin...
15:09:18: Configuration unchanged, skipping qmake step.
15:09:18: Starting: "C:\QtSDK\QtCreator\bin\jom.exe"
cd sqlbrowser\ && C:\QtSDK\QtCreator\bin\jom.exe -nologo -j 2 -f Makefile
C:\QtSDK\QtCreator\bin\jom.exe -nologo -j 2 -f Makefile.Debug
cd odbc\ && C:\QtSDK\QtCreator\bin\jom.exe -nologo -j 2 -f Makefile
C:\QtSDK\QtCreator\bin\jom.exe -nologo -j 2 -f Makefile.Release
link /LIBPATH:"c:\QtSDK\Desktop\Qt\4.8.1\msvc2010\lib" /NOLOGO /DYNAMICBASE /NXCOMPAT /INCREMENTAL:NO /DLL /MANIFEST /MANIFESTFILE:"release\qsqlodbc.intermediate.manifest" /VERSION:4.81 /OUT:..\plugins\qsqlodbc4.dll #C:\DOCUME~1\SHAINE~1\LOCALS~1\Temp\qsqlodbc4.dll.5076.0.jom
Creating library ..\plugins\qsqlodbc4.lib and object ..\plugins\qsqlodbc4.exp
mainODBC.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall QODBCDriverPlugin::metaObject(void)const " (?metaObject#QODBCDriverPlugin##UBEPBUQMetaObject##XZ)
mainODBC.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall QODBCDriverPlugin::qt_metacast(char const *)" (?qt_metacast#QODBCDriverPlugin##UAEPAXPBD#Z)
mainODBC.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall QODBCDriverPlugin::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#QODBCDriverPlugin##UAEHW4Call#QMetaObject##HPAPAX#Z)
..\plugins\qsqlodbc4.dll : fatal error LNK1120: 3 unresolved externals
jom 1.0.6 - empower your cores
command failed with exit code 1120
command failed with exit code 2
command failed with exit code 2
15:09:19: The process "C:\QtSDK\QtCreator\bin\jom.exe" exited with code 2.
Error while building project odbcplugin (target: Desktop)
When executing build step 'Make'

Resources