How to change console mode to gui mode in QtCreator? - c++

I am trying to convert an application console mode to gui mode but the problem is the console window still appearing with main window.What i want is to hide console window as it dose in gui mode.
.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = cmd
TEMPLATE = app
CONFIG -= console
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainfile
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

In creator go in project under tab Run Settings uncheck Run in terminal. cmd window will not appear.

Related

Qmake, setting up environment

I am following this qt tutorial.
helloworld.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = helloworld
TEMPLATE = app
SOURCES += main.cpp
main.cpp
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton button ("Hello world!");
button.show();
return a.exec(); // .exec starts QApplication and related GUI, this line starts 'event loop'
}
Type the following shell commands to build the program.
qmake -project
Error:
PS E:\QT_tut\Tut_cmake_1\nSR> qmake -project
Project ERROR: Cannot run compiler 'cl'. Output:
===================
===================
Maybe you forgot to setup the environment?
PS E:\QT_tut\Tut_cmake_1\nSR>
QT: 5.12.12
VS: 2019
But, I can run this program using QT GUI.

QT C++ application using QGIS API

I'm trying to develop a QT C++ application that uses QGIS API and I can't manage Qt Creator to compile the code. I've read several topicts at gis.stackexchange and from other sources. The information is mostly outdated and I havent't found a solution. What I need is an overall guideline about the compatibility of the following items that help me find the right combination:
Qt version: tried 4.8.6 and 5.9.
Qt compiler: tried MinGW, VS2008 Express (Qt 4.8.6) and VS Community 2017 (Qt 5.9)
QGIS version: tried 2.14 to 2.18.
QGIS compile: I only use the regular OSGeo4w installer and AFAIK QGIS is compiled with Qt4 and MSVC. I've tried to compile QGIS from source using MinGW with no success (again, versions compatibility issues).
Windows version: I'm using Windows 10 64bit.
OSGeo4W or OSGeo4W64.
Qt works Ok in every case with it's own libraries, but when I try to import QGIS libraries it gives an 'undefined reference' error when building, even though intellisense works fine as it recognizes QgsApplication as a member of qgsapplication.h.
The following is just a test I can't get working. My Pro file is:
QT += core gui xml
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QgisWindow
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# QGIS
INCLUDEPATH += "C:/OSGeo4W64/apps/qgis-rel-dev/include"
INCLUDEPATH += "C:/OSGeo4W64/include"
LIBS += -L"C:/OSGeo4W64/apps/qgis-rel-dev/lib" -lqgis_core -lqgis_gui
DEFINES+=CORE_EXPORT=
DEFINES+=GUI_EXPORT=
# QGIS
main.cpp is:
#include <QApplication>
#include <qgsapplication.h>
int main(int argc, char *argv[])
{
QgsApplication app(argc, argv, true);
QgsApplication::setPrefixPath("C:/OSGeo4W64/apps/qgis-dev", true);
int retval = app.exec();
QgsApplication::exitQgis();
return retval;
}
Then it fails to build/compile whith this error:
main.cpp:20: error: undefined reference to `QgsApplication::QgsApplication(int&, char**, bool, QString const&, QString const&)'
I'm actually porting a PyQgis app I've made two years ago and as I'm fairly new to Qt Creator I'm stuck at he beginning.
I found the right combination of the above mentioned items to make it possible for Qt to import Qgis libraries and build the app:
Qt 4.8.6 for MSVC2010
Qt Creator 4.3.0 (from a Qt 5.9.0 installation). Qt Creator 3.0 may be used as well.
Visual C++ express 2010 - Include Windows Kits/SDK in install
OSGeo4W - Installed qgis-ltr-dev and all libraries
Cmake 2.8.2
To launch Qt Creator I use this .bat:
Creator.bat:
ECHO Setting up QGIS DEV ENV
set PYTHONPATH=
set OSGEO4W_ROOT=C:\OSGeo4W
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
#set QMAKESPEC=win32-msvc2010
#set PATH=%OSGEO4W_ROOT%\bin;%OSGEO4W_ROOT%\apps\qgis-ltr-dev\bin;%PATH%
#set INCLUDE=%INCLUDE%;%OSGEO4W_ROOT%\include;%OSGEO4W_ROOT%\apps\qgis-ltr-dev\include
#set LIB=%LIB%;%OSGEO4W_ROOT%\lib;%OSGEO4W_ROOT%\apps\qgis-ltr-dev\lib
path %OSGEO4W_ROOT%\bin;%SYSTEMROOT%\System32;%SYSTEMROOT%;%SYSTEMROOT%\System32\wbem;C:\Program Files (x86)\Git\bin;C:\Tools\QtCreator\bin;%PATH%
set VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
start "Qt Creator" /B C:\Qt\Qt5.9.0\Tools\QtCreator\bin\qtcreator.exe %*
The PRO file:
QT += core gui xml
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 1_hello_world_qgis_style
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp
## QGIS
INCLUDEPATH += "C:/OSGeo4W/include"
INCLUDEPATH += "C:/OSGeo4W/apps/qgis-ltr-dev/include"
LIBS += -L"C:/OSGeo4W/apps/qgis-ltr-dev/lib" -lqgis_core -lqgis_gui
DEFINES+=CORE_EXPORT=
DEFINES+=GUI_EXPORT=
## QGIS
main.cpp file (adapted from Tim Sutton's examples)
// Qt Includes
#include <QApplication>
// QGIS Includes
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgssinglesymbolrendererv2.h>
#include <qgsmaplayerregistry.h>
#include <qgsvectorlayer.h>
#include <qgsmapcanvas.h>
int main(int argc, char ** argv)
{
// Start the Application
QgsApplication app(argc, argv, true);
app.setPrefixPath("D:/GIS");
QString myPluginsDir = "<path to Qgis plugins dir>";
QString myLayerPath1 = "<path to shapefile 1>";
QString myLayerBaseName1 = "Layer1";
QString myLayerPath2 = "<path to shapefile 2>";
QString myLayerBaseName2 = "Layer2";
QString myProviderName = "ogr";
// Instantiate Provider Registry
QgsProviderRegistry::instance(myPluginsDir);
// create maplayer instances
QgsVectorLayer * mypLayer1 = new QgsVectorLayer(myLayerPath1, myLayerBaseName1, myProviderName);
QgsVectorLayer * mypLayer2 = new QgsVectorLayer(myLayerPath2, myLayerBaseName2, myProviderName);
QgsSingleSymbolRendererV2 *mypRenderer1 = new QgsSingleSymbolRendererV2(QgsSymbolV2::defaultSymbol(mypLayer1->geometryType()));
QgsSingleSymbolRendererV2 *mypRenderer2 = new QgsSingleSymbolRendererV2(QgsSymbolV2::defaultSymbol(mypLayer2->geometryType()));
QList <QgsMapCanvasLayer> myLayerSet;
mypLayer1->setRendererV2(mypRenderer1);
mypLayer2->setRendererV2(mypRenderer2);
// Add Vector Layers to the Layer Registry
QgsMapLayerRegistry::instance()->addMapLayer(mypLayer1, true);
QgsMapLayerRegistry::instance()->addMapLayer(mypLayer2, true);
// Add Layers to the Layer Set
myLayerSet.append(QgsMapCanvasLayer(mypLayer1, true));
myLayerSet.append(QgsMapCanvasLayer(mypLayer2, true));
// Create the Map Canvas
QgsMapCanvas * mypMapCanvas = new QgsMapCanvas(0, 0);
mypMapCanvas->setExtent(QgsRectangle(-63.50, -28.10, -58.33, -24.00)); // Chaco
mypMapCanvas->setWheelAction(QgsMapCanvas::WheelAction(2) , 1.2);
mypMapCanvas->enableAntiAliasing(true);
mypMapCanvas->setCanvasColor(QColor(255, 255, 255));
mypMapCanvas->freeze(false);
// Set the Map Canvas Layer Set
mypMapCanvas->setLayerSet(myLayerSet);
mypMapCanvas->setVisible(true);
mypMapCanvas->setWindowTitle("GIS DSH");
mypMapCanvas->refresh();
// Start the Application Event Loop
int retval = app.exec();
app.exitQgis();
return retval;
}
Paths are hardcoded (bad programming practice) but is just for this test.
And now it runs OK. Please, see my post on gis.stackexchange.com for the links and the screencaps of Qt Creator Build & Run configurations.

QSound Unexpected null receiver while playing .wav files

I would like to play .wav samples of engine through out Qt creator. Of course the very first thought was on QSound class, but I did whatever was necessary to play it and it always shows me
QCoreApplication::postEvent: Unexpected null receiver
Which means that I entered wrong path for file, it seems simple but I have tried with absoulte paths and etc. Nothing new has happened.
Sourcecode and photos, I am trying it on windows but I would like to run it on Raspberry(fedora).
#include <QCoreApplication>
#include <QSound>
#include <iostream>
#include <QMediaPlayer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSound let("music/letitplay.wav");
let.play();
/*QMediaPlayer * music = new QMediaPlayer();
music->setMedia(QUrl("qrc:/sounds/letitplay.wav"));
music->play();
*/
return 0;
}
Snippet from .pro
QT += core
QT -= gui
QT += multimedia
TARGET = silnik1
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
RESOURCES +=
Screen of path and file directory
QSound plays sounds asynchronously; you're going to need to start your QCoreApplication first so there's an event loop running.

QT - PostgreSQL plugin error

I have an app, that should connect to database.
Under linux i've no problems, but under windows - > i've to add to system "Path" place, where QT enviroment is placed (C:/QT/Tools/QTCreator/bin) otherwise it doesn't work and shows errors like below.
id doesn't work if i place exe file on pendrive too
i would like to send this app to my firends, who shouldn't install QT environment and just run this app.
dirs on my pendrive
/
app.exe
plugins (dir)
-qsqlpsql.dll
-sqldrivers (dir)
--qsqlpsql.dll
what should i do? where did i mistake?
pro file:
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = NZP
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32{
LIBS += -L"C:/Program Files/psqlODBC/0903/bin"
}
RC_FILE += Info.rc
in my main.cpp:
QApplication a(argc, argv);
qDebug()<<QCoreApplication::applicationDirPath();
a.addLibraryPath(QCoreApplication::applicationDirPath());
a.addLibraryPath(QCoreApplication::applicationDirPath()+"/plugins");
qDebug()<<a.libraryPaths();
MainWindow w;
w.show();
return a.exec();
if i run this app under windows, where i installed QT - everythink is correct, but if i move it on pendrive or run under machine without QT framework - i become error:
QSqlDatabase: QPSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QODBC3 QODBC QPSQL QPSQL7
Failed to open database connection. Invalid driver specfied.
i have looked in internet, but by now without sensible solution
help!
Ugh! i found the problem!
1.
you have in your install destination this folder structure:
yourap.exe
-plugins -> folder
--sqldrivers -> folder
and you have to copy from migws/bin not QT/Tools/Creator/bin!
---qsqlpsql.dll
in your app directory you have to copy
libeay32.dll
with you found in:
C:\Program Files\psqlODBC\0903\bin (http://www.postgresql.org/ftp/odbc/versions/msi/ - ii used https://ftp.postgresql.org/pub/odbc/versions/msi/psqlodbc_09_03_0300.zip)
in your app (main.cpp), just add path:
QApplication a(argc, argv);
a.addLibraryPath(QCoreApplication::applicationDirPath()+"/plugins/");
a.addLibraryPath(QCoreApplication::applicationDirPath());

Add Settings entry for iOS app using Qt 5.4

I have built an iOS 8.2 app using Qt 5.4. I want to add a user-entry field in the Settings app like a lot of other apps do, but I can't figure out how to do it. I tried the following (which doesn't work):
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
TDA::TapDisplaySingleton* mainClass =
new TDA::TapDisplaySingleton(&app);
QObject::connect(&app, SIGNAL(aboutToQuit()),
mainClass, SLOT(aboutToQuitApp()));
QGuiApplication::setOrganizationName("NASA Langley");
QGuiApplication::setOrganizationDomain("larc.nasa.gov");
QGuiApplication::setApplicationName("TAP Display");
QSettings settings;
settings.setValue("HOSTNAME", "localhost");
QTimer::singleShot(10, mainClass, SLOT(run()));
return app.exec();
}
When I fire the app up in the Simulator and open the Settings app, my entry does not appear. Does anyone know how to do this effectively in iOS using Qt?
As it turns out, Qt does provide a way to generate a Settings.bundle file for your app. You need to define a Root.plist and Root.strings file in advance and then add the following to your .PRO file:
SETTINGS_BUNDLE_DATA_ROOT.files += Root.plist
SETTINGS_BUNDLE_DATA_ROOT.path = Settings.bundle
SETTINGS_BUNDLE_DATA_LANG.files += Root.strings
SETTINGS_BUNDLE_DATA_LANG.path = Settings.bundle/en.lproj
QMAKE_BUNDLE_DATA += SETTINGS_BUNDLE_DATA_ROOT SETTINGS_BUNDLE_DATA_LANG
When your project is linked, the bundle will be created.
QSettings is only for storing settings in a persistent fashion. Adding settings to the iOS Settings application is outside of the scope of Qt. Look up "iOS Settings Bundle" for information on how to create those - eg https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html .