netbeans: how to add widgets to QT variable - qt

I have seen posts in which people say that Qt 5 projects are now required to add widgets to QT variable and this is why simple Qt example in netbeans doesn't work: cannot include QtGui/QApplication -> no such file or directory.
how to add this to QT variable? do I have to do it for each project in IDE or in .bashrc or somehowe?

the problem was because of link error. it was because it didn't link with QtWidgets.
g++ -m64 -Wl,-rpath,/opt/Qt5.0.1/5.0.1/gcc_64 -Wl,-rpath,/opt/Qt5.0.1/5.0.1/gcc_64/lib -o dist/Debug/GNU-Linux-x86/QtApplication_2 build/Debug/GNU-Linux-x86/main.o -L/usr/X11R6/lib64 -L/opt/Qt5.0.1/5.0.1/gcc_64/lib -lQt5Gui -lQt5Core -lGL -lpthread
first I have checked that using something from QtCore still works. the code was:
#include <QtCore/QCoreApplication>
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QCoreApplication a(argc, argv);
// create and show your widgets here
return a.exec();
}
and it was fine, so I knew it is only the linkage error.
I still didn't know what to add, as you don't have QtWidget in Qt tab on project Properties in Netbeans. but trial and error showed that it is enough to check the QtOpenGl on this tab (Modules), then it is linked against more libs:
g++ -m64 -Wl,-rpath,/opt/Qt5.0.1/5.0.1/gcc_64 -Wl,-rpath,/opt/Qt5.0.1/5.0.1/gcc_64/lib -o dist/Debug/GNU-Linux-x86/QtApplication_1 build/Debug/GNU-Linux-x86/main.o -L/usr/X11R6/lib64 -L/opt/Qt5.0.1/5.0.1/gcc_64/lib -lQt5OpenGL -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread

Related

QML console.log() and console.debug() don't write to console

