I tried to compile the following code in Qt 5.0.0:
#include <QApplication>
#include <QtSql/QSql>
#include <Qtsql/QSqlDatabase>
#include <QStringList>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringList db = QSqlDatabase::drivers();
return a.exec();
}
and I received this error:
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QStringList __cdecl QSqlDatabase::drivers(void)" (__imp_?drivers#QSqlDatabase##SA?AVQStringList##XZ) referenced in function _main
debug\test.exe:-1: error: LNK1120: 1 unresolved externals
I have added QT += sql in my .pro. What's the problem?
You should add QtSql.lib ( you can find the name of Qtsql exactly in QT/lib in your computer) by go to Project/Properties/Configuration properties/Linker/Input, add QtSql.lib to Additional Dependencies
P.S. I used to face this error, and the error is fixed by this way. Good luck
Related
I have created simple QT console application in Visual Studio 2019
Following is the code
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
std::cout << "Available Audio devices ";
QList<QAudioDeviceInfo> audioDevices = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
return a.exec();
}
When compiledi am getting the following link error
>------ Build started: Project: AVSyncTesting, Configuration: Debug x64 ------
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QAudioDeviceInfo::~QAudioDeviceInfo(void)" (__imp_??1QAudioDeviceInfo##QEAA#XZ) referenced in function "public: void * __cdecl QAudioDeviceInfo::`scalar deleting destructor'(unsigned int)" (??_GQAudioDeviceInfo##QEAAPEAXI#Z)
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QList<class QAudioDeviceInfo> __cdecl QAudioDeviceInfo::availableDevices(enum QAudio::Mode)" (__imp_?availableDevices#QAudioDeviceInfo##SA?AV?$QList#VQAudioDeviceInfo####W4Mode#QAudio###Z) referenced in function main
Try to use this code to get devices:
const auto deviceInfos = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
for (const QAudioDeviceInfo &deviceInfo : deviceInfos)
qDebug() << "Device name: " << deviceInfo.deviceName();
Hey I am using Qt Creator because there is no qml implementation in visual studio yet.
I am not able to change the style of quick components no matter what. I have tried it with global constants and as well as C++ API call but it still throws errors. I don't know what else to try.
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQuickControls2/QQuickStyle>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickStyle::setStyle(QLatin1String("Material"));
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
I stopped at this, I get these errors:
main.qml:6: error: Expected token `{'
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl QQuickStyle::setStyle(class QString const &)" (__imp_?setStyle#QQuickStyle##SAXAEBVQString###Z) referenced in function main
debug\RandomGenerator.exe:-1: error: LNK1120: 1 unresolved externals
I don't know what to do, there are no more tutorials explaining this.
You can try to do this in qml.
enter this in qtquickcontrols2.conf
[Controls]
Style=Material
[Material]
Theme=Dark
If you want to change color or another properetys enter this in main.qml in AplicationWindow
Material.accent: Material.color(Material.Blue)
More info about styles and there properetys you can find here
I am quite new to Qt framework. I try to code a class that contains a 2D array whose data is supposed to be displayed in the main window (using QTableWidget, for example). The class has the following declaration:
// gridgame.h
template <int gsize_r,int gsize_c>
class GridGame : public QObject
{
Q_OBJECT
private:
char field[gsize_r][gsize_c];
public:
GridGame();
void setupConnect(QMainWindow & win);
signals:
void onPrintStatus(char[gsize_r][gsize_c]);
};
The definition of the class is of course given in cpp file as follows:
//gridgame.cpp
#include "gridgame.h"
template <int gsize_r,int gsize_c>
GridGame<gsize_r,gsize_c>::GridGame()
{
int i,j;
for (i=0;i<gsize_r;++i)
for (j=0;j<gsize_c;++j)
field[i][j] = '-';
}
template <int gsize_r,int gsize_c>
void GridGame<gsize_r,gsize_c>::setupConnect(QMainWindow &win)
{
connect(this,SIGNAL(onPrintStatus()),&win,SLOT(onRefresh()));
}
template <int gsize_r,int gsize_c>
void GridGame<gsize_r,gsize_c>::onPrintStatus(char[gsize_r][gsize_c]){
emit field;
}
The class is used in main as follows:
#include <QtCore>
#include <QApplication>
#include <QObject>
#include "mainwindow.h"
#include "gridgame.cpp"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
const int sizer = 5, sizec = 5;
GridGame<sizer,sizec> gg;
w.show();
gg.printStatus();
return a.exec();
}
Question: The code seems to compile ok, but I get the linker errors even if I do not call any function of the class (just have a declaration of a variable)
-1: error: undefined reference to `GridGame<5, 5>::metaObject() const'
-1: error: undefined reference to `GridGame<5, 5>::qt_metacast(char const*)'
-1: error: undefined reference to `GridGame<5, 5>::qt_metacall(QMetaObject::Call, int, void**)'
-1: error: error: ld returned 1 exit status
What might be the problem? I am using Qt Creator 3.6.1 and Qt library 5.6.0.
Thanks in advance for any help.
I want to create a simple Tcp Communication project but I get some problems and I dont know how to solve that problem. When I try to find solution all people tell add this code (QT += network)on .pro file but in ui projects I dont have any pro file so I dont know the find the solution.
//commu.h
#ifndef COMMU_H
#define COMMU_H
#include <QtWidgets/QMainWindow>
#include "ui_commu.h"
#include <QtNetwork/QTcpSocket>
#include <QObject>
#include <QString>
class commu : public QMainWindow
{
Q_OBJECT
public:
commu(QWidget *parent = 0);
~commu();
void start(QString address, quint16 port);
private:
Ui::commuClass ui;
QTcpSocket client;
public slots:
void startTransfer();
};
#endif // COMMU_H
//commu.cpp
#include "commu.h"
#include <QtNetwork/QHostAddress>
commu::commu(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(&client, SIGNAL(connected()),this,SLOT(startTransfer()));
}
commu::~commu()
{
client.close();
}
void commu::start(QString address, quint16 port)
{
QHostAddress addr(address);
client.connectToHost(addr, port);
}
void commu::startTransfer()
{
client.write("Hello, world", 13);
}
//main.cpp
#include "commu.h"
#include <QtWidgets/QApplication>
#include <QtCore>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
commu w;
w.show();
return a.exec();
commu client;
client.start("127.0.0.1", 8888);
}
I get the errors:
1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QTcpSocket::~QTcpSocket(void)" (__imp_??1QTcpSocket##UAE#XZ) referenced in function "public: virtual __thiscall commu::~commu(void)" (??1commu##UAE#XZ)
1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QTcpSocket::QTcpSocket(class QObject *)" (__imp_??0QTcpSocket##QAE#PAVQObject###Z) referenced in function "public: __thiscall commu::commu(class QWidget *)" (??0commu##QAE#PAVQWidget###Z)
1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::~QHostAddress(void)" (__imp_??1QHostAddress##QAE#XZ) referenced in function "public: void __thiscall commu::start(class QString,unsigned short)" (?start#commu##QAEXVQString##G#Z)
1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::QHostAddress(class QString const &)" (__imp_??0QHostAddress##QAE#ABVQString###Z) referenced in function "public: void __thiscall commu::start(class QString,unsigned short)" (?start#commu##QAEXVQString##G#Z)
1>c:\users\sel\documents\visual studio 2010\Projects\commu\Win32\Debug\\commu.exe : fatal error LNK1120: 4 unresolved externals
You need to enable modules you're using in Qt Project Settings. More info you can find in Qt docs: Qt Visual Studio Add-in
You also shouldn't use includes like
#include <QtWidgets/QMainWindow>
you should always include only class file without path like
#include <QMainWindow>
Same goes for all modules, so skip QtNetwork etc after you enable modules for your project.
I am using Qt and i want to show one window of configuration, and when the user push a button, show it the main window.
I think in that:
#include "mainwindow.h"
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog b;
b.show();
a.exec();
MainWindow w;
w.show();
return a.exec();
}
but when i execute it, Qt tolds me:
LNK2019 extern "public: virtual __thiscall Dialog::~Dialog(void)" (??1Dialog##UAE#XZ) sin resolver al que se hace referencia en la función _main
and:
main.obj:-1: error: LNK2019: símbolo externo "public: __thiscall Dialog::Dialog(class QWidget *)" (??0Dialog##QAE#PAVQWidget###Z) sin resolver al que se hace referencia en la función _main
Anyone knows what I can do? If I delete the object Dialog, it works well. Its there any other form to have two windows?
Thanks for your time.