Hourglass over Qt Creator breakpoint, program not breaking - qt

I have created a very simple Qt Creator console application, with defaults for everything except for the main.cpp file, which I have edited as follows:
#include <QCoreApplication>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout << "hello" << std::endl;
std::cout << "world" << std::endl;
return a.exec();
}
I have also added two breakpoints; one for each of the std::cout lines, by left-clicking on the margin to the left of the line number. This creates a red circle in the margin, with a small purple hourglass over the circle. When I build and run the application, the output is:
hello
world
and the program does not break at these lines in the IDE. Why not?

Have you try to click on "Start Debugging" (F5) instead of click on "Run" (Ctrl + R)?

#Trilarion: Antonio Borondo's question is justified. It appears as if the author of the original question actually hit the Run button (or pressed Ctrl-R, or ...) instead of hitting the Debug button (or F5, or ...). Getting a clarification on what he did is important to answer the original question.
As you noticed, commenting is not available to everybody, so asking in an "answer" is the only way forward.

In case anyone else still has this issue, this is most likely caused by the build not being set to Debug.
In Qt Creator 4.5.x, you can set the build type in the lower left hand corner, you will see an icon of a monitor, when you click on it, you will have a choice of Debug, Profile, or Release. When you set the build to Debug, you will no longer have hourglass icons on your breakpoints, and the program will break on these lines.

Related

Why does my Qt app ignore the setting of applicationDisplayName?

I am running a small app on KDE Plasma 5 created with Qt and the KDE framework. Almost everything works like a charm, just one part doesn't work. I just cannot set the application display name. I have the following code:
int main(int argc, char **argv) {
QApplication application(argc, argv);
KLocalizedString::setApplicationDomain("blender-render-control");
KCrash::initialize();
KAboutData aboutData(QStringLiteral("blender-render-control-center"),
i18n("Blender Render Control Center"),
QStringLiteral("1.0"),
i18n("A simple application to control the blender render control server"),
KAboutLicense::Custom,
i18n("Copyright 2019, Knerd "));
aboutData.addAuthor(i18n("Knerd"), i18n("Author"), QStringLiteral("knerd#knerd.knerd"));
aboutData.setOrganizationDomain("knerd.knerd");
aboutData.setDesktopFileName(QStringLiteral("knerd.knerd.blender-render-control"));
KAboutData::setApplicationData(aboutData);
QApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("knerd.knerd.blender-render-control")));
application.setApplicationDisplayName(i18n("Blender Render Control Center"));
application.setApplicationName(i18n("Blender Render Control Center"));
QCommandLineParser parser;
aboutData.setupCommandLine(&parser);
parser.process(application);
aboutData.processCommandLine(&parser);
auto *window = new MainWindow();
window->show();
return QApplication::exec();
}
From reading the docs and checking some examples, this should set the application title in my KDE environment. But it doesn't, the application name is the name of the executable.
Is this a bug in KDE or am I doing something wrong?
The docs are a bit confusing on what the applicationName and displayApplicationName are actually used for, there has been some bug reports about it, and behavior has changed between versions if I remember correcly.
If you want a window-title, I think you can do.
window->setWindowTitle( QCoreApplication::applicationName() );

Qt application remains in memory after closing all windows

