qt simple tcp communication with ui projects - qt

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.

Related

Link error while using QAudioDeviceInfo in QT console application

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();

Qt with OpenCV: LNK2019:unresolved external symbol and File not Found mainwindow.obj

I am trying to build an example qt-opencv project Hello_Qt_OpenCVthat is provided by Computer-Vision-with-OpenCV-3-and-Qt5. I use QtCreator4.5 with Qt5.7 and opencv3.2. Their configurations work fine with some other qt-opencv projects.
Building the project gives three issues:
File not found: mainwindow.obj: LNK2019: unresolved external symbol "void __cdecl cv::medianBlur(class cv::_InputArray const &,class cv::_OutputArray const &,int)" (?medianBlur#cv##YAXAEBV_InputArray#1#AEBV_OutputArray#1#H#Z) referenced in function "private: void __cdecl MainWindow::on_outputPushButton_pressed(void)" (?on_outputPushButton_pressed#MainWindow##AEAAXXZ)
File not found: mainwindow.obj: LNK2019: unresolved external symbol "void __cdecl cv::GaussianBlur(class cv::_InputArray const &,class cv::OutputArray const &,class cv::Size,double,double,int)" (?GaussianBlur#cv##YAXAEBV_InputArray#1#AEBV_OutputArray#1#V?$Size_#H#1#NNH#Z) referenced in function "private: void __cdecl MainWindow::on_outputPushButton_pressed(void)" (?on_outputPushButton_pressed#MainWindow##AEAAXXZ)
LNK1120: 2 unresolved externals release\Hello_Qt_OpenCV.exe:-1: error
I tried some suggestions at (File not found: mainwindow.obj by clean, run qmake and build, which however won't work in this case.
For your information, I post the mainwindow.cpp and mainwindow.h:
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
loadSettings();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_inputPushButton_pressed()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open Input Image", QDir::currentPath(), "Images (*.jpg *.png *.bmp)");
if(QFile::exists(fileName))
{
ui->inputLineEdit->setText(fileName);
}
}
void MainWindow::on_outputPushButton_pressed()
{
QString fileName = QFileDialog::getSaveFileName(this, "Select Output Image", QDir::currentPath(), "*.jpg;;*.png;;*.bmp");
if(!fileName.isEmpty())
{
ui->outputLineEdit->setText(fileName);
using namespace cv;
Mat inpImg, outImg;
inpImg = imread(ui->inputLineEdit->text().toStdString());
if(ui->medianBlurRadioButton->isChecked())
cv::medianBlur(inpImg, outImg, 5);
else if(ui->gaussianBlurRadioButton->isChecked())
cv::GaussianBlur(inpImg, outImg, Size(5, 5), 1.25);
imwrite(fileName.toStdString(), outImg);
if(ui->displayImageCheckBox->isChecked())
imshow("Output Image", outImg);
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
int result = QMessageBox::warning(this, "Exit", "Are you sure you want to close this program?", QMessageBox::Yes, QMessageBox::No);
if(result == QMessageBox::Yes)
{
saveSettings();
event->accept();
}
else
{
event->ignore();
}
}
void MainWindow::loadSettings()
{
QSettings settings("Packt", "Hello_OpenCV_Qt", this);
ui->inputLineEdit->setText(settings.value("inputLineEdit", "").toString());
ui->outputLineEdit->setText(settings.value("outputLineEdit", "").toString());
ui->medianBlurRadioButton->setChecked(settings.value("medianBlurRadioButton", true).toBool());
ui->gaussianBlurRadioButton->setChecked(settings.value("gaussianBlurRadioButton", false).toBool());
ui->displayImageCheckBox->setChecked(settings.value("displayImageCheckBox", false).toBool());
}
void MainWindow::saveSettings()
{
QSettings settings("Packt", "Hello_OpenCV_Qt", this);
settings.setValue("inputLineEdit", ui->inputLineEdit->text());
settings.setValue("outputLineEdit", ui->outputLineEdit->text());
settings.setValue("medianBlurRadioButton", ui->medianBlurRadioButton->isChecked());
settings.setValue("gaussianBlurRadioButton", ui->gaussianBlurRadioButton->isChecked());
settings.setValue("displayImageCheckBox", ui->displayImageCheckBox->isChecked());
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <QDir>
#include <QFile>
#include <QCloseEvent>
#include <QMessageBox>
#include <QSettings>
#include "opencv2/opencv.hpp"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void closeEvent(QCloseEvent *event);
private slots:
void on_inputPushButton_pressed();
void on_outputPushButton_pressed();
private:
Ui::MainWindow *ui;
void loadSettings();
void saveSettings();
};
#endif // MAINWINDOW_H
You may also download the project files and give a quick run to see if the issues are related to the project itself.
Add imgproc320 in the .pro file, and then the problem is solved

LNK2019: extern symbol error

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.

QSqlDatabase LNK2019 error

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

QT: unresolved external symbol while trying to get webpage source code

I started developing with Qt and I'm trying to get webpage source code. Here's what I have :
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QUrl>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
/**/
class GetHTMLSource : public QObject
{
Q_OBJECT
public:
GetHTMLSource();
void GetSource();
public slots:
void GetDone(QNetworkReply*);
private:
QNetworkAccessManager* NetManager;
};
GetHTMLSource::GetHTMLSource()
{
NetManager = new QNetworkAccessManager(this);
connect(NetManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(GetDone(QNetworkReply*)));
}
void GetHTMLSource::GetSource()
{
NetManager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));
}
void GetHTMLSource::GetDone(QNetworkReply* ReplyIn)
{
QByteArray DataIn=ReplyIn->readAll();
QString DataString(DataIn);
//process str any way you like!
}
/**/
void MainWindow::on_pushButton_clicked()
{
GetHTMLSource Test;
Test.GetSource();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
.pro
QT += core gui
TARGET = TestGetPageSource
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
QT += network
Errors are :
mainwindow.obj:-1: error:LNK2001: unresolved external symbol "public:
virtual struct QMetaObject const * __thiscall
GetHTMLSource::metaObject(void)const "
(?metaObject#GetHTMLSource##UBEPBUQMetaObject##XZ)
mainwindow.obj:-1: error:LNK2001: unresolved external symbol "public:
virtual void * __thiscall GetHTMLSource::qt_metacast(char const *)"
(?qt_metacast#GetHTMLSource##UAEPAXPBD#Z)
mainwindow.obj:-1: error:LNK2001: unresolved external symbol "public:
virtual int __thiscall GetHTMLSource::qt_metacall(enum
QMetaObject::Call,int,void * *)"
(?qt_metacall#GetHTMLSource##UAEHW4Call#QMetaObject##HPAPAX#Z)
Generally if you create classes derived from QObject you have to put them in separate header and source files.
So one .h and one .cpp file for one class.
Fixed it on my own. Of course it was my stupid mistake.
In class definition :
public slots:
void GetDone(QNetworkReply*);
was not matching actual function :
void GetHTMLSource::GetDone(QNetworkReply* ReplyIn)
{ ... }
However moving it to seperated .h and .cpp files probably helped.

Resources