Qt, use of "&" in tr - qt

I'm going through the Qt5 Application tutorial. In the creation of actions, I keep seeing the usage of "&" like the follwoing:
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this)
...
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
...
enter code exitAct = new QAction(tr("E&xit"), this);
...
I can't seem to find why these "&" are used, the Qt documentations on tr uses these notations without explaining what they are for. Further, when I deleted the "&" symbol, the resulting application doesn't seem to change at all.

As far as tr is concerned & is just another character in the string and has no special meaning.
However it does have a special meaning to QAction: It sets the following character as the entry's shortcut within a menu, so when you have the "File" menu open, pressing the n key will activate the "New" entry, x will active "Exit" and so on. Depending on the OS, the character after the & may also be underlined (on Windows it's only underlined when you press the Alt key).

& is used to indicate a character that should be used for the Alt-key shortcut for a given menu item. Assuming these actions are being put into a menu named &File, pressing Alt-F, then N would activate the New action.

It's for shortcut (in many cases it's Alt and the character).
If you code doesn't have & it will be like this:
now if you have & before the key you want to use for shortcut, it will look like this:
Here I assume you already have these actions added to a menu as in the pictures above, and you also have & for the menu name, something like this:
fileMenu = menuBar()->addMenu("&File");

Related

Shortcut / key combination to access Jupyter notebook menu bar

