Is there a quicker way to copypaste C++ pointers from Visual Studio's Watch window into a conditional breakpoint?
Context: My pointer values change each time I restart my application, so I need to update the address in my conditional breakpoint.
If I have a watch for this, copypasting it gives something like this:
+ this 0x000001287234a8c0 {mTick=2994 mTime=0.00000000 ...} AnimComponent *
When I right click on this, there's a "Copy Value" option, but it copies that {} block too:
0x000001287234a8c0 {mTick=2994 mTime=0.00000000 ...}
So my current process to copy my this watch's pointer value:
Click on this
Ctrl-C
Edit conditional breakpoint
Ctrl-v, remove the {} block, and add a == after this
I'm using C++, so I can't use the "Make Object ID" feature.
Watch Window
Use (void*)this in the watch window to prevent the {} block and make "Copy Value" copy only the number.
Keyboard Shortcut
Open Tools > Customize > Keyboard
Search for "CopyValue" and you should find DebuggerContextMenus.AutosWindow.CopyValue. Map a keyboard shortcut to it and you can copy values with only two steps: click + keyboard.
If you use Ctrl-Shift-C to copy, be sure to remove that shortcut from View.ClassView or it will interrupt your copying.
Related
I recently switched to QTCreator for C++ from Eclipse background.
I am looking for these two features in QtCreator...
(1) I want to open the file a class is written in by typing that class's name. For example Ctrl+Shft+T in Eclipse.
(2) I want to move back to where my cursor were before the current operation.
Any shortcuts for above two features?
To open the file of a class:
If you are in a file that is making use of the class, you can place the cursor on the class then press F2. By default, F2 is bound to the Follow Symbol Under Cursor action.
If you want to get to the class without to find an instance of it, you can use the Locator . By default Press:
ctrl + k to access the locator
Type c then space to locate classes
Now type the name of your class. Once you've selected the class, hit enter
To navigate back to where you were, you can use GoBack and GoForward, as mentioned by Roman Zaytsev. These default to alt + left and alt + right. It has been many years since I have used eclipse, but I thought it also used alt + left and right as I was pleased to find QtCreator used it as well.
(2) I want to move back to where my cursor were before the current operation.
Go to Tools > Options > Environment > Keyboard. There assign shortcuts for "GoBack" and "GoForward" in "QtCreator" section.
When I hover my cursor on a function called in my code in Visual studio 2012, a small box pops up to show its declaration. How let this work for constructors?
Function(1, 2); // Hover on Function
MyClass a(1, 2); // Hover on a or MyClass or use any shortcut key
The problem is C++'s syntax for constructor calls. It works fine with normal methods because they look like method calls. Constructor calls are all mixed up with the object declaration, and when you hover over them, VS just gives you a tooltip with the object's declaration because it assumes that's what you want.
Hovering over the text gets you the same tooltip as if you had invoked the "Display Quick Info" command (Ctrl+K, Ctrl+I).
But there is also the "Display Parameter Info" command, invoked with Ctrl+Shift+Space, that will display the information you're interested in for constructors.
The trick is that you have to invoke the command with the caret inside of the parentheses. It won't work when the caret is somewhere within the identifier.
I'm trying to design a Qt GUI application with user customize-able hotkeys. The main issue I'm running into is how to synchronize the hotkeys across the application since a particular hotkey (for example, copy) may be used by multiple widgets/components.
My current strategy is to use a reference class which holds a list of QKeySequence objects for each different hotkey. Each widget would have to have a way to reference this master list and have custom implementations of low-level the keyPressEvent which would compare inputted keys vs. the hotkeys. I don't particularly like this strategy, though, as it requires significant re-implimentation in each widget and feels like I'm trying to re-invent the wheel.
I also tried using QAction objects which can hold QKeySequence shortcuts internally, then use these to trigger higher-level events which I can handle using slots & signals. However, the main issue I have here is how to manage which slots signals get routed to.
For example, say I have 2 open widgets which can both receive a copy action signal. I can connect a slot for both of these to the same signal and take advantage of the single update point for shortcuts, but then things get messy since only the active widget should act on the copy signal, not both widgets. I can re-implement the focusOutEvent and focusInEvent handlers to connect/disconnect slots manually, but this also seems to run into the same issue above where I'm trying to re-invent the wheel and doing more work than is necessary.
Is there an easier way around this problem?
I don't think there is a particularly easy/non-tedious solution to this problem, but when I needed to add user-customizable hotkeys to my application, here is how I did it:
1) Start with your application that has hard-coded key shortcuts, e.g. code like this:
QMenu * editMenu = new QMenu;
QAction * copyItem = menu->addAction(tr("Copy"), this, SLOT(CopyData()));
copyItem->setShortcut(tr("Ctrl+C"));
2) Create a GetKeySequence() function that looks something like this:
static QHash<QString, QKeySequence> _usersKeyPreferences;
static bool _usersKeyPreferencesLoaded = false;
QKeySequence GetKeySequence(const QString & keySequence, const QString & contextStr)
{
if (_usersKeyPreferencesLoaded == false)
{
// Oops, time to load in the user's saved custom-key settings from a file somewhere
_usersKeyPreferences = LoadUsersKeyPreferencesFromFile();
_usersKeyPreferencesLoaded = true; // so we'll only try to load the file once
}
if (_usersKeyPreferences.contains(contextStr))
{
return _usersKeyPreferences[contextStr];
}
else
{
// No user preference specified? Okay, fall back to using the
// hard-coded default key sequence instead.
return QKeySequence(qApp->translate(contextStr, keySequence));
}
}
3) Now the tedious part: grovel over all of your code, and anywhere you've specified a key-sequence explicitly (like in the third line of the code shown for step 1), wrap it with a call to GetKeySequence(), like this:
copyItem->setShortcut(GetKeySequence(tr("Ctrl+C"), tr("Edit_Menu|Copy")));
4) At this point, your program's key-sequences will be customizable; just make sure that the key-settings-file is present on disk before GUI-creation code runs. Here's an excerpt from my program's key-mappings file (which I store as a simple ASCII text file):
Edit_Menu|Copy = Ctrl+C
Edit_Menu|Cut = Ctrl+X
Edit_Menu|Paste = Ctrl+V
[... and so on for all other menu items, etc...]
... of course one downside to this approach is that once the GUI is created, the key-bindings can't be modified "on the fly" (at least, not without a lot of additional coding). My program gets around this simply by closing and then re-creating all windows after the user clicks "Save and Apply" in the Edit Key Bindings dialog.
5) An optional further step (which is some extra work up front but saves time in the long run) is to write a program (or script) that greps all the .cpp files in your program's codebase looking for calls GetKeySequence() in the code. When it finds a GetKeySequence() call, it parses out the two arguments to the call and prints them as a line in a key-bindings file with the default settings. This is useful because you can make this script part of your autobuild, and thereafter you'll never have to remember to manually update the default key-settings-file whenever you add a new menu item (or other key-sequence specifier) to your program.
This worked well for me, anyway. The advantage is that you don't have to refactor your existing program at all; you can just go through it inserting GetKeySequence() as necessary while leaving the larger logic/structure of the program intact.
Is there is a tool or a setting in the Visual Studio debugger to stop on breakpoints or when a variable is set to a particular value? I mean, if I know that value will be set to "HELLO," I want the debugger will stop the same way it would if it reached a breakpoint?
You're looking for a Conditional Breakpoint.
Set a breakpoint anywhere in code.
Enable the list of breakpoints window by going to Debug menu -> Windows -> Breakpoints.
In your breakpoints window, right click on a breakpoint
Select Condition...
Enter any expression involving your variable
The breakpoint will be hit when the condition is met.
Via the right click on breakpoints menu, you can also set breakpoints:
Only from certain processes or threads
Upon hit counts
Only when a condition or variable is changed
there are watchpoints.
Daves answer.
And I'll add that you can just add a if statement that contains a couple of dummy statements and you put a breakpoint inside it. It does the same thing.
Typical use :
if (i == 250) {
int dummy = 2+2; //breakpoint here
}
In your case, since you watch the value of a string (assuming C++ strings)
if (mystring == "hello")
{
int dummy = 2+2; //breakpoint here
}
try - System.Diagnostics.Debug.Assert(yourVariable <> "HELLO") and then click on 'Cancel' button to start debugging. This works for ASP.net and Silverlight projects
There is a article in MSDN which provides the procedure:
http://msdn.microsoft.com/en-us/library/aa295838(VS.60).aspx#_core_setting_a_breakpoint_when_a_register_expression_is_true
But it seems that i tis for visual studio 6 ... Actually I can not find the "Breakpoint" entry under "Edit" Menu...
Do you know how to do that? I want to break when EAX changes to an error code so I can find the place where this error is returned.
Assuming you are using Visual Studio 2008, you can find it in its own Debug menu.
You need to create a breakpoint. In the Breakpoint Windows ( Debug->Windows->Breakpoint ) right-click the BP and select condition.
In the condition-field you can just type the expression ( "eax == ebx" ).
Just to be explicit about what #DarthCoder said:
First create a plain breakpoint, so there's something to edit
Then Debug->Windows->Breakpoint brings up the list of breakpoints, including the one you just made
Right-click on the breakpoint, pick "Condition"
Enter the condition, such as eax==ebx