gtkmm glib gio critical - c++

On running the following simple.cc example for gtkmm
#include <gtkmm.h>
int main(int argc, char * argv[]){
Glib::RefPtr<Gtk::Application> app
= Gtk::Application::create(argc,argv,"org.gtkmm..examples.base");
Gtk::Window window;
//Gtk::ApplicationWindow window(app);
return app->run(window);
}
I face the following message:
(process:9428): GLib-GIO-CRITICAL **: g_application_set_application_id: assertion `application_id == NULL || g_application_id_is_valid (application_id)' failed
However, the application doesn't break, the window is produced and doesn't exit until I ctr+C the program.
What are the implications of this GLib-GIO-Critical message ? What do I do to suppress the message ?

If the provided application-id is not valid then it will not be set. I'm not familiar with the glibmm bits, but if you don't provide an ID to g_application_new then, according to the documentation, "...some features of GApplication (most notably application uniqueness) will be disabled."
"Suppressing" it is easy--just fix it. Provide a valid application ID or don't provide one at all (pass NULL instead of a string). In your example, getting rid of the extra dot ("org.gtkmm.examples.base" instead of "org.gtkmm..examples.base") should do the trick. The g_application_id_is_valid documentation explains what constitutes a valid ID, including that "Application identifiers must not contain consecutive '.' (period) characters."

I'm glad with the explanation in the solution but .. based on that just pass an empty string "". However "org.gtkmm.example" should work

Related

QLocale codeToTerritory only worls with 2-digit codes

I would like to get the Country name for any ISO 3166 country codes. My project currently uses 3-digit codes, so if possible, from that. According to the Qt documentation QLocale::countryToTerritory should solve this, but in my test programs, it only works with 2-digit codes. I also tried the deprecated codeToCountry function, but it has the same result. Also the numeric code does not have any result.
Here is a simple test program:
#include <QCoreApplication>
#include <QLocale>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString l2 = "DE";
QString l3 = "DEU";
QString d3 = "276";
qDebug() << QLocale::codeToCountry(l2) << QLocale::codeToCountry(l3) << QLocale::codeToCountry(d3);
qDebug() << QLocale::codeToTerritory(l2) << QLocale::codeToTerritory(l3) << QLocale::codeToTerritory(d3);
return a.exec();
}
The output is:
QLocale::Germany QLocale::AnyTerritory QLocale::AnyTerritory
QLocale::Germany QLocale::AnyTerritory QLocale::AnyTerritory
I tried with a few different countries, all with the same results.
I built the project with Qt 6.3.0 with MSVC2019 compiler on windows.
Is the documentation simply wrong, or am I missing something?
Looking at the code source of codeToTerritory you can see that the country code is compared against a list of codes contained in territory_code_list. This variable is defined in qlocale_data_p.h.
It seems that only the ISO 3166-1 alpha-2 code (i.e. 2-character code) is used in this list. There is neither "DEU" nor "276" in territory_code_list, which means that QLocale::AnyTerritory is returned.
So I do not know if the documentation is wrong, but at least it is imprecise.
EDIT: digging a bit further, the list of codes is generated from https://unicode.org/Public/cldr/. These files only contain 2-letter codes for countries (there are some 3-digit codes corresponding to continents, but none for countries).
EDIT2: so, you need to write the conversion from 3-digits / 3-letters code to alpha2-code to be able to use Qt methods. You can get data from this website.

ANSI escape code not working properly [C++]

I am using the ANSI escape code to print colored output.
I am getting proper colored output in vs code integrated terminal.
But when I am running the program in external command-prompt/Powershell, I am not getting the expected colored output.
My program looks something like this:
#define RESET "\033[0m"
#define RED "\x1B[31m"
#define GREEN "\x1B[32m"
int main(int argc, char** argv){
if(argc != 1){
std::cout << RED ">> Invalid Arguments!" RESET;
}else{
if(verify_password()){
...
...
...
}else{
std::cout << "\n>> " RED "Invalid Password!" RESET;
}
}
return 0;
}
Complete Code
NOTE: One weird thing I observed is that if I am entering the correct password then everything is working fine in both terminals(getting proper colors). But the problem is when either I am entering an incorrect password or an invalid amount of arguments
What might be the reason for this?
EDIT: I figured out a way to make it work. I find out that in order to make these escape codes work I need to have at least one call to system() function. But still, I am not sure how these things are connected.
Historically, consoles on Windows required use of the console API in the Windows SDK in order to do effects like color. But recent versions of Windows now support escape codes (and even UTF-8), but programs have to opt-in by calling SetConsoleMode with ENABLE_VIRTUAL_TERMINAL_PROCESSING. (Opting in preserves backward compatibility for older programs that aren't using escape codes or UTF-8.)
If you're getting color in some places and not others, then I'd guess there's a problem related to the state the terminal/console is in when sending the escape codes. For example, if the terminal thinks it's already processing an escape code and a new one begins, it might not recognize either. I suppose this might also be a problem if one part of the program uses escape codes but another part uses the Console API.

Running the command line dot program from a Qt application generates no output, what is wrong?

I have an app that generates a dependencies.dot file which I then want to convert to an SVG image.
When I do that from a simple application like so:
int main(int argc, char * argv[])
{
system("dot -Tsvg ../BUILD/dependencies.dot > ../BUILD/dependencies.svg");
return 0;
}
It works great. The SVG image is present and working.
When I instead run it from my Qt application, the SVG file is created (by the shell), but it remains empty.
Any idea what could prevent dot from outputting data to its stdout?
Just in case, I also tried a cat to send the input through stdin instead of a filename:
system("cat ../BUILD/dependencies.dot | dot -Tsvg > ../BUILD/dependencies.svg");
And that didn't make any differences.
Also using the full path (/usr/bin/dot) did not help either.
Another test, I tried to use popen() and the first fread() immediately returns 0 (i.e. the mark of EOF).
It may not be Qt, but something is interacting with dot's ability to do anything. Any pointers on why that is would be wonderful.
Maybe an important note? I start my app. from a console, so stdin, stdout and stderr should all work as expected. I actually can see debug logs appearing there and other apps seem to work just as expected (i.e. My Qt app. can successfully run make, for example).
Here is an example of the resulting SVG (when I don't run it from within my Qt app):
For reference, the source code can be found on github. This is part of the snapbuilder. A tool that I use to run a build on launchpad. It's still incomplete, but it's getting there.
https://github.com/m2osw/snapcpp/tree/master/snapbuilder
The specific function to look for: project::generate_svg().
I still have no clue what side effect Qt has on system() that the dot command would fail. However, if using my own fork() + execve(), then it works.
I wanted a new process class for my environment, so I implemented that. This newer version is using FIFOs or directly opening closing files that it passes to the process.
...
// write the script in `std::stringstream dot` then:
//
std::string script(dot.str());
g_dot_process = std::make_shared<cppprocess::process>("dependencies");
g_dot_process->set_command("/usr/bin/dot");
g_dot_process->add_argument("-Tsvg");
g_dot_process->add_input(cppprocess::buffer_t(script.data(),
script.data() + script.length()));
g_dot_process->set_capture_output();
g_dot_process->set_output_capture_done(output_captured);
g_dot_process->start(); // TODO: check return value for errors
...
// and in output_captured()
//
void snap_builder::svg_ready(std::string const & svg)
{
std::string const svg_filename(...);
{
std::ofstream out;
out.open(svg_filename);
if(!out.is_open())
{
std::cerr << "error: \n";
return;
}
out.write(svg.c_str(), svg.size());
}
dependency_tree->load(QString::fromUtf8(svg_filename.c_str()));
}
Now the dot file is generated and displayed as expected.
This is rather strange since most everything else I've done with a simple system() call works as expected. There must be something about stdin or stdout that makes dot not do its work.

QCommandLineParser: suspend execution if built-in options were processed

I am developing a Qt 5.x application whose main function is more or less like that (simplified):
int main(int argc, char *argv[])
{
QApplication app(arg, argv);
QCommandLineParser cmdLineParser;
cmdLineParser.addHelpOption();
cmdLineParser.addVersionOption();
cmdLineParser.process(app);
/// *** SOME INITIALIZATION STUFF *** ///
app.exec();
}
I am experiencing some problems when I invoke the execution of application specifying the --version (or --help) option on command line: the application outputs the version (or the help) and then sometimes terminates with a segmentation fault...
I think it is due to the fact that when QCommandLineParser detects a built-in option on command line (--version or --help) simply calls QApplication's exit() for terminating the execution and this interferes with my own *** SOME INITIALIZATION STUFF *** (basically some other threads are started and maybe a prematurely call on exit() causes some problems)...
However, my question is: is it possible to make Qt avoid to go further in my *** SOME INITIALIZATION STUFF *** if a built-in option is detected on command line? In other words, is there a method to be called for knowing that a built-in option has been detected by the command line parser? Otherwise: is there a method for knowing that exit() has been called on application, and hence the application is terminating? If so, it would suffice to enclose my *** SOME INITIALIZATION STUFF *** in a if condition argumenting on that method and it would be fine...
Thanks for the support.
>> EDIT <<
I've done some further investigations... the behavior seems not be dependent on my own *** SOME INITIALIZATION STUFF ***... in fact even a "minimal, complete and verifiable example" as the following one sometimes terminates the execution with segmentation fault when the executable is triggered with --version on the command line:
#include <QApplication>
#include <QCommandLineParser>
int main(int argc, char *argv[])
{
// Instantiate the application
QApplication app(argc, argv);
app.setApplicationVersion("1.0.0");
// Prepare for parsing the command line
QCommandLineParser cmdLineParser;
cmdLineParser.setApplicationDescription("Test application");
// Add support for command line standard options (help & version)
cmdLineParser.addHelpOption();
cmdLineParser.addVersionOption();
// Perform actual parsing of command line
cmdLineParser.process(app);
// Let the application run
return app.exec();
}
Just to clarify, I am using Qt 5.3.2 on an ARM device running Linux OS.

Get parameter from exe in C++

As we know the Query String in web. It's key/value go with the website URL ex: abc.com?myName=stack
For example in PHP, if we want get value of the myName, just do this $_GET['myName']
So, in C++, how can I get it?
in C# I pass an parametter to an *.exe file ( this exe file is C++ code ).
In C++ code, how to get this parametter value .
Build a console application with just the following code:
#include <iostream>
int main(int argc, char** argv)
{
for(int i = 1; i != argc; ++i )
{
std::cout << argv[i] << std::endl;
}
}
Assuming the name of the .exe is mytest.exe, execute it with some arguments, such as:
mytest.exe Hello there.
You should get the following output:
Hello
there.
Hope the simple example makes it clear as to how to process command line arguments in C++.
Have no idea about your situation, but surely you will have realize what parameters do you really need?
If you just need arguments from the command line, simple use the char** argv variable. In complicated cases you can use GNU getopt or even Boost::Program_options (the last is cross-platform);
If you are trying to access environmental variables, use standard getenv functions.