Retrieve different text from combo box in Qt Designer (pyqt) - qt

this could sound strange and it is more a curiosity then a question.
I have a simple combobox with 2 elements in Qt Designer.
The 2 combobox elements are vertical and horizontal but for the script I'm writing I need to get only v or h.
Usually I easily do it with a loop like:
name = self.combbox.currentText()
if name == 'vertical':
name = 'v'
else:
name = 'h'
and that's ok.
I was just thinking if there is a way in Qt Designer to assign a kind of tag to the elements so the user sees the complete text but with the code it can be retrieved the tag.
Thanks to all

I don't believe you can do this with Qt Designer alone (see How can I add item data to QComboBox from Qt Designer/.ui file).
With some extra Python, though, you can add use setItemData() to add whatever extra data you want (How can I get the selected VALUE out of a QCombobox?) and retrieve it with itemData() and currentIndex().

Related

Get the actual Qt window title, properly replacing the [*] placeholder

I need to get the visible title of a Qt top level window (or MDI subwindow), because I want to list window titles in different places just like they're visible for the user.
Consider a program that supports multiple top level editor windows (or an MDI area with similar purposes) that should list the titles of those windows, like a menu or an internal "window manager".
If I want to list those windows and also support the [*] placeholder for the windowModified property, their windowTitle() will return that annoying placeholder no matter of their state.
Unfortunately, the windowTitle feature is a bit abstract, for the following reasons:
due to the windowModified feature, it always returns the placeholder if it's set;
the [*] placeholder can be "escaped" with multiple, even occurrences, in order to actually display [*] in the window title: Title [*][*] will always be shown as Title [*] no matter the value of the windowModified property;
if the windowTitle property is an empty string (the default), it falls back to the windowFilePath property, which not only make it always include the [*] placeholder, but could also behave oddly in the rare case that property contains the [*] placeholder; while I realize that this is a very odd (and somehow irresponsible, assuming the system actually supports it) situation, I still want a reliable way to get the currently resulting window title, even in those rare (though "wrong") situations;
Is there a way to get the real title that Qt sets for the window, considering the above?
The best approach for top level windows (not MDI subwindows) is to use QWindow::title:
This property holds the window's title in the windowing system
The window title might appear in the title area of the window
decorations, depending on the windowing system and the window flags.
It might also be used by the windowing system to identify the window
in other contexts, such as in the task switcher.
How to obtain the QWindow?
If you already know the QWidget, you can use QWidget::windowHandle. Note that the QWindow object is only available for native widgets (f.ex. top level widgets, but not for MDI subwindow).
Another approach is to directly query for all the top level windows using QGuiApplication::topLevelWindows
MDI subwindow support?
As those MDI subwindows are not top level windows, the QWindow doesn't exists. So, this approach doesn't work in that case. Possible solutions are:
See #musicamente's answer, mimicking the Qt placeholder replacement procedure.
Create a Qt bug report to expose the resolved window title or alter the Qt source code yourself to do so and distribute it with your application.
There is no absolute and certain way to get the title that the OS will finally show in the title bar of a top level window.
As explained in the windowTitle documentation, some systems might support displaying the applicationDisplayName. Some highly customizable (Linux) OS might show an altered version of the provided title. There is almost no way to get the "final", displayed title, unless you want to dig into specific OS modules that interface with the Window Manager (and that might not be enough anyway, due to the high level of customization possible on *nix systems).
Considering that what's relevant for the OP is the "internal" window title (what Qt finally "relays" to the system), the solution is to implement what Qt actually does internally with qt_setWindowTitle_helperHelper().
Be aware that the Qt implementation is not perfect. There are some odd cases when specific combinations of the placeholder string are used. For instance:
using [*] [*] [*] as window title results in "[*]" being shown for an unmodified window and "* [*] *" otherwise;
with [*] [*][*] [*], the unmodified window title is " [*] [*]" (note the leading space) and the other is "* [*]* [*]"
While, as said above, the Qt implementation is far from perfect, what we're interested into is the actual window title relayed to the OS, so we must adhere to it, since the visual result is the important aspect, no matter whether it's "correct" or not.
Finally, remember that this implementation might become invalid in the future, in case Qt developers decide to change this behavior (and, I believe, they should).
The following code is a simple function that will return the actual window title relayed to the OS for a give widget, which can be used for any situation in which the visible title has to be displayed:
def realWindowTitle(widget):
title = widget.windowTitle()
placeHolder = '[*]'
if not placeHolder in title:
return title
phSize = len(placeHolder)
style = widget.style()
if (widget.isWindowModified()
and style.styleHint(
style.SH_TitleBar_ModifyNotification, None, widget)
# for PyQt6 or full Enum support use
# style.StyleHint.SH_TitleBar_ModifyNotification
):
replaceHolder = '*'
else:
replaceHolder = ''
index = title.find(placeHolder)
while index != -1:
index += phSize
count = 1
while title.find(placeHolder, index) == index:
count += 1
index += phSize
if count % 2: # odd number of [*] -> replace last one
lastIndex = title.rfind(placeHolder, 0, index)
title = (title[:lastIndex]
+ replaceHolder
+ title[lastIndex + phSize:])
index = title.find(placeHolder, index)
# return "escaped" sequences of the remaining double placeholders
return title.replace('[*][*]', placeHolder)

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 find all SO10 text modules used in an SAP Adobe Form?

