Running simple Qt GUI application in Go - qt

I am trying to modify following simple code from here to use Qt in Go.
#include <qapplication.h>
#include <qpushbutton.h>
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QPushButton hello( "Hello world!", 0 );
hello.resize( 100, 30 );
a.setMainWidget( &hello );
hello.show();
return a.exec();
}
My Go (Golang) code is:
package main
import (
"github.com/therecipe/qt"
"github.com/therecipe/qt/widgets"
)
func main() {
a := qt.NewQApplication(len(os.Args), os.Args) //ch: widgets.NewQ..
hello := widgets.QPushButton( "Hello world!", 0 )
a.setMainWidget( &hello )
hello.Show()
a.Exec()
}
However, I get following error:
# github.com/therecipe/qt/core
core.cpp:9:30: fatal error: QAbstractAnimation: No such file or directory
#include <QAbstractAnimation>
^
compilation terminated.
I have installed Qt bindings in Go using command:
go get "github.com/therecipe/qt"
I am working on Debian9 Linux and have following Qt4 packages installed:
libntrack-qt4-1 016-1.3 016-1.3 016-1.3 install
libqt4-dev 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 install
libqt4-dev-bin 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 install
libqt4-opengl-dev 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 install
python-qt4 4.11.4+dfsg-2+b1 4.11.4+dfsg-2+b1 4.11.4+dfsg-2+b1 install
qt4-default 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 install
qt4-linguist-tools 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 install
qt4-qmake 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 4:4.8.7+dfsg-11 install
ruby-qt4 4:4.14.3-1+b3 4:4.14.3-1+b3 4:4.14.3-1+b3 install
Where is the problem and how can it be solved? Thanks for your help.
Edit: For installing, I followed commands at https://github.com/therecipe/qt/wiki/Installation-on-Linux [Your system package managers version (without Android support)]. However, the command qtsetup gives following output with error:
$ sudo /home/abcde/go/bin/qtsetup
ERRO[0000] failed to run command _func=RunCmd cmd="go env GOMOD" dir= env= error="exec: \"go\": executable file not found in $PATH" name=GOMOD

Related

QML Svg in qt 5.15 static build, using CMake

