I can't figure out how to link FTDI library in my Qt project. I copied ftd2xx.h file to my project directory. The file I want to link is dll: ftd2xx.lib which is stored in F:\Workspace\qt\libs\ftdi\amd64
I get error:
release/testftdi.o:testftdi.cpp:(.text+0x6f8): undefined reference to `_imp__FT_Open#8'
collect2.exe: error: ld returned 1 exit status
I have QtWidget application with one PushButton:
TestFtdi.pro file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestFtdi
TEMPLATE = app
LIBS += -L"F:\Workspace\qt\libs\ftdi\amd64" -lftd2xx
INCLUDEPATH += f:/Workspace/qt/libs/ftdi/amd64
SOURCES += main.cpp\
testftdi.cpp
HEADERS += testftdi.h
FORMS += testftdi.ui
main.cpp file:
#include "testftdi.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TestFtdi w;
w.show();
return a.exec();
}
testftdi.h file:
#ifndef TESTFTDI_H
#define TESTFTDI_H
#include <QMainWindow>
namespace Ui {
class TestFtdi;
}
class TestFtdi : public QMainWindow
{
Q_OBJECT
public:
explicit TestFtdi(QWidget *parent = 0);
~TestFtdi();
private slots:
void on_pushButton_clicked();
private:
Ui::TestFtdi *ui;
};
#endif // TESTFTDI_H
testftdi.cpp file:
#include "testftdi.h"
#include "ui_testftdi.h"
#include <QDebug>
#include "windows.h"
#include "ftd2xx.h"
TestFtdi::TestFtdi(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TestFtdi)
{
ui->setupUi(this);
}
TestFtdi::~TestFtdi()
{
delete ui;
}
void TestFtdi::on_pushButton_clicked()
{
FT_HANDLE ftHandle;
FT_STATUS ftStatus;
ftStatus = FT_Open(0, &ftHandle);
if(ftStatus != FT_OK) { // FT_Open failed
qDebug() << "FT_Open failed";
}
}
The compiler command looks in this situation like this:
g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release\TestFtdi.exe release/main.o release/testftdi.o release/moc_testftdi.o -lmingw32 -LC:/Qt/5.5/mingw492_32/lib -lqtmain -lshell32 -LF:\Workspace\qt\libs\ftdi\Static\amd64 -lftd2xx -lQt5Widgets -lQt5Gui -lQt5Core
Could you help me with this?
My guess is, compiler might be looking for ftd2xx rather than ftd2xx.lib (file name is ftd2xx.lib.dll, right?). Have you tried changing the LIBS line to
LIBS += -L"F:\Workspace\qt\libs\ftdi\amd64" -lftd2xx.lib
Related
I am connecting the projector to Quectel QCOM application. Now I want to skip that application and create my customized application to control the projector.
I wrote a program based on the guidelines of Quectel to switch off the projector. Following is the code. Even I used the QSerialPort code. But the projector couldn't switch off. However using the command prompt of the Quectel QCOM application, I could control the projector.
Please help me in this guys! I am stuck in this since a month.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec(); //
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include<QSerialPort>
#include<string>
#include<QtGui>
#include <QMessageBox>
using namespace std;
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow() //
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QMessageBox::information(this,"Title here","Hello World");
serial = new QSerialPort(this);
serial->setPortName("COM3");
serial->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
serial->setDataBits(QSerialPort::Data8);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setStopBits(QSerialPort::OneStop);
serial->setParity(QSerialPort::NoParity);
if(!serial->open(QIODevice::ReadWrite))
{
qDebug()<<"error: can't open com port";
}
QString command = "WT+LEDE=0\n";
QByteArray x = command.toLocal8Bit();
serial->write(x);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
QMake project file:
#-------------------------------------------------
Project created by QtCreator 2019-11-02T10:55:21
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = GUISerialPort
TEMPLATE = app
SOURCES += main.cpp
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
I'm having problems including winscard.lib in my QT project.
I have windows 10 x64 and I downloaded the microsoft sdk V7.1 because I didn't have the winscard library in the microsoft sdk v10.
Now, my .pro file looks like this:
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = NFC
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp \
globs.cpp \
newcard.cpp \
newcircuitnumber.cpp
HEADERS += \
mainwindow.h \
globs.h \
newcard.h \
newcircuitnumber.h
FORMS += \
mainwindow.ui \
newcard.ui \
newcircuitnumber.ui
INCLUDEPATH += "C:\Program Files\Microsoft SDKs\Windows\v7.1\Include"
LIBS += -L"C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\IA64"
LIBS += -winscard -lz
mainwindow.h
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QDebug>
#include <QMessageBox>
#include <QSqlQuery>
#include "globs.h"
#include "qt_windows.h"
#include "winnt.h"
#include "winscard.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
SCARDCONTEXT cardContext;
LPWSTR mszReaders;
DWORD dwReaders;
SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL,&cardContext);
dwReaders = SCARD_AUTOALLOCATE;
SCardListReadersW(cardContext,NULL,(LPWSTR)&mszReaders,&dwReaders);
QByteArray buffer;
wchar_t *it = mszReaders;
while(*it !='\0')
{
buffer.append(QChar(*it));
*it++;
}
qDebug()<<buffer;
}
The error I get when I try to compile are the following:
Errors
Can somebody point me to a solution? What am I doing wrong?
Thanks a lot in advance.
I am using Qt5.4.3 and at some point, Qt prints out message like this
LEAK: 145 CachedResource
LEAK: 4432 WebCoreNode
I try to display a webpage on a Qt application, but whatever the webpage is, memory always leaks.
Here is the all code of project named"test"(I add the webview control in mainwindow.ui)
#test.pro
QT += core gui webkit
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets webkitwidgets multimedia multimediawidgets
TARGET = EchartDemo
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
INCLUDEPATH += $$PWD
MOC_DIR = temp/moc
RCC_DIR = temp/rcc
UI_DIR = temp/ui
OBJECTS_DIR = temp/obj
DESTDIR = bin
//main.cpp
#include "widget.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.showMaximized();
return a.exec();
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//`widget.cpp`
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->webView->load(QUrl("file:///"+qApp->applicationDirPath()+"/html/hao.html"));
}
Widget::~Widget()
{
delete ui;
}
//hao.html
<meta http-equiv="Refresh" content="0; url=http://www.hao123.com/?1460255739"/><meta property="shurufa:url-navigate" content="985" />
In order to show the webpage normally, you should put the html file to the folder \build-EchartDemo-Desktop_Qt_5_4_2_MinGW_32bit-Debug\bin\html.
Of course, you can change the content of the hao.html file to whatever you like.
Why do I have these memory leaks?
This is a known bug in Qt and these "memory leaks" are only warnings in some cases. You can read more about it here : Qt Bug 40373, and also see the other bug reports mentioning those leaks.
Also, you should consider to use QWebEngineView which is much better ;) (and update to Qt5.6, but it is another story!).
I have a C++/Qt application which loads a plugin (.dll/.so) using QPluginLoader facilities.
This plugin is basically an embedded python interpreter which allows to inspect Qt objects in the main application via the PyQt4 module.
Problem is the command PyQt4.QtCore.QCoreApplication.instance(), executed from the plugged-in python interpreter, returns None even though the QCoreApplication instance has been created by the C++ application.
This is only on windows in debug mode.
On linux or on release mode on windows, the command PyQt4.QtCore.QCoreApplication.instance() correctly returns the QCoreApplication instance that was created by the C++ application.
Following is some minimalist code showing the problem.
When compiled in release mode:
$ ./a.out
1+1
2
import PyQt4
import PyQt4.QtCore
PyQt4.QtCore.QCoreApplication.instance()
<PyQt4.QtCore.QCoreApplication object at 0x00C69198>
=> Ok
When compiled in debug mode:
$ ./a.out
import PyQt4
import PyQt4.QtCore
PyQt4.QtCore.QCoreApplication.instance()
=> Not ok (returned None)
File main.cpp
#include <QCoreApplication>
#include <QPluginLoader>
#include <QDebug>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QPluginLoader loader("plugin.dll");
loader.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint);
loader.load();
if(!loader.isLoaded()) {
qDebug() << loader.errorString();
return 1;
}
(void)loader.instance();
return app.exec();
}
File plugin.h
#ifndef PLUGIN_H
#define PLUGIN_H
#include <QObject>
#include <Python.h>
class Plugin : public QObject
{
public:
Plugin();
~Plugin();
private:
PyThreadState *m_ts;
};
class InterpInput : public QObject
{
Q_OBJECT
public:
InterpInput(QObject *parent = 0) : QObject(parent) { }
public slots:
void monitorInput();
signals:
void done();
void inputReady();
};
class InterpOutput : public QObject
{
Q_OBJECT
public:
InterpOutput(QObject *parent = 0) : QObject(parent) { }
public slots:
void processLine();
public:
PyThreadState *m_ts;
};
#endif
File plugin.cpp
#include "plugin.h"
#include <QCoreApplication>
#include <QThread>
#include <QtPlugin>
#include <QPluginLoader>
Q_EXPORT_PLUGIN2(Plugin, Plugin)
Plugin::Plugin()
{
Py_Initialize();
PyEval_InitThreads();
InterpInput *in = new InterpInput();
InterpOutput *out = new InterpOutput(this);
in->connect(in, SIGNAL(inputReady()), out, SLOT(processLine()));
in->connect(in, SIGNAL(done()), QCoreApplication::instance(), SLOT(quit()));
QThread *thr = new QThread(this);
in->moveToThread(thr);
thr->connect(thr, SIGNAL(started()), in, SLOT(monitorInput()));
m_ts = PyEval_SaveThread();
out->m_ts = m_ts;
thr->start();
}
Plugin::~Plugin()
{
PyEval_RestoreThread(m_ts);
Py_Finalize();
}
void InterpInput::monitorInput()
{
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
int ret = PyRun_SimpleString("import sys\nimport code\nic = code.InteractiveConsole()");
assert(ret == 0);
while(true) {
ret = PyRun_SimpleString("line = ic.raw_input()");
if(ret) { break; }
inputReady();
}
done();
PyGILState_Release(gstate);
}
void InterpOutput::processLine()
{
PyEval_RestoreThread(m_ts);
int ret = PyRun_SimpleString("ic.push(line)");
PyRun_SimpleString("sys.stdout.flush()");
PyRun_SimpleString("sys.stderr.flush()");
(void)PyEval_SaveThread();
assert(ret == 0);
}
File Makefile
MOC=/cygdrive/c/Qt/4.8.4/bin/moc
GCC=/cygdrive/c/MinGW/bin/mingw32-g++.exe
FLAGS=-Ic:/Qt/4.8.4/include -Ic:/Qt/4.8.4/include/QtCore -Lc:/Qt/4.8.4/lib -Lc:/Qt/4.8.4/bin -lQtCore4 -Lc:/Python27/libs -lpython27 -Ic:/Python27/include -DQT_NO_DEBUG
#FLAGS=-Ic:/Qt/4.8.4/include -Ic:/Qt/4.8.4/include/QtCore -Lc:/Qt/4.8.4/bin -lQtCored4 -Lc:/Python27/libs -lpython27 -Ic:/Python27/include -g
LIBFLAGS=-shared
all:
$(MOC) plugin.h > plugin_moc.cpp
$(GCC) -o a.out main.cpp $(FLAGS)
$(GCC) -o plugin.dll $(LIBFLAGS) plugin.cpp plugin_moc.cpp $(FLAGS)
The explanation is the following.
In debug version, the C++ application and plugin link to debug Qt libraries (QtCored4.dll, etc.)
Whereas the installed PyQt4 modules (QtCore.pyd, etc.) link to release Qt libraries (QtCore4.dll, etc.)
It follows that the C++ application, and the PyQt4 module, each see a QCoreApplication instance but each see a different one, which lives in a different library (respectively the debug, and the release, version of the Qt library).
It follows that the instance living in the PyQt4 module is not initialized when the C++ application is initialized, and is thus null.
This is verified by compiling the PyQt4 modules specifying that they should be linked to the debug Qt libraries: the problem then disappears as the debug-mode C++ application and the PyQt4 modules link to the same Qt library.
I try to use QwtPlot, but when i add this line to my MainWindow.cpp
QwtPlot *plot = new QwtPlot(QwtText("Demo"), this);
the application compile and link without errors, but when I try to run it I get
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff514227c in ?? () from /usr/lib/libQtGui.so.4
without any backtrace. My .pro file:
INCLUDEPATH += /usr/include/qwt
CONFIG += qwt
LIBS += -lqwt
I'm using Qwt 6.0.2, Qt Creator 2.7.0 and have Qt 4.8.4 and 5.0.2 installed.
The error also occours when I create a "Qt Gui Application" (without .ui file) and just this code:
qwt-test.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = qwt-test
TEMPLATE = app
INCLUDEPATH += /usr/include/qwt
CONFIG += qwt
LIBS += -lqwt
SOURCES += main.cpp\
MainWindow.cpp
HEADERS += MainWindow.hpp
main.cpp
#include "MainWindow.hpp"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug() << "main";
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow.hpp
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_HPP
MainWindow.cpp
// MainWindow.cpp
#include "MainWindow.hpp"
#include <qwt_plot.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QwtPlot *plot = new QwtPlot(QwtText("Demo"), this);
}
MainWindow::~MainWindow()
{
}
Thanks!
It was a problem with Qt Creator (or Qwt being incompatible with Qt 5), it recognized qmake as qmake for Qt 4 but it was for Qt 5. Fixing the versions in Options -> Build&Run -> Qt Versions and using Qt 4 for the project fixed the segfault.