I'm using Qt 5.6 on Fedora 23 and I noticed that console.log() and console.debug() don't write anything to console. My example code:
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
Component.onCompleted: {
console.warn("warn completed")
console.log("log completed")
console.error("error completed")
console.debug("debug completed")
console.exception("exception completed")
console.info("info completed")
}
}
}
prints to console:
QML debugging is enabled. Only use this in a safe environment.
qml: warn completed
qml: error completed
qml: exception completed
onCompleted (qrc:/main.qml:16)
qml: info completed
so warn, error, exception, and info work fine. What am I doing wrong?
Edit #1:
Project is freshly created, all my sources:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
project.pro
TEMPLATE = app
QT += qml quick
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
Edit #2:
Compile Output from Qt Creator shows that there are no QT_NO_DEBUG_OUTPUT, QT_NO_INFO_OUTPUT, or QT_NO_WARNING_OUTPUT:
14:43:36: Running steps for project project...
14:43:36: Configuration unchanged, skipping qmake step.
14:43:36: Starting: "/usr/bin/make"
g++ -c -pipe -g -std=gnu++0x -Wall -W -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../project -I. -I../../Qt5.6.0/5.6/gcc_64/include -I../../Qt5.6.0/5.6/gcc_64/include/QtQuick -I../../Qt5.6.0/5.6/gcc_64/include/QtGui -I../../Qt5.6.0/5.6/gcc_64/include/QtQml -I../../Qt5.6.0/5.6/gcc_64/include/QtNetwork -I../../Qt5.6.0/5.6/gcc_64/include/QtCore -I. -I../../Qt5.6.0/5.6/gcc_64/mkspecs/linux-g++ -o main.o ../project/main.cpp
/home/krzys/Qt5.6.0/5.6/gcc_64/bin/rcc -name qml ../project/qml.qrc -o qrc_qml.cpp
g++ -c -pipe -g -std=gnu++0x -Wall -W -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../project -I. -I../../Qt5.6.0/5.6/gcc_64/include -I../../Qt5.6.0/5.6/gcc_64/include/QtQuick -I../../Qt5.6.0/5.6/gcc_64/include/QtGui -I../../Qt5.6.0/5.6/gcc_64/include/QtQml -I../../Qt5.6.0/5.6/gcc_64/include/QtNetwork -I../../Qt5.6.0/5.6/gcc_64/include/QtCore -I. -I../../Qt5.6.0/5.6/gcc_64/mkspecs/linux-g++ -o qrc_qml.o qrc_qml.cpp
g++ -Wl,-z,origin -Wl,-rpath,\$ORIGIN -Wl,-rpath,/home/krzys/Qt5.6.0/5.6/gcc_64/lib -o project main.o qrc_qml.o -L/home/krzys/Qt5.6.0/5.6/gcc_64/lib -lQt5Quick -lQt5Gui -lQt5Qml -lQt5Network -lQt5Core -lGL -lpthread
14:43:37: The process "/usr/bin/make" exited normally.
14:43:37: Elapsed time: 00:01.
Fedora 22 and later disables Qt debug output by default [1]. You can enable Qt debug output by modifying the system-wide /etc/xdg/QtProject/qtlogging.ini or by creating a user-specific configuration file ~/.config/QtProject/qtlogging.ini for example with the following contents:
[Rules]
*.debug=true
https://bugzilla.redhat.com/show_bug.cgi?id=1227295
I found a more convenient solution by adding one of the two - depending
on if you use Qt Quick 1 or 2 to the pro file (rebuild and run QMake again)
according to https://doc.qt.io/qt-5/qtquick-debugging.html:
Qt Quick 1: CONFIG+=declarative_debug
Qt Quick 2: CONFIG+=qml_debug
To put it in your code it would look like:
project.pro
TEMPLATE = app
QT += qml quick
CONFIG += c++11 qml_debug #or CONFIG+=declarative_debug for QtQuick 1
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
Now it might look like this:
Instead of enabling Debug logging system wide like jpnurmi suggests in the accepted answer. You can simply set a system variable to get the debug messages to show up in the console. Qt Wiki about Logging Rules
To set it for one single execution of your app or qmlscene/qhot qml-preview. If you are starting them from the commandline:
$ QT_LOGGING_RULES="*.debug=true; qt.*.debug=false" your_executable
$ QT_LOGGING_RULES="*.debug=true; qt.*.debug=false" qmlscene/qhot main.qml
Or to set it for this session only (will be unset after reboot). And should be respected even if you launch/test from QtCreator or other IDE:
$ export QT_LOGGING_RULES="*.debug=true; qt.*.debug=false"
And then just start/test your executable or qmlscene/qhot normally
Why is this better?
Setting the logging rules systemwide will mean all Qt apps you run will write debug messages to their logfiles. I discovered a few log files that were gigabytes in size because the apps were accumulating a huge amount debug messages in their logs during normal usage.

Qt linker errors Qmake + Makefile

I want to build a Qt project outside Qt Creator, so I'm using qmake to generate a Makefile on the following project file:
TEMPLATE = app
TARGET = test
INCLUDEPATH += . \
/usr/include/qt5/QtWidgets/
QMAKE_CXXFLAGS += --std=c++11
# Input
SOURCES += test.cc
Which was generated also by qmake, bar the c++11 flag and the second include path. The Makefile contains link paths to the Qt library
LIBS = $(SUBLIBS) -L/usr/X11R6/lib64 -lQt5Gui -L/usr/lib/x86_64-linux-gnu -lQt5Core -lG L -lpthread
What's weird in the above is that I don't have a /usr/X11R6 folder. Instead, libQt5Gui.so is located in /usr/lib/x86_64-linux-gnu, so I'm a little puzzled where the X11R6 comes from.
Anyway, this is my linker output:
test.cc:(.text.startup+0x20): undefined reference to `QApplication::QApplication(int&, char**, int)'
test.cc:(.text.startup+0x25): undefined reference to `QApplication::exec()'
test.cc:(.text.startup+0x2f): undefined reference to `QApplication::~QApplication()'
test.cc:(.text.startup+0x43): undefined reference to `QApplication::~QApplication()'
The above is the result of building the following source:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
return app.exec();
}
When I try to build the project file in Qt Creator, the same error appears. Am I missing libraries? Or is something configured erroneously?
(I'm on Ubuntu 14.04, and I just installed the qtcreator package from the repo's, assuming that all development libraries would be installed along with it.)
As stated in the docs, you need to include the widget library to use QApplication.
Add this to your project file:
QT += widgets
If you're not going to build a GUI app, use QCoreApplication instead. It doesn't have that dependency.

Simple Qt program builds but doesn't show output