I would like to migrate from 5.9 to 5.15 and I don't get to load svg from QML.
The original version, using qt provided by Ubuntu (5.9) did load svg from QML with
no extra steps. The new build uses a static Qt.
I use:
Ubuntu 18
In the house static build of Qt 5.15
CMake (3.18)
build Qt
./configure \
-confirm-license \
-release \
-static \
-no-pch \
-nomake tests \
-nomake examples \
-nomake tools \
-nomake examples \
-nomake tests \
-skip qtdoc \
-skip wayland \
-skip qtwebengine \
--prefix=/opt/Qt-5.15.0
CMake
cmake_minimum_required(VERSION 3.18)
project(test VERSION 0.0.0 LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5 5.15
PATHS /opt/Qt-5.15.0
COMPONENTS
Core
Widgets
Qml
Svg
Quick
QuickControls2
Charts
Multimedia
LinguistTools
QmlImportScanner
Xml
REQUIRED QUIET
)
add_executable(test_qt
# c++ code files
main.cpp
# qml code files
qml/Main.qml
# resource files
qml/resources.qrc
)
target_include_directories(test_qt PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(test_qt
PRIVATE
Qt5::Widgets
Qt5::Qml
Qt5::Quick
Qt5::QuickControls2
Qt5::Svg
Qt5::Xml
Qt5::Gui
)
qt_import_qml_plugins(test_qt
INCLUDE
Qt5::QtQuick2Plugin
Qt5::QSvgPlugin
Qt5::QtQuickControls2Plugin
)
main.cpp
#include <QGuiApplication>
#include <QObject>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlContext>
int main(int argc, char *argv[]) {
int status = 0;
try {
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl("qrc:/Main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::quit, &QGuiApplication::quit);
if (engine.rootObjects().isEmpty())
return -1;
status = app.exec();
} catch (std::exception &) {
return -1;
}
return status;
}
Main.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
id: window
width: 850
height: 850
visible: true
color: "lightgray"
Image{
source: "image.svg"
}
}
I omit the resource file, but it lists qml, png and svg files.
When using PNG image is loaded successfully, but when using SVG the following error appears:
qrc:/Main.qml:13:5: QML Image: Error decoding: qrc:/image.svg: Unsupported image format
The current Qt build contains the following files related to SVG handling:
./lib/libQt5Svg.a
./lib/libQt5Svg.la
./lib/cmake/Qt5Svg
./lib/cmake/Qt5Svg/Qt5SvgConfigVersion.cmake
./lib/cmake/Qt5Svg/Qt5SvgConfig.cmake
./lib/cmake/Qt5Gui/Qt5Gui_QSvgIconPlugin.cmake
./lib/cmake/Qt5Gui/Qt5Gui_QSvgPlugin_Import.cpp
./lib/cmake/Qt5Gui/Qt5Gui_QSvgIconPlugin_Import.cpp
./lib/cmake/Qt5Gui/Qt5Gui_QSvgPlugin.cmake
./lib/pkgconfig/Qt5Svg.pc
./lib/libQt5Svg.prl
./include/QtSvg
./include/QtSvg/QGraphicsSvgItem
./include/QtSvg/5.15.0/QtSvg
./include/QtSvg/QSvgRenderer
./include/QtSvg/QSvgWidget
./include/QtSvg/QSvgGenerator
./include/QtSvg/QtSvg
./include/QtSvg/QtSvgVersion
./include/QtSvg/QtSvgDepends
EDIT
I added the missing Svg library dependency to the find_package command. The issue remains unsolved.
If I link against a shared build of the Qt libraries, this project works correctly.
The solution for me was in addition to including Qt5::Svg in find_package and target_link_libraries, eg:
find_package(Qt5 REQUIRED COMPONENTS Core Quick Qml Xml Charts Gui Svg QuickControls2)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick Qt5::Qml Qt5::Xml Qt5::Charts Qt5::Gui Qt5::Svg Qt5::QuickControls2)
To also include the following when statically linking Qt 5.15:
qt5_import_plugins(${PROJECT_NAME} INCLUDE Qt5::QSvgPlugin)
That resolved errors such as "QML ButtonIcon: Error decoding: qrc:/resources/images/add-white-36dp.svg: Unsupported image format"
You are using cmake, but in a qmake .pro project,using static linking, you
must add
QT += svg
Note that svg plugin depends of widgets:
"QT += svg" is the same that "QT += svg widgets"
In android/IOS is better skip svg images to reduce app size.
/opt/Qt/5.15.1/gcc_64/lib$ ldd libQt5Svg.so
linux-vdso.so.1 (0x00007fff7bff0000)
libQt5Widgets.so.5 => /opt/Qt/5.15.1/gcc_64/lib/./libQt5Widgets.so.5 (0x00007ff0cbf55000)
libQt5Gui.so.5 => /opt/Qt/5.15.1/gcc_64/lib/./libQt5Gui.so.5 (0x00007ff0cb624000)
You can try to add some C++ QTSVG code in your main.cpp ,to test linking
https://doc.qt.io/qt-5/qsvggenerator.html

Ploting an image with 'imshow' of opencv in webots

hi I am using opencv in webots and I want to plot an image . This is the controller:
#include <webots/Robot.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace webots;
using namespace cv;
int main(int argc, char **argv){
Robot *robot = new Robot();
int timeStep = (int) robot->getBasicTimeStep();
while (robot->step(timeStep) != -1) {
Mat a1 = imread("/home/alireza/Desktop/1.jpg");
cv::imshow("test",a1);
};
delete robot;
return 0;
}
and this is the makefile:
OPENCV = `pkg-config opencv --cflags --libs`
LIBRARIES = $(OPENCV)
space :=
space +=
WEBOTS_HOME_PATH=$(subst $(space),\ ,$(strip $(subst \,/,$(WEBOTS_HOME))))
RESOURCES_PATH = $(WEBOTS_HOME)/projects/robots/robotis
CXX_SOURCES = $(wildcard *.cpp)
include $(WEBOTS_HOME_PATH)/resources/Makefile.include
I make it successfully but when I want to run it I face a runtime error about Qt :
[co] qt.qpa.plugin: Could not find the Qt platform plugin "xcb" in ""
[co] This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
I really need help.

