Qt Console Application - Terminal Won't Stay Open [In Ubuntu] - qt

I recently began developing in Qt, but I am having an issue with console projects. When I run my program (Console Program), terminal opens and then immediately closes so I cannot see the output.
Here is the code that I am using:
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "In the stream";
return a.exec();
}
I looked in my project’s .pro file and it seems to be setup correctly (it is configured to use the console with CONFIG += console). I am not sure why the terminal window won’t stay open. I have tried adding system("PAUSE)
This does not work however. I am using Ubuntu and the default Qt Creator settings.
When I run the project I use the default run button, but I have also tried the debug button. The terminal window still just flashes open and then closes before I can see the output. The Application Output pane at the button just says that it is starting the application. This is what my terminal settings look like in QtCreator: x-terminal-emulator -e (this is default)
My QtCreator version is 2.4.1
Could someone tell me how to keep the console/terminal open so I can see the qdebug() output?
Thanks

Install xterm (sudo apt-get install xterm)
Go to project's run settings and check 'Run in terminal'
Now your console application will use xterm to show itself, but it will fail to debug. The other - safe - way to see console output is to open 'Application Output' (Alt+3). Though it cannot be closed by Ctrl+c (you'll have to use 'stop' button in QtCreator).

I've had this problem multiple times before, and it usually means that you're missing a dependency (which is not uncommon on Linux).
How to solve this:
Run the executable from the command line. It usually outputs an error message, that could give you additional info onto what the problem actually is
If that doesn't work or you need more info, use the ldd command to check dependencies:
ldd < path to your executable >
This will display all the dependencies required and their location - and will also tell you if it can't find one
Once you find the missing dependency (if that's the problem), you can most certainly install it with apt or apt-get

Related

Program installed with CMake /CPack / NSIS64 cannot find Qt plugins

I am trying to install a C++/Qt/Qml program with CMake. I tried the IFW generator, but I'd rather stick to NSIS64.
Setup install file is correctly generated, and program is correctly installed. But my installed Qt program crashes right away, because
qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""
It is an easy to solve problem when running the program from the command line: we just need to set QT_QPA_PLATFORM_PLUGIN_PATH to the current directory (I installed/copied qwindows.dll there).
It is strange that the .exe does not look into the current program directory for Qt plugins. Anyways, a way to solve my problem would be: how to set an environment variable in a shortcut .lnk (created by NSIS)?
I have tried a bunch of things with NSIS (added in NSIS.template.in):
CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\#CPACK_NSIS_DISPLAY_NAME#.lnk" "QT_QPA_PLATFORM_PLUGING_PATH=$INSTDIR\bin $INSTDIR\bin\#CPACK_NSIS_DISPLAY_NAME#.exe"
CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\#CPACK_NSIS_DISPLAY_NAME#_cmd.lnk" \
"C:\Windows\System32\cmd.exe" "/c SET QT_QPA_PLATFORM_PLUGING_PATH=$INSTDIR\bin && start $INSTDIR\bin\#CPACK_NSIS_DISPLAY_NAME#.exe"
but this does not compile.
Maybe there is a different way to achieve this. Even if that would work, I would like to avoid setting QT_QPA_PLATFORM_PLUGING_PATH globally during the install, if possible, and restrain it to the program launch.
NOTE: documentation says:
CreateShortcut "$SMPROGRAMS\My Company\My Program.lnk" "$INSTDIR\My Program.exe"
"some command line parameters" "$INSTDIR\My Program.exe" 2 SW_SHOWNORMAL
ALT|CONTROL|SHIFT|F5 "a description"
but I am not too sure about how to use this in my case.

Command run in terminal but not in QProcess while PATHs are inserted

I want to run a command in Ubuntu via Qt using QProcess.
My command is fluent3DMeshToFoam <mesh address>
and when I run it into terminal its OK and produces sum output and files in a specific location. But I have problem with running it using QProcess.
I have noticed that I should add the path of fluent3DMeshToFoam to ProcessEnvironment of my QProcess object. So I did:
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("PATH" , "<path of fluent3DMeshToFoam>");
myProcess.setProcessEnvironment(env);
myProcess.start("fluent3DMeshToFoam" , QStringList() << "<mesh address>");
myProcess.waitForFinished(-1);
I connected readyRead() and errorOccurred() signals and after I run, the errorOccurred signal emits and the following error shows:
execve: No such file or directory
I searched alot and could not find out where the problem is.
thanks.
After a-lot of searches, finally I found-out that there was a problem with LD_LIBRARY_PATH which does not considered by Qt.
Without any using of setProcessEnvironment(...), I open terminal and export the LD_LIBRARY_PATH just before running it as follows:
export LD_LIBRARY_PATH
exec "./my_app"
Then my_app is aware of all libraries stored LD_LIBRARY_PATH

How do I get the system QFileDialog in Qt5?

I created file dialogs to select files within a Qt application. When migrating to Qt5 I encounter a regression, the file dialog is much poorer when linking against Qt5 rather than Qt4:
Qt4
Qt5
For example it is very hard for an user to navigate to the network mounted share directories.
The code is as simple as this:
QString path;
path = QFileDialog::getExistingDirectory(this, tr("Pick a file"));
QFileDialog::DontUseNativeDialog does not change anything.
My OS is Ubuntu 16.04, I have both nautilus (3.14.3) and nemo (2.8.7) installed. nemo is configured to be the default file manager:
xdg-mime default nemo.desktop inode/directory application/x-gnome-saved-search
Every other application seems to use the same dialog as the Qt4 file dialog screenshot.
How do I get the "old" file dialog in Qt5?
If I try with QT_QPA_PLATFORMTHEME= ./my_app then I get this:
The following works for me on Ubuntu 16.04 – it is more of a workaround than an actual solution, though:
Edit: First make sure that the package libqt5libqgtk2 is installed. It brings the GTK2 bindings for Qt5. If the package is not installed, the file choosers will look like the last screenshot shown in the question above.
Then, assuming you want to launch your application my_app from a terminal, launch it as follows (mind the space after =):
$ QT_QPA_PLATFORMTHEME= my_app
This is because, according to a bug report on launchpad, there seems to be a problem with the Ubuntu package appmenu-qt5: the package forces Qt5 dialogs to become non-native as a side effect of explicitly setting QT_QPA_PLATFORMTHEME=appmenu-qt5 via /etc/profile.d/appmenu-qt5.sh. Unsetting the platform theme via QT_QPA_PLATFORMTHEME= before launching your application changes this behavior locally.
Edit: When using the described approach, however, the global menu might not work in my_app.

QtCreator cannot find stddef.h working on linux

I just freshly installed Qt Creator 4.1.0
Based on Qt 5.7.0 (GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 64 bit)
This error drives me crazy after I fixed all kit errors, actually I spent some time on google looking for proper compiler. This could be something with compiler version I think:
/usr/include/c++/5.3.1/cstddef:45: error: stddef.h: No such file or
directory
I saw solutions for windows everywhere but could not find for linux, does anyone has experience how to fix it?
Screenshots
rpm -qa | grep devel | sort
expat-devel-2.1.0-10.fc22.x86_64
fontconfig-devel-2.11.94-4.fc22.x86_64
freetype-devel-2.5.5-2.fc22.x86_64
gdbm-devel-1.11-4.fc22.x86_64
glibc-devel-2.21-13.fc22.i686
glibc-devel-2.21-13.fc22.x86_64
java-1.8.0-openjdk-devel-1.8.0.65-3.b17.fc22.x86_64
kernel-debug-devel-4.2.5-201.fc22.x86_64
kernel-debug-devel-4.2.6-200.fc22.x86_64
kernel-devel-4.2.6-200.fc22.x86_64
libdb-devel-5.3.28-12.fc22.x86_64
libdrm-devel-2.4.61-3.fc22.x86_64
libICE-devel-1.0.9-2.fc22.x86_64
libpng-devel-1.6.16-3.fc22.x86_64
libSM-devel-1.2.2-2.fc22.x86_64
libstdc++-devel-5.3.1-6.fc22.i686
libstdc++-devel-5.3.1-6.fc22.x86_64
libXau-devel-1.0.8-4.fc22.x86_64
libxcb-devel-1.11-8.fc22.x86_64
libXcursor-devel-1.1.14-4.fc22.x86_64
libXdamage-devel-1.1.4-6.fc22.x86_64
libXext-devel-1.3.3-2.fc22.x86_64
libXfixes-devel-5.0.1-4.fc22.x86_64
libXft-devel-2.3.2-2.fc22.x86_64
libXi-devel-1.7.4-2.fc22.x86_64
libXinerama-devel-1.1.3-4.fc22.x86_64
libXrandr-devel-1.4.2-2.fc22.x86_64
libXrender-devel-0.9.9-1.fc22.x86_64
libxshmfence-devel-1.2-1.fc22.x86_64
libXt-devel-1.1.4-10.fc22.x86_64
libXv-devel-1.0.10-2.fc22.x86_64
libXxf86vm-devel-1.1.4-1.fc22.x86_64
libX11-devel-1.6.3-1.fc22.x86_64
mesa-libGL-devel-10.6.9-1.20151008.fc22.x86_64
mesa-libGLU-devel-9.0.0-7.fc22.x86_64
perl-devel-5.20.3-328.fc22.x86_64
python3-devel-3.4.2-6.fc22.x86_64
qt-devel-4.8.7-4.fc22.x86_64
systemtap-devel-2.9-1.fc22.x86_64
systemtap-sdt-devel-2.9-1.fc22.x86_64
xorg-x11-proto-devel-7.7-12.fc21.noarch
zlib-devel-1.2.8-7.fc22.x86_64
I had this problem as well, and for me I had to disable ClangCodeModel plugin. To do this, I went to Help->About Plugins...->Uncheck ClangCodeModel (under C++ category). After restarting QT Creator, everything was working fine. I found this solution on QT Forums
If the compiler works from a terminal, it should work from QtCreator. So first make it work from the terminal.
From a terminal, create a file main.cpp with:
#include <cstddef>
int main()
{
return 0;
}
Then type the command:
gcc -H main.cpp -o main
If the compilation works, there must be a problem in your kit definition in QtCreator. Possibly make sure the folder where stddef.h is in the include file list of your kit. But you should not need to do that. Temporarily, you can add the include path to your project (see How to add include path in Qt Creator?) to check this is really the problem. Then you'll need to find a way to fix the kit for good.
If the compilation does not work, fix your environment (possibly using sashoalm's commented link). QtCreator is just and IDE, if you run it with a broken compiler it won't fix it for you. The compilation output will show where files (cstddef and then stddef.h) are picked from (-H option). Try cpp -v to see where included files are searched.
On my machine, compilation output gives me:
. /usr/include/c++/4.8/cstddef ..
/usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h ...
/usr/include/x86_64-linux-gnu/c++/4.8/bits/os_defines.h ....
/usr/include/features.h .....
/usr/include/x86_64-linux-gnu/sys/cdefs.h ......
/usr/include/x86_64-linux-gnu/bits/wordsize.h .....
/usr/include/x86_64-linux-gnu/gnu/stubs.h ......
/usr/include/x86_64-linux-gnu/gnu/stubs-64.h ...
/usr/include/x86_64-linux-gnu/c++/4.8/bits/cpu_defines.h ..
/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h Multiple include
guards may be useful for: /usr/include/c++/4.8/cstddef
/usr/include/x86_64-linux-gnu/bits/wordsize.h
/usr/include/x86_64-linux-gnu/gnu/stubs-64.h
/usr/include/x86_64-linux-gnu/gnu/stubs.h
/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
see stddef.h was found in /usr/lib/gcc/x86_64-linux-gnu/4.8/include
And cpp -v gives:
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/4.8/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
Note: If you created the kit manually, I would recommend that you uninstall QtCreator. Make gcc work from a terminal and then reinstall QtCreator. This one should create the kits automatically and they should then work.
This is a common problem with QT I experience a lot with all versions of Fedora, the problem is that the path to "/usr/lib64/qt5/bin" is missing from the user environment.
To check for the path at a command line what the path is present set to enter the following: echo $PATH
You should see the "/usr/lib64/qt5/bin" listed somewhere in the response if it is not present then edit the .bashrc file in the root of your home directory as follows: -
Add QT5 build environment
if! [[ "$PATH" =~ "/usr/lib64/qt5/bin" ]]
then
PATH="/usr/lib64/qt5/bin:$PATH"
fi
Just above the "export PATH" statement
You will need to log off and on again for the changes to take effect.

Eclipse CDT output does not show in RUN mode only

CDT 8.4
Eclipse Luna 4.4
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
MinGW
The Environment variables are set correctly.
The project compiles without error.
Release and Debug executeables execute via the windows console.
#include <iostream>
using namespace std;
int main()
{
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
When running the "Debug Configuration" in the eclipse IDE i get the expected output:
When running the "Run Configuration" in the eclipse i get an unexpected output:
There is no difference in the two configurations, they are the same configuration run in different modes.
Everything is a fresh installation.
What am i missing, how can i get the "Run Configuration" to display the output correctly?
I found another answer on stackoverflow. I feel like this problem is simmilar with the difference that. My paths are correct and i can run the programs outside of eclipse just fine. I will try to apply all the solutions but i think the answer here will be something different.
EDIT:
I added JVM to the tags.
The solution may have to do with restarting the JVM.
Does the JVM cache the environment variables?
I got back to my computer this morning without having changed anything and the output started working.
My guess is that a restart is required after you add something to the path variable.
This is a bit weird in the modern age - but restarting did the trick.
This would also explain the reports that reinstalling helps, since people tend to restart after reinstalling something like the jvm.

Resources