I have just started learning Qt and tried to compile and run a simple program of hello world. The program builds without any issues and gives this output in compiler output
Starting: /qtbuild/bin/qmake /home/ved/Qt/train1/train1.pro -spec /qtbuild/mkspecs/qws/linux-arm-g++ -r CONFIG+=debug
Exited with code 0.
Starting: /usr/bin/make -w
make: Entering directory `/home/ved/Qt/train1'
make: Nothing to be done for `first'.
make: Leaving directory `/home/ved/Qt/train1'
Exited with code 0.
but on trying to run the program, it only displays this:
Starting /home/ved/Qt/train1/train1...
/home/ved/Qt/train1/train1 exited with code 255
My code:
#include
#include
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QLabel *label = new QLabel("Hello World!!!");
label->show();
return a.exec();
}
I am completely new to Qt building procedure and can't understand what is wrong.
Update
tried changing QCoreApplication to QApplication. No change.
Running build steps for project train1...
Starting: /qtbuild//bin/qmake /home/ved/Qt/train1/train1.pro -spec /qtbuild/mkspecs/qws/linux-arm-g++ -r CONFIG+=debug
Exited with code 0.
Starting: /usr/bin/make -w
make: Entering directory `/home/ved/Qt/train1'
arm-linux-g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED -I/qtbuild/mkspecs/qws/linux-arm-g++ -I. -I/qtbuild/include/QtCore -I/qtbuild/include/QtNetwork -I/qtbuild/include/QtGui -I/qtbuild/include -I. -I/usr/local/tslib-arm/include -o main.o main.cpp
In file included from /qtbuild/include/QtCore/qobject.h:48,
from /qtbuild/include/QtCore/qiodevice.h:46,
from /qtbuild/include/QtCore/qxmlstream.h:45,
from /qtbuild/include/QtCore/QtCore:3,
from main.cpp:1:
/qtbuild/include/QtCore/qstring.h:91: note: the mangling of 'va_list' has changed in GCC 4.4
arm-linux-g++ -Wl,-rpath,/qtbuild/lib -o train1 main.o -L/usr/local/tslib-arm/lib -L/qtbuild//lib -lQtGui -L/qtbuild//lib -L/usr/local/tslib-arm/lib -lQtNetwork -lQtCore -lpthread
make: Leaving directory `/home/ved/Qt/train1'
Exited with code 0.
I use Qt 4.6.3.
If you want a QLabel to display, you need to run the GUI application class QApplication, not QCoreApplication.
You should tell Qt, that you want to build project with GUI. Open your's project .pro file and change line
QT += ...
to
QT += core gui
example, .pro file:
QT += core gui
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp
main.cpp:
#include <QtGui/QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel lbl("hello world");
lbl.show();
return a.exec();
}
You need to create a window if you want to display a label. Basically something like this (not tested):
QMainWindow* win = new QMainWindow();
QLabel *label = new QLabel(win, "Hello World!!!");
label->show();
win->show();
change QCoreApplication to QApplication an add a Main Window
QApplication a(argc, argv);
QMainWindow* mainWin = new QMainWindow();
QLabel *label = new QLabel(mainWin, "Hello World!!!");
mainWin->setCentralWidget(label);
mainWin->show();
You must set in project configuration that you are compiling Qt GUI application. Using of QApplication instead QCoreApplication is not enough. I don't know your IDE, so i can't provide "howto" - but i belive that you will easly find necessary options. For eapmle, in MSVC, you set neccessary application type (console or GUI) during creation of a project.
Also - exit code 255 shows on some error. Exit code must be zero, exept cases, when you manually change it.
Try to unclick Shadow build in your Project/Build properties.
I having the same problem. Let it to be restart QT. Surely it works

QT Windows 7 helloworld program -> debug/main.o Error 1

I am trying to install and use QT framework on my Windows 7 Professional edition 32 bits machine. I downloaded whole pack (800+ mb) of QT and run installer.
AFter that I added to my system PATH two lines:
J:\Qt\Qt5.0.1\5.0.1\mingw47_32\bin;
J:\Qt\Qt5.0.1\Tools\MinGW\bin
After that I created empty project in QT with simple program, that QT created for me by itself. Applications -> QT gui application, "Use as default project location", Next, Next, Next.
And here is the code, that was created:
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:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
After that I hit Run button on beneath get following error:
19:23:09: Running steps for project qldt...
19:23:09: Configuration unchanged, skipping qmake step.
19:23:09: Starting: "J:\Qt\Qt5.0.1\Tools\MinGW\bin\mingw32-make.exe"
J:/Qt/Qt5.0.1/Tools/MinGW/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'J:/Users/shidant2/Desktop/workspace/qldt/qldt-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug'
g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -DQT_NEEDS_QMAIN -I..\qldt -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include" -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtWidgets" -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtGui" -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtCore" -I"debug" -I"." -I"." -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\mkspecs\win32-g++" -o debug\main.o ..\qldt\main.cpp
Makefile.Debug:357: recipe for target 'debug/main.o' failed
mingw32-make[1]: *** [debug/main.o] Error 1
mingw32-make[1]: Leaving directory 'J:/Users/shidant2/Desktop/workspace/qldt/qldt-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug'
makefile:34: recipe for target 'debug' failed
mingw32-make: *** [debug] Error 2
19:23:30: The process "J:\Qt\Qt5.0.1\Tools\MinGW\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project qldt (kit: Desktop Qt 5.0.1 MinGW 32bit)
When executing step 'Make'
As I understand, there no problem with the code, but with compiler.
I tried to look for solution, but did not find anything useful..
I guess such serious framework as QT has not to live with such problem, espicially when it impacts the beginner experience.
So, please, lets solve it here and now )
I found something interesting.
I tried to build everyhing withut QT:
I go to cmd and go there to folder:
cd J:/Users/shidant2/Desktop/workspace/qldt/qldt-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug
There I run this:
g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -DQT_NEEDS_QMAIN -I..\qldt -I"............\Qt\Qt5.0.1\5.0.1\mingw47_32\include" -I"............\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtWidgets" -I"............\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtGui" -I"............\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtCore" -I"debug" -I"." -I"." -I"............\Qt\Qt5.0.1\5.0.1\mingw47_32\mkspecs\win32-g++" -o debug\main.o ..\qldt\main.cpp
Then I get my main.o !
And if I try to build main.o from QT Creator, it can not build this main.o.
Creator creates empty main.o, hangs some time without doing single thing
and after that it gives error as I posted in first message.
I suspect this can be something with permissions for QT in Windows 7.
But still can not figure out what is the problem.
Any suggestions, guys?
Thank you.
http://www.qtforum.org/article/38999/error-compiling-wrong-slash-used-in-makefile-debug.html
Is there a problem with the slashes it is using?

QApplication: No such file or directory

I installed QT4 Creator in /usr/programs/qt , I add to PATH /usr/programs/qt/bin,
QTDIR=/usr/programs/qt,LD_LIBRARY_PATH=$QTDIR/lib, and also for MANPATH and export. Problem is that demo examples work fine, but when I create new project in other directory for example /home/Jane/ it doesn't work, I got errors like
/home/Jane/test-build-desktop/../test/main.cpp:1:
error: QApplication: No such file or
directory
/home/Jane/test-build-desktop/../test/main.cpp:2:
error: QLabel: No such file or
directory
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
Can anybody help me ?
Add to your .pro file:
QT += gui
I've the same problem. in my ".pro" file it was
QT -= gui
then I changed it to
QT += gui
and the problem solved
QApplication: No such file or directory ...
try to add
QT += widgets
at your .pro file. I had the same message... It looks like from 5.0 Qt-based applications does not like widgets by default...
The solution works for me, Qt 5.7
After added the following line to your .pro file:
QT += widgets
Right-click on your Qt Project and click "Run qmake"
Run qmake
After this when you re-complie your project, everything should be fine.
If your .pro file has this line:
QT -= gui
you need to delete it. It tells that the gui module to be removed from your app.
You can enable it by typing
QT += gui
but actually it is not needed since the gui module is enabled by default.
For Ubuntu 14.04 if you get the same error:
ABC$ make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I. -I/usr/include/qt5 -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o textpad.o textpad.cpp
textpad.cpp:1:24: fatal error: QApplication: No such file or directory
#include <QApplication>
^
compilation terminated.
make: *** [textpad.o] Error 1
Try qmake-qt4 and then make. Of course you can get all the QT4 libraries if its not present using:
sudo apt-get install libqt4-dev

Resources