I'm a beginner in using Qt and OpenCV, and I have a small problem.
My application works fine, but after closing it seems that opencv.exe (application name) is still in memory.
Here is my code:
int main(int argc, char* argv[]) {
QCoreApplication a(argc, argv);
cv::Mat img = cv::imread("img.jpg");
cv::namedWindow("Image");
cv::imshow("Image",img);
return a.exec();
}
How to kill task with closing application window?
I don't sure that I work correct with exec() function.
QCoreApplication::exec() starts an event loop.
Often times this is tied to the presence of a terminal window.
With QApplication::exec() it also starts an event loop, but it usually is tied to the presence of a QMainWindow or the last QWidget that was opened.
The easiest way right now for you to close it, is to go to Projects > Run > Run in Terminal, and check it.
You may also need to go to your .pro file and add CONFIG += console.
When you start using Qt signals and slots, the event loop will be extremely useful.
Also for any of Qt's GUIs to function properly you need the exec() event loop running.
Another way that you can kill your task when running it in Qt Creator is to go to the Application Output tab at the bottom and click the red square stop button.
Hope that helps.
You could try to call qApp->quit() in the close event of your non-qt window (I don't know OpenCV though).
qApp is equivalent to QCoreApplication::instance() if you started a non-gui application (in Qt terms of course), or a QApplication if you started a gui application.
To gracefully come out of event loop started by QCoreApplication::exec() QCoreApplication::quit () must be called.
Somehow when you are done with your OpenCV stuff it should call QCoreApplication::quit (). As it is a static slot you can connect a signal to it or call it explicitly.

QT application app.exec() in main() code is not reached until the main window is closed

I have created a QT application using QT Creator 2.4.0.
and created a main.cpp file that includes main() function as below:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Q_INIT_RESOURCE(MainResources);
MainWindow mainWindow;
mainWindow.show();
cout << "1\n";
int retVal = app.exec();
cout << "2\n";
return retVal;
}
When I execute this function the couts "1" and "2" are not shown at all.
I was expecting to see at least "1" in the console output.
they are printed only after I close the main window.
thus it seems as if app.exec() is not executed until the main window is closed...
1 - can anyone explain this?
When I try to execute this application from command line (the app is built statically)
the call returns immediately after I run the application.exe file, and doesn't wait for the main window to be closed.
2 - is there a way to make the application wait until the main window is closed?
Thanks
app.exec() is a blocking function (it launches Qt event loop) and doesn't return until the last window of the app is closed.
You indeed probably doesn't see "1" because of buffering issues. use qDebug() << "1" instead.
It's highly likely that the standard output is buffered, and thus the "1" may actually be printed but isn't flushed. You'd have to flush the standard output to get it to appear at the right time. Or you can print to cerr instead, which is likely unbuffered.
You indicate that the program runs normally in the first case (from QtCreator?) but without the console output you expect and in the second case, running from the console directly, the program exits without showing a window? I would say that you are running two different executables.

QT + boost-thread not working

I'm writing a code using both Qt and boost.
I know that qt window needs to be started in the main thread, so, I'm trying to run some code in another thread using boost (not QThread).
The problem is, if I run my code without starting the qt window, it works perfectly fine, however, if I call the app.exec(), the other thread (the boost one) stops working. I don't know what is happening, any clues?
QApplication app(argc, argv);
QMainWindow window;
//creating a separated thread and starting up
boost::thread thr1( boost::bind( &X::x, &a ) );
//if we join, it works
//thr1.joing()
//but if I run the following lines, my thr1 freezes
window.show();
app.exec();
the problem was with my boost implementation. I fixed recompiling it

Getting font metrics without GUI (console mode)

Let's say some images have to be generated by a Qt console program and that font metrics are needed by internal algorithms (they use the text width/height as input to compute the position where the drawing should occur). This program has to be runable on a Linux without any GUI (runlevel-3, basically a cluster without any display server).
Problem: QFontMetrics are only available when running a Qt application in GUI mode.
Any workaround to get string metrics without any display server ?
Ok after additional comments I think I understand your problem.
Just do it like that:
include <QApplication>
int main(int argv, char **args)
{
QApplication app(argv, args);
QApplication::processEvents(); // this should allow `QApplication` to complete its initialization
// do here whatever you need
return 0; // or some other value to report errors
}
You can also try use QGuiApplication this version doesn't require (doesn't use) widgets.
See also example in documentation how to handle none gui cases.
This code works perfectly on my Ubnutu with Qt 5.3
#include <QGuiApplication>
#include <QFontMetrics>
#include <QDebug>
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
QFont font("Times", 10, QFont::Bold);
qDebug() << font;
QFontMetrics metrics(font);
qDebug() << metrics.boundingRect("test");
return 0;
}
It also works with Qt 4.8 when QApplication is used.
Project file was quite simple
QT += core
TARGET = MetricsNoGui
TEMPLATE = app
SOURCES += main.cpp
Qt 4.8 has such QApplication constructor, whose 3rd parameter can help to solve the issue. Simple provide false as 3rd argument and enjoy using QFontMetrics in Qt console application. There will be no crashes if one starts app on systems without X server.
I didn't find a way to use QFont or QPrinter with QCoreApplication :( However, if you are able to install Xvfb, then your QApplication will be runnable also on a server without any display. I'm using this setup on a headless Raspberry Pi Zero.
Xvfb :1 -screen 0 1024x768x16 &
export DISPLAY=:1.0
./YourQApplication
This isn't the most elegant solution, but after hours of desperate searching, it's the only one I found.

Resources