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.
Related
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.
I am trying to copy a 1100x1100 matrix from a .mat file to an array variable of type float in C++. I read online and found that the matio library is a good option. I installed their library using "make" on Ubuntu 12.04 (I followed the method given on their webpage).
However, I am unable to write code using it mainly because I am new to C++. I am using g++ to compile the file. I get errors such as "unknown reference to Mat_Open" and so on.
I did find this bit of code on the webpage:
#include <stdlib.h>
#include <stdio.h>
#include "matio.h"
int main(int argc,char **argv)
{
mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open(argv[1],MAT_ACC_RDONLY); //here argv[1] is "a.mat"?
if ( NULL == matfp )
{
fprintf(stderr,"Error opening MAT file %s0,argv[1]);
return EXIT_FAILURE;
}
matvar = Mat_VarReadInfo(matfp,"x"); // x is the variable we are trying to access?
if ( NULL == matvar )
{
fprintf(stderr,"Variable ’x’ not found, or error reading MAT file\n");
}
I have a couple of questions:
here, argv[1] corresponds to the .mat file I am trying to open right?
x in this code is the variable present in the .mat file I am trying to copy?
When I ran this code, I received errors stating - Unknown reference to Mat_Open and so on. Another couple of the same type of errors also were there.
I compiled this using : g++ abc.cpp -o test. (Followed by ./test. But I never got around to that due to the errors obtained during compilation).
How can I make it work? Is there any mistake with the code I used? Or with the compile statement I am using-maybe there are some linkers I need to use for compilation.
Thank you. Please remember that I am new to C++. Any advice would be helpful.
1) argv[1] - is a first parameter you put after your program call. If you want to "feel it", use debugger or code like this:
#include <iostream>
for (unsigned i = 0; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
}
2) Yes, looking at http://libmatio.sourcearchive.com/documentation/1.3.4/group__MAT_g4c8205ff25c5b688a40775fbb1840b7e.html I can say, that you will read variable with name "x".
3) "undefined reference" means you need to build with matio libraries. Add something like "-lLibraryName" to your compile string. And it will have to be built.
To avoid many problems, try to install Code::Blocks, it's cross-platform and quite easy to start using C++ if you never did it before. It also supports debuggers, so you will avoid many problems quite easy.
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.
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
I wrote this program in Qt Creator but I'm not sure how to run it. Here is my code:
#include <QtCore/QCoreApplication>
using namespace std;
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
string str;
cin >> str;
cout << " str is : " << str;
return a.exec();
}
When I run it my console shows this:
Starting /home/hamed/qt programs/test3-build-desktop/test3...
...and nothing happens. What should I do?
When copy pasting your code, it runs for me as expected (well, it doesn't terminate, but runs). Here is what I did in command line:
cd testproject
qmake -project
qmake
make
./testproject
As mentioned in the comment above, Qt itself is a library, so you are probably referring to some IDE when saying running it from "within Qt" - behaviour there is solely dependent on what IDE you are using.
Update:
From your message
Starting /home/hamed/qt programs/test3-build-desktop/test3...
I assume that you are using the QtCreator IDE, which does not allow you to enter things to console when running. I don't know whether you can get it to do so, but it works if you enter your project directory in console and use ./projectname. The building part mentioned above will be handled by QtCreator.
Another update:
Check out this thread for information on how to get it to work directly from QtCreator.
nothing happens!!
Your program is expecting an input, as you written here:
cin >> str;
what should i do?
Just type in something and press enter.
Add QTimer::singleShot(0, &a, SLOT(quit())); before the line return a.exec(); and don't forget to #include <QtCore/QTimer> (or you can make life easier and import the everything #include <QtCore>).
The a.exec() enters an event loop which waits for an event; normally in the form of user input with a graphical user interface. This however is a command line program and there isn't really a way for a user to send an event so it sits and waits forever. This is useful for server type applications but not what you're doing here. :)
(Note, this is one of many reasons your application might appear to be doing nothing. You may need to follow several of these answers before your program does what you expect)