Add network to Qt Project - c++

I try to use the QtNetwork library and added all dependencies for it in the .pro file. But when I compile my code Qt Creator fails in building the project and claims
C1083: Include "QTcpSocket": No such file or directory - telnet.h:4
I thought adding network to the .pro file would be enough?
networkmonitor.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-07-24T13:18:19
#
#-------------------------------------------------
QT += core gui network charts
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = networkmonitor
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
telnet.cpp
HEADERS += \
mainwindow.h \
telnet.h
FORMS += \
mainwindow.ui
telnet.h
#ifndef TELNET_H
#define TELNET_H
#include <QTcpSocket>
#include <QTcpServer>
#include <QDebug>
class Telnet
{
public:
Telnet();
void sendValues(QString _ip, int _port, QString _message);
private:
QTcpSocket *socket;
};
#endif // TELNET_H

Normally this means you just forgot to re-run qmake

Related

Empty QT GUI crashes after linking DLLs

For my project i need a GUI and library called "STM32CubeProgrammer CLI".
They include some example C++ projects with VS and QT (both only command line, no GUI).
I'm able to compile and work with them.
In the next step I created a QT Widgets Application and add the STM library but the program crash instantly.
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
$$PWD/../../../include/CubeProgrammer_API.h \
$$PWD/../../../include/DeviceDataStructure.h \
FORMS += \
mainwindow.ui
#comment out that line and the programm works
win32: LIBS += -L$$PWD/../../../lib/ -lCubeProgrammer_API
INCLUDEPATH += $$PWD/../../../include
DEPENDPATH += $$PWD/../../../include
This is the main.cpp I don't edit any "window" or other files, I don't even use a function of the dll.
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
The linker find the dlls, but the program only crashes if I'm using a GUI, a simple cmd program dont crash. Just for fun I added Qlibrary, but the program crashes befor.
I'm using qmake, QT 5.14.2 MinGW-32bit and QT Creator 4.11.1.

Qt5 - Windows : inconsistent dll linkage error and definition of dllimport static data member not allowed