CMake tests fails but if just run testing executable so everything is ok

I'm using mingw (4.9.1) that was stored with last Qt official build. CMake version is 3.1.2. The situation is when i called make test so i got failed tests with exceptions, but, if i just run this test *.exe by manually — everything is ok, tests running!
What can be wrong?
And yes, I checked the libs its liked with...
There are these files...
CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
CMAKE_POLICY(SET CMP0020 NEW)
#ENABLE_TESTING( true )
SET( CMAKE_AUTOMOC ON )
SET( CMAKE_INCLUDE_CURRENT_DIR ON )
SET(CMAKE_PREFIX_PATH $ENV{QTDIR})
FIND_PACKAGE( Qt5Core REQUIRED )
FIND_PACKAGE( Qt5Test REQUIRED )
ADD_EXECUTABLE( coretest coretest.cpp )
ADD_TEST( coretest coretest )
QT5_USE_MODULES( coretest Core Test )
TARGET_LINK_LIBRARIES( coretest ${QT_LIBRARIES} )
And single test code file (coretest.cpp):
#include <QTest>
class Coretest : public QObject {
Q_OBJECT
private slots:
void t1();
};
void Coretest::t1()
{
QVERIFY(true);
}
QTEST_MAIN(Coretest)
#include "coretest.moc"
Well, I've got the solution. Just forget to check Path var...

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'

incompatible qt library between app and plugin compiled same way

After compiling a Qt application and a Qt plugin with the same flags (except -shared added for compiling the .dll) there is an error message at execution:
"The plugin 'Z:/bug_pyqt/plugin.dll' uses incompatible Qt library. Expected build key "Windows mingw release full-config", got "Windows mingw debug full-config""
Why this error message if both the main application and the plugin were compiled with the same flags?
They were compiled with mingw32-g++ on windows XP under cygwin with a hand-made makefile.
Compiling the main application with option "-g" (and plugin still without) makes things "work" and the error message disappear but what is the rationale?
File main.cpp to compile into a.out:
#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();
exit(1);
}
(void)loader.instance();
return app.exec();
}
File plugin.h:
#ifndef PLUGIN_H
#define PLUGIN_H
#include <QObject>
class Plugin : public QObject
{
Q_OBJECT
public:
Plugin();
~Plugin();
};
#endif
File plugin.cpp to compile into plugin.dll:
#include "plugin.h"
#include <QtPlugin>
Q_EXPORT_PLUGIN2(Plugin, Plugin)
Plugin::Plugin() { }
Plugin::~Plugin() { }
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
LIBFLAGS=-shared
all:
$(MOC) plugin.h > plugin_moc.cpp
$(GCC) -o a.out main.cpp $(FLAGS)
$(GCC) -o plugin.dll $(LIBFLAGS) plugin_moc.cpp plugin.cpp $(FLAGS)
After investigation, here is a half answer
First off, the "build key" that is mentionned in the error message in defined as macro QT_BUILD_KEY in file QtCore/qconfig.h.
Here is a relevant extract of this file:
#if defined(__SYMBIAN32__)
# define QT_BUILD_KEY "Symbian full-config"
#else
# if !defined(QT_NO_DEBUG)
# if (defined(WIN64) || defined(_WIN64) || defined(__WIN64__))
# define QT_BUILD_KEY "Windows x64 mingw debug full-config"
# else
# define QT_BUILD_KEY "Windows mingw debug full-config"
# endif
# else
# if (defined(WIN64) || defined(_WIN64) || defined(__WIN64__))
# define QT_BUILD_KEY "Windows x64 mingw release full-config"
# else
# define QT_BUILD_KEY "Windows mingw release full-config"
# endif
# endif
#endif
From this we understand that we can force the build type of the plugin to be "release" by defining the macro QT_NO_DEBUG.
Adding "-DQT_NO_DEBUG" on the compile command for the plugin solves the issue.
This still does not explain why by default Qt_BUILD_KEY (or QT_NO_DEBUG) is different between the main app and the plugin.

Resources