As observed in the screenshot above, I have the problem that variables in the Locals and Expressions window in QT Creator are displayed as being . How can I get Qt Creator to show the values for the OpenCV filter functions?
QtCreator marks "not accessible" for the value, not for the variable. That means those variables are not initialized yet. Try to inspect the same values by adding another debugging point after the matrices are initialized.
I was getting this for a struct I had defined. I could see the contents of other structs, but not this particular one. It turned out that I was declaring the struct inside a function in my main window object. I just had to take the struct out of the function, just declaring it at global level before the function (not even bothering to put it in the header, although that's better practice). That was enough to allow me to see the values of a QList, that is itself still declared in the function in question.
Related
I am trying to use Code::Blocks debugger, and I'd like to have access to the values of the class attributes when debugging one of its methods.
I know that when I used Visual Studio, I could unfold the argument "this" to see all the values of the class attributes.
Do you know if this is possible with Code::Blocks ? Or is there a workaround ?
I don't see myself debugging with Code::Blocks without this feature.
Thanks
You can manually put *this in the watches ( although I don't expect this kind of workaround to be the right final answer )
I broke my Visual Studio project (2015, C++) into three pieces:
The main application in a static library
The executable which just has a main function
Unit tests which use the static .lib file so that they can import the required classes and do the unit tests on them.
Before I split it into a lib/exe/tests, the main application was just a standalone executable, and it worked perfectly fine.
Now I can't run the executable with only the main function, nor the unit tests as a certain pointer is always null. The only main difference is that I'm using a raw pointer in this example, but I was using a unique_ptr in my code (currently however I switched to the raw pointer just to make sure this following example is as accurate as possible and it didn't magically compile/run properly with the raw pointer).
The code looks very similar to the following (the extra code has been removed):
// console.hpp
extern Console *console;
The implementation cpp file (only of what is needed):
// console.cpp
Console *console = new Console();
Now in some unrelated function, this code fails due to the console pointer being a nullptr:
// some_other_file.cpp
#include "console.hpp"
// Inside some function...
console->doSomething(); // console is NULL
Again, the code I have worked perfectly fine when it was in one project. Regardless, it all compiles fine with no linking errors even though it has been broken into 3 pieces, but now that pointer is always null.
As a last interesting note, the non-pointer variables that are global and extern do work. Is this something limited to pointers/unique_ptrs?
Is this something fixable by a singleton pattern?
The clue is in this comment: "it appears other code is getting called before the main function that don't let the global variable get initialized."
The code referencing console is probably running as part of the initialization of another global and, in this case, is happening before the initialization of console. You have to be very careful to be sure that you're not depending on the order of the global initializers. You were probably getting lucky before you split the program, and now your luck has run out.
The easiest way to fix it is to use a singleton pattern. Instead of having other parts of the program directly reference the pointer, you have them call a function that returns the pointer, and that function will initialize it the first time. This ensures that it's initialized before it's used.
Here's a common pattern for it:
Console *GetConsole() {
static Console *console = new Console();
return console;
}
Now console is no longer a global variable. Everything that wants to access the console calls GetConsole instead.
The function-local static variable will be initialized the first time the function is called, and after that it just returns the value. If you have multiple threads and an older compiler, you have to do more work to protect against a possible race condition. Modern compilers should do the initialization in a thread-safe way.
I'm developing Qt UI application and localizing the UI strings. In my application, I'm using QLabel and setting the texts dynamically. I experimented two ways of setting the texts for localization. While executing the application, I could see, in my first approach strings are getting localized but in second approach they are not.
//Approach 1:
//ui.cpp
//Directly hardcoding the values
labelBox1->setText(QApplication::translate("context","test string"));
//Approach 2:
//UI_Consts.h
static const QString str = QT_TRANSLATE_NOOP("Context","Test String");
//variable 'str' is in global scope
//ui.cpp
#include "UI_Consts.h"
...
labelBox1->setText(str);
When I debugged why second approach not worked, I found that global variables are initialized before the execution of main function. We generally write code for Translation in main() and hence those global variables are not getting translated.
I prefer second approach as we can list down all the UI strings in a single file and easily maintain them. I want to know is there any way to localize the strings which are declared in Global Scope(like used in my second approach)?
Well, that's not the way Qt's translation system is meant to be used...
But if you want to do it that way you could write a function (or implement a class/struct holding all strings) that initializes all your string variables after loading the translations in main() - of course this would result in even more code :D
I think you've misunderstood what for is QT_TRANSLATE_NOOP. In the Qt Linguist Manual: Programmers you can read under the section: Using QT_TR_NOOP() and QT_TRANSLATE_NOOP()
If you need to have translatable text completely outside a function, there are two macros to help: QT_TR_NOOP() and QT_TRANSLATE_NOOP(). These macros merely mark the text for extraction by lupdate. The macros expand to just the text (without the context).
So, the problem isn't the global variable str.
How can I find out the address of a variable on the stack in the visual studio debugger watch window (or elsewhere?)
Reading the variable works fine:
streets streets [11790](0x1c66a690 [...] std::vector<Street *,std::allocator<Street *> >
But prefixing with & doesn't give me an address:
&streets streets [11790](0x1c66a690 [...] std::vector<Street *,std::allocator<Street *> >
Also, trying to read the size doesn't work, why is that?
streets.size() CXX0075: Error: Cannot set up Function Evaluation
The program is compiled in debug mode.
The Visual Studio Debugger drives debugger watch, quick-watch, auto, and local variable views through a translation defined by schema in a file called autoexp.dat (depending on your VS version, the content therein can vary markedly). The file is located in your VS-InstallDir/Common7/Packages/Debugger folder (at least it is for VS2010 and VS2012).
Knowing this, a couple of ideas for you to try/consider:
Method One: Library Know-How
To access the actual address of the first element within the vector I ultimately just do this:
streets._Myfirst
if you know the number of elements you're going to view, you can use the array-expansion extension by:
streets._Myfirst,N
where N is the number of elements
Note: this only works as shown above with vectors. The practice is different depending on which container you are using. There are no-doubt simpler ways that are probably less dependent on the implementation of std::vector<>, but this is the simplest wasy I know how to get you up and debugging quickly.
Method Two: Scorched Earth
Under Tools/Options/Debugging/General is a list of features you can switch on and off. One of them you will find particularly useful to this question:
Show raw structure of objects in variable windows.
Turn this ON to see raw member variables of all structures and containers, including standard containers like std::vector<>. This effectively disables usage of the templates in autoexp.dat
To see the address, cast to a void *, like so: (void *)&streets.
This is Visual Studio's attempt to be helpful by showing you the pointed-to vector directly. A similar problem affects arrays of vectors.
I'd like to get a value from a variable that's located deeply in the source code of the OpenCV library. Specifically, I'm trying to print out the value of stage_sum from the file haar.cpp. My starting point, facedetect.cpp, calls the method detectMultiScale, which then calls the function cvHaarDetectObjects, which calls cvHaarDetectObjectsForROC etc., until it finally reaches the function cvRunHaarClassifierCascadeSum, where stage_sum is calculated.
Is there a way I could get the value out to facedetect.cpp easily, without changing the declarations of all the preceding functions/methods, headers etc.? Simply trying to cout or printf the value directly in the source code hasn't given any results.
Thanks everyone for your help!
One option is simply to use a debugger.
However, if you want to do this programatically (i.e. access the variable as part of your application code), then unless the variable is exposed in the library's public interface, there are two options available:
Modify the library's source code, and recompile it.
Resort to undefined-behaviour (fiddling around with the raw bytes that make up an object, etc.).
Just to point the obvious, adding a std::cout() or printf() call inside haar.cpp won't do the trick. You need to recompile OpenCV for this changes to take effect and then reinstall the libraries on your system.