I prefer the convenience and speed of the keyboard over using the trackpad/mouse whenever possible. Is there a means to access the Jupyter menu bar with the keyboard. For example the Edit menu:
On Linux we could access a top-level item as Alt-E | D for Edit | Delete cells . On macos it is a headache but still possible: <Access Menubar shortcut>| E | D.
Is there any way to achieve that on Jupyter?
So you want to bind custom keyboard shortcuts...
The documentation tell you to use the JavaScript API, where the documentation is in the source code.
So you end up with something like this in custom.js (it's convenient that the first characters of all the menus are distinct)
for(let id of ["filelink", "editlink", "viewlink", "insertlink", "celllink", "kernellink"]) {
const element=document.getElementById(id)
const actionName=Jupyter.notebook.keyboard_manager.actions.register(function (env) { element.click() },
`open-menu-${id[0]}`, "custom")
const shortcut='alt-'+id[0]
Jupyter.keyboard_manager.command_shortcuts.add_shortcut(shortcut, actionName)
Jupyter.keyboard_manager.edit_shortcuts.add_shortcut(shortcut, actionName)
}
This code binds Alt+E to clicking edit menu.
It's rather version-dependent (as it depends on the ID of the links on the menu) -- this is tested on Jupyter notebook version 6.1.6.
If you use a for loop to generate the shortcuts (like in the code above), it's very important that you register actions manually (and have different names for them) instead of add_shortcut(shortcut, handler) because otherwise the handler would generate the action name from the string representation of the handler, and that would be all the same. No JavaScript closure can help you.
If you also want to use Enter key to select a menu, you have to do a little more -- because by default it's bound to enter edit mode:
{
Jupyter.keyboard_manager.command_shortcuts.add_shortcut("enter", function(env){
const element=document.activeElement
if(element.getAttribute("role")==="menuitem")
element.click()
else{
env.notebook.edit_mode();
// alternatively:
//env.notebook.keyboard_manager.actions.call("jupyter-notebook:enter-edit-mode")
}
})
}
Modify it a little more if you want to add shortcut for the 2 other menus, as they don't conveniently have an ID.

How to use the Edit/add new keyboard shortcuts in Jupyter

I am in the Edit Command mode Shortcuts dialog and things seem reasonable ..
But the actual behavior is a different story
When using the Option modifier (plus a non-modifier key) it just ends up printing a high-ascii character value in the add shortcut area .. and then when I refresh the page it has gone away.
I can not get Command modifier (plus a non-modifier key) to work at all. It is jus ignored.
The Control modifier (plus any key) is completely ignored.
So there is a basic usability misunderstanding here. Advice appreciated.
I added 'Ctrl-Q' as a shortcut to restart kernel and run all the cells. You can see the picture below. It is working fine. In the 'Ctrl-Q', 'Q' was just lowercase and it ran successfully.

Qt: how to save QTextEdit contents with custom properties

I have a text editor (QTextEdit). Some words in my editor contains additional information attached (namely, two corresponding integer positions in wave file for that word).
They are stored in Python object as custom properties for QTextCharFormat objects (I attach them with code like this: self.editor.textCursor().setCharFormat(QTextCharFormat().setProperty(MyPropertyID, myWordAttachment) )
Unfortunately, if I save my document to html, all of that additional formatting is lost.
So, I want to perform simplest task: to save my document with all of it's formatting,including myWordAttachment (and to load it from disk).
Am I right that Qt5 doesn't have something ready for it, and I have to write all that document's serialization code by myself? (I still hope that where is simple function that did the job)
1.you loop your text every character.
2.and you catch the character and its charFormat()
3.and you get the properties.
4.Because the properties are eventually a value of something, int,str,...
So you get the properties by charFormat().property(1),(2),(3)... or properties()
5.The most important thing is the character's position & the range.You get the position during the 1th loop.
6.When you catch the CharFormats, you insert into something hashable object like list.
& and you don't forget to insert the CharFormats position.
6.you save your document and the position & properties.
My suggestion for your solution.
1.you can get characterCount() by the QTextDocument object.
2.you loop the range of the characterCount()
3.Before doing it, you make a QTextCursor object.
4.you set the textcursor at the first position.(movePosition method & Start moveoperation & KeepAnchor flag)
5.you move the cursor to right one character & Another.
6.you check the character's charFormat() by tc.charFormat() and the tc.position()
7.But it is the time to Think twice. CharFormat is always the bunch of characters.
you probably get some characters of the same CharFormat().
You can prepare for it.I can Think about some way,but... you should set the QCharFormat objectType or propertyId() for specifing the QCharFormat in Advance(during editing your document).Why don't you set the texts into the properties for after saving & loading.I hope you manage to pass here during debugging & tring.
8.if you get a charFormat,and you check the objectType().
9.if the objectType() is the same as Before searched, you pass the search engine without doing anything.
10.The second important thing is that calls clearSelection() each searching.
11.You save your document() as it is html strings.and you save the charFormats() properties.
12.when you load your document(),the html sentence comes back.
and load the properties.
you make QTextCursor and setPosition( the property's position saved in advance.)
you move QTextCursor until the position and you select the target texts.
you adopt the charFormat properties again and the end.
Summary
The important thing how you specify the charFormat().
You can catch the charFormat without any problem.but the charFormat() is adopted in some range.So you must distinguish the range.
1.The targeted texts is set in the QTextCharFormat's property.
2.You have The QTextCursor pass during the same QTextCharFormat's object.
I can Think of them...
I Think it is some helps for you.

How to Click a Link on a IE webpage?

I want to open a button named 'Open' in IE window using VBScript. Source code is like this:
<a title="Devtems Life Menu" href="javascript:OpenOryx('cboClone');">Open</a>
Before I start answering the question, I just want to say that I am by no means an expert at VBScript. My knowledge is based off a lot of messing around with it during weekends and a bit of read the documentation. There may be better ways of doing things, but this works for me.
My recommendation is if you have access to the html, add an attribute to your link so it looks something like this:
<a title="Devtems Life Menu" href="javascript:OpenOryx('cboClone');" id="openMenu">Open</a>
Then you can easily reference it in your code. If you can't access the html, then you may have to use a more roundabout way of checking the contents of each link. The actual syntax is described below in a little script I constructed. It firstly loads w3schools.com, then clicks on the references tab. This uses ie.document.getElementById(arg). Notice that the references navigation panel opens. After that it will prompt you to click the ok button to continue. It will then load google.com, and look through all the <a> tags. If it contains Open it clicks on it. You can use the same syntax, except different details for your script.
' Create the ie object
set ie = createobject("internetexplorer.application")
' Navigate to wherever you want
ie.navigate("http://www.w3schools.com/")
ie.Visible = true
' Call the subroutine we define below
waitForPage(ie)
' Get link by id
' This is a one liner, and I personally think is better than the method below
' first of all you get the tag you want by id
' then you click it
ie.document.getElementById("navbtn_references").click()
' call ie.document.parentWindow.execScript("w3_open_nav('references')", "JavaScript")
' could also be written as:
'set buttonElement = ie.document.getElementById("navbtn_references")
'buttonElement.click()
MsgBox("Click ok to continue to google.")
' Load google
ie.navigate("https://google.com/")
' And wait for it to load
waitForPage(ie)
' Click on a link by attribute (same technique can be used for name etc)
' Get all elements with tag input
set linkElements = ie.document.getElementsByTagName("a")
' Then loop through them
for each possibleElement in linkElements
' If it has a certain name..
if possibleElement.innerHtml = "About" then ' You could use If possibleElement.getAttribute("name") = "foo" Then or possibleElement.getAttribute("class") = "bar"
' Click it!
possibleElement.click()
end if
next
' Subroutine to wait for internet explorer to load a page
sub waitForPage(ie)
do
WScript.Sleep(100)
loop while ie.ReadyState < 4
end sub
Another method is rather than actually finding and clicking on the link, you simply run the script that is run when a user normally clicks on a link. In your case, it could mean running javascript:OpenOryx('cboClone'); directly. Here is how to run javascript code in VBScrpt:
call ie.document.parentWindow.execScript("javascript:OpenOryx('cboClone');", "JavaScript")
I hope that at least one of these methods helps you in writing your script.

How to type text on text fields and click on a button using AutoIt

I have a JNLP file (say Test.jnlp). I have opened that file using AutoIt. My code for opening Test.jnlp file:
$d = "D:\Ripon\"
$f = "Test.jnlp"
Run("cmd /c " & " """ & $d & $f & """ ", "", #SW_HIDE)
After opening .jnlp file a Login screen comes. My requirement is to type Username and Password -> Click Login button.
As I couldn't find the identity of elements (text fields, button) I failed to do that. Please help me.
There are a few types of GUI that don't give you ids for controls, almost all Java toolkits are an example of that.
There are several methods of automating them still:
Assuming the window is always the same size (which is often a pretty safe assumption to make) then AutoIt will allow you to click the window at a given position, or send text just to that window. For example in the above case you could try the following code:
ControlSend("Window Title", "", "", "MyUsername{TAB}MyPassword{ENTER}")
Another solution is using accessibility features. If you google 'autoit java accessibility bridge' I'm sure you'll get some results like this one. I've never tried this personally.
There is also a java access bridge no idea how this works either, but other people have been using it for a while.
As a last resort, you have to mimic user mouse and keyboard actions. This really isn't the best solution but at least you can be very sure it will work.
The following snippet of code should work:
Send("username")
Send("{TAB}")
Send("password")
Send("{ENTER}")

Resources