I am trying to build a project using Windows 10 and Qt5.12. The small program is using the smtp protocol available here. I can confirm that on my Windows I have the OpenSSL 1.1.1c 28 May 2019.
On my Ubuntu 19.04 the same exact program compiles and run as usual but not on Windows.
I am attaching below a print screen of the errors; however those are mostly of two types:
1) inconsistent dll linkage
2) definition of dllimport static data member not allowed
Following this link it seems that Windows needs its "own" include (i.e. #include <windows....) however in my case the smtp library from the above link does not have any #include <windows> and don't know if they have to be generated. It seems they don't from the post I found
In addition I was reading this post too because I thought I could be useful but no information was useful to help me sort out the problem
I dug more and actually went to where the windows includes are and the following is the path I was able to find, but don't know if that could be useful:
From all the posts I red the problem seems to be, in this specific case for Windows on how the
.pro file is written. Below my .pro file.
Note that I cloned this repository into my windows 10.
.pro
QT += quick quickcontrols2 concurrent network core gui
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
TARGET = SMTPEmail
TEMPLATE = lib
DEFINES += SMTP_BUILD
win32:CONFIG += dll
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
progressbardialog.cpp \
robot.cpp \
robotmanager.cpp \
settings/emailaddress.cpp \
settings/mimeattachment.cpp \
settings/mimecontentformatter.cpp \
settings/mimefile.cpp \
settings/mimehtml.cpp \
settings/mimeinlinefile.cpp \
settings/mimemessage.cpp \
settings/mimemultipart.cpp \
settings/mimepart.cpp \
settings/mimetext.cpp \
settings/quotedprintable.cpp \
settings/smtpclient.cpp \
user.cpp \
usermanager.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
HEADERS += \
progressbardialog.h \
robot.h \
robotmanager.h \
settings/SmtpMime \
settings/emailaddress.h \
settings/mimeattachment.h \
settings/mimecontentformatter.h \
settings/mimefile.h \
settings/mimehtml.h \
settings/mimeinlinefile.h \
settings/mimemessage.h \
settings/mimemultipart.h \
settings/mimepart.h \
settings/mimetext.h \
settings/quotedprintable.h \
settings/smtpclient.h \
settings/smtpexports.h \
user.h \
usermanager.h
EDITS
To be even more specific it seems that every header the offending line is the following below:
class SMTP_EXPORT EmailAddress : public QObject // <-- SMTP_EXPORT
which leads to the smtpexports.h which I copied below:
#ifndef SMTPEXPORTS_H
#define SMTPEXPORTS_H
#ifdef SMTP_BUILD
#define SMTP_EXPORT Q_DECL_EXPORT
#else
#define SMTP_EXPORT Q_DECL_IMPORT
#endif
#endif // SMTPEXPORTS_H
ADDITIONAL EDITS
Almost all error are solved after adding DEFINES += SMTP_BUILD but I have two errors left and I added a print screen below:
Thank you very much for pointing in the right direction on how to solve this problem.
The files are designed to be compiled with the define SMTP_BUILD set where the source is added, either to a library or to an executable. You have to add the
DEFINES += SMTP_BUILD
to your pro file.

Qt with WinRT C++ build issue

I want to build a modern Windows application using WinRT (Windows 10). I use Qt 5.13.1 UWP kits for Visual Studio 2017. When building a project, it displays a lot of compilation errors:
Code:
testproject.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
testproject.cpp
HEADERS += \
testproject.h
FORMS += \
testproject.ui
LIBS += -lwindowsapp
# Default rules for deployment.
#qnx: target.path = /tmp/$${TARGET}/bin
#else: unix:!android: target.path = /opt/$${TARGET}/bin
#!isEmpty(target.path): INSTALLS += target
testproject.h
#ifndef TESTPROJECT_H
#define TESTPROJECT_H
#include <QDialog>
#include <QDebug>
#include "winrt/Windows.System.Diagnostics.h"
using namespace winrt;
using namespace Windows::System::Diagnostics;
QT_BEGIN_NAMESPACE
namespace Ui { class TestProject; }
QT_END_NAMESPACE
class TestProject : public QDialog
{
Q_OBJECT
public:
TestProject(QWidget *parent = nullptr);
~TestProject();
private:
Ui::TestProject *ui;
};
#endif // TESTPROJECT_H
testproject.cpp
#include "testproject.h"
#include "ui_testproject.h"
TestProject::TestProject(QWidget *parent)
: QDialog(parent)
, ui(new Ui::TestProject)
{
ui->setupUi(this);
init_apartment();
auto info = SystemDiagnosticInfo::GetForCurrentSystem();
auto memory = info.MemoryUsage().GetReport().TotalPhysicalSizeInBytes();
qDebug() << memory;
}
TestProject::~TestProject()
{
delete ui;
}
Without WinRT code it compiles successfully. Any ideas how to configure WinRT using Qt? What libraries are required for Qt to run WinRT code? Thanks in advance.
Try either adding #undef X64 before #include "winrt/Windows.System.Diagnostics.h" or go to your project settings, select "Configuration Properties" -> "C/C++" -> "Preprocessor", in the dropdown next to "Preprocessor Definitions" select "Edit", remove the line X64. I've no idea what this define is for, but it conflicts with the ProcessorArchitecture::X64 enum in Windows.System.0.h.
PS. This answer is just a reproduced guesswork as I can't make much sense of the errors on the screenshot. Usually copy-pasting the text from the Output panel is more informative than the Error List.

QT Unit Testing moc "unresolved external symbol" for QMetaObject

I'm trying to add unit tests to a project of mine for the first time.
I can run mock tests alright (without using my project's classes) and run the application alright. But if I instantiate objects from the project I get an unresolved external symbol of the QMetaObject. If I recall correctly, this means the moc of the object isn't being included on the project.
How do I fix this? I have the same issue using googletests. The guide also doesn't help on this. I've tried installing the qt unit testing plugin, same result.
I've uploaded a mock project that follows the same structure that I'm using in the aforementioned project, fetch it here: https://github.com/quimnuss/QtUnitTestingTest
I'm using a static build of qt on windows, but I guess that's irrellevant. Using QtCreator as IDE and NMAke build.
I've also tried add the HelloWorld.lib, but taking a look at the Makefile.release it isn't used.
Somebody has an idea of what I'm doing wrong?
Here's the unit testing .pro:
QT += widgets network testlib
TARGET = tst_someunittesttest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += $$PWD/../HelloWorld
include($$PWD/../HelloWorld/helloworldCommon.pri)
LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld
message("Searching libs here $$LIBS")
SOURCES += tst_someunittesttest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"
The first error's complete message:
tst_someunittesttest.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl HelloWorld::metaObject(void)const " (?metaObject#HelloWorld##UEBAPEBUQMetaObject##XZ)
When you use the following flags:
LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld
You must have the compiled dynamic or static library. Therefore you must create a project generating a library. In the next part I show you how to create a dynamic library.
HelloWorldLib.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-01-06T12:37:49
#
#-------------------------------------------------
QT -= gui
TARGET = HelloWorldLib
TEMPLATE = lib
DEFINES += HELLOWORLDLIB_LIBRARY
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += $$PWD/include
SOURCES += src/helloworldlib.cpp
HEADERS += include/helloworldlib.h\
include/helloworldlib_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
DESTDIR = $$PWD/lib
include/helloworldlib.h
#ifndef HELLOWORLDLIB_H
#define HELLOWORLDLIB_H
#include "helloworldlib_global.h"
#include <QDebug>
class HELLOWORLDLIBSHARED_EXPORT HelloWorldLib: public QObject
{
Q_OBJECT
public:
HelloWorldLib(){
}
static bool returnTrue()
{
return true;
}
public slots:
void someSlot()
{
qDebug() << "test";
}
};
#endif // HELLOWORLDLIB_H
include/helloworldlib_global.h
#ifndef HELLOWORLDLIB_GLOBAL_H
#define HELLOWORLDLIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(HELLOWORLDLIB_LIBRARY)
# define HELLOWORLDLIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define HELLOWORLDLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // HELLOWORLDLIB_GLOBAL_H
src/helloworldlib.cpp
#include "helloworldlib.h"
Here I show the test project.
HelloWorldTest.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-01-06T12:42:42
#
#-------------------------------------------------
QT += testlib
QT -= gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = tst_helloworldtesttest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += tst_helloworldtesttest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../HelloWorldLib/lib/release/ -lHelloWorldLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../HelloWorldLib/lib/debug/ -lHelloWorldLib
else:unix: LIBS += -L$$PWD/../HelloWorldLib/lib/ -lHelloWorldLib
INCLUDEPATH += $$PWD/../HelloWorldLib/include
DEPENDPATH += $$PWD/../HelloWorldLib/include
tst_helloworldtesttest.cpp
#include <QString>
#include <QtTest>
#include <helloworldlib.h>
#include <QDebug>
class HelloWorldTestTest : public QObject
{
Q_OBJECT
public:
HelloWorldTestTest();
private Q_SLOTS:
void testCase1_data();
void testCase1();
};
HelloWorldTestTest::HelloWorldTestTest()
{
}
void HelloWorldTestTest::testCase1_data()
{
QTest::addColumn<QString>("data");
QTest::newRow("0") << QString();
}
void HelloWorldTestTest::testCase1()
{
QFETCH(QString, data);
QVERIFY2(true, "Failure");
HelloWorldLib hw;
QVERIFY(hw.returnTrue());
}
QTEST_APPLESS_MAIN(HelloWorldTestTest)
#include "tst_helloworldtesttest.moc"
Output:
********* Start testing of HelloWorldTestTest *********
Config: Using QtTest library 5.7.1, Qt 5.7.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 6.2.1 20160830)
PASS : HelloWorldTestTest::initTestCase()
PASS : HelloWorldTestTest::testCase1(0)
PASS : HelloWorldTestTest::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 0ms
********* Finished testing of HelloWorldTestTest *********
In the following link is the complete project: https://github.com/eyllanesc/stackoverflow/tree/master/QtUnitTestingTest

Porting a Windows compiled QT application to Linux?

I created this simply application in Qt4 and would like to test it on RH Linux distro. The distro has both QtCore "Qt4" and Qt3 installed. I cannot add or delete any of these Qt versions, but would like to work with what's available.
I have an error compiling my windows based Qt program "Qt4" in Linux .
First question :
- How do I compile my compile in Linux without QT creator "only Qt libraries" are installed, what I did is get the .pro file from windows and typed qmake . , the errors are :
WARNING : Found potential symbol conflict of mainwindow.cpp (mainwwindow.cpp) in SOURCES
WARNING : Found potential symbol conflict of mainwindow.h (mainwwindow.cpp) in HEADERS
WARNING : Found potential symbol conflict of dialog.cpp (dialog.cpp) in SOURCES
WARNING : Found potential symbol conflict of dialog.h (dialog.h) in HEADERS
How can I modify qmake to specify the version of Qt
Thank you.
Below is my .pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestTool
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
dialog.cpp
HEADERS += mainwindow.h \
dialog.h
FORMS += mainwindow.ui \
dialog.ui
The compilation error is :
Try using the Qt4 version of qmake explicitly:
/usr/lib64/qt4/bin/qmake -o Makefile TestTool.pro