How do I find all the text modules used in an Adobe Form? Is there a table somewhat like TADIR where I could type in the Adobe Form name and the object names (text modules) for that form name will appear?
My answer regarding Smart Forms is perfectly valid for Adobe Forms too. Adobe Forms also have SO11 text elements hard-coded in the form FM code. The only difference is link between Adobe form FM and Adobe form INCLUDE: for some reason there is no link for them in the D010INC table.
However, as your intention is to find text elements of a single form, and not mass searching, you can find out the INCLUDE manually.
Find out Adobe form function module name. You can do this either manually (Test button in SFP tcode)
or via the FP_FUNCTION_MODULE_NAME FM. Resulting Adobe Form function module name should follow /1BCDWB/SM0000XXX pattern.
Open this FM in SE37 and go to function group code (GoTo->Main program). There you will find something like this and there get the name of main INCLUDE, which holds the code.
Then by forward navigation go to INCLUDE code and by simple Find of %textkey-name pattern you can observe all text elements which were utilized in the form.

Delete row button in alv grid in sap

i am using function "REUSE_ALV_GRID_DISPLAY" in order to display a grid. My problem is that not all the buttons in alv toolbar are displayed. For example, i can not see the "delete row" button.
This is my call:
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IT_FIELDCAT = fieldcatalog
TABLES
t_outtab = lt_files_records_final
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
Can you please help?
IF you need the full editor functionality (including cell editors), you will have to move away from the (obsolete and unsupported) function module to the class CL_GUI_ALV_GRID. See the documentation here.
If you only need a delete button, it might be easier to add a custom button. Check the program SALV_DEMO_TABLE_FUNCTIONS for an example (and start using the ALV OM instead of the old function modules - much easier to code with).

How to translate with Qt Linguist with label arguments (%1)

I got a line for a Qlabel like this:
QString(tr("Are you sure you want to delete the scene called %1 ?")).arg(variable);
Some people told me you can't translate that. They told me to append different strings with the parameters and the text...
But what about a phrase using various parameters? How does the translator know which order if it is appended in that order?.
Has no sense for me. There must be a way!.
Should should drop the QString(...) part, since tr() already returns a QString. Otherwise I don't see a problem with the translation of the following code:
tr("Are you sure you want to delete the scene called %1 ?").arg(variable);
In the Use QString::arg() for Dynamic Text part of the Qt documentation you can find more information.

Resources