How to implement specific functionality in a custom text object in pyQt? - qt

I need to implement a specific text object in QEdit PyQt. I need rich objects that will content special information, capture the keyboard arrow keys only when cursor text is on these objects. They need support selection and copy / paste operations also. Outside of them, the cursor must act as normal behavior. At this time, the object does not need to show images or svg etc. Is only for capture text cursor purposes and to store specific information related with the object.
I read that I can implement a customized text object with this example:
textobject.py
but the example does not contain information about implement new html tags, how to capture focus, etc.
My questions are:
How can I add a new html tag related with my custom object for loading and saving documents?
How can I detect when text cursor is on the custom object to modify keyboard cursor behavior?
PyQt documentation says:
"Copy and Paste operations ignore custom text objects."
So is no way to implement copy / paste functionality?
How can I access to my custom object content?
I want to have specifics methods to navigate within the information contained in the object. I could save this information in another object, but I need to synchronize the information with the cursor because a document can contain many of these objects.
Context for my application: I'm developping a very specific application for blind users. I am blind myself. I need to make very specifics customizations to the Richttext area for accessibility. The goal is to develop an accessible math equation editor. For this, I need to add support for mathMl tag. I will process mathMl content inside these tags in my custom object, this part is already implemented at basic level without using any GUI framework. At this time I don't need to show the equation in the screen, I let to screen readers interact with my editor via a COM object. At this time, the equation box is functional, but I need to integrate this functionality in a rich text area. the idea is, a document can contain N math expressions that can be read, modified and saved in a document.
I plan to implement accessibility for sighted people also in the equation editor, but it is beyond my skills and is not a priority right now.
Also I need to accessibilize tables in rich text area, currently tables are not accesible in QT. Then I think to extend qt table functionality. Style elements like headings etc, does not exist in pyQt. OK, you can write a paragraph with heading style, but screen readers can't detect it. So I need to develop this functionality. I could solve it with customized objects I think.
Currently it need to work in windows only. Maybe later I'll consider other platforms.
I started to program in pyQt yesterday, but I already read a lot of documentation. I wrote a basic rich text editor to test my requirements.
I already tried to implement this application with wxPython phoenix because I've been using WX for a long time. But I couldn't implement custom text objects and I didn't find documentation about it.
If pyQt is limited for my requirements, could you recoment another gui framework?
Y could consider another languages, I chose Python because Python has many math tools like sympy or numpy, and implement user scripting functionality in the app is very easy.
Sorry for my bad english.

Related

Qt Quick Dynamically Custom Layouts (like workspaces in an IDE)

The project I'm working on calls for workspace layouts for a large section of it, by which I mean that users can move around panes, resize them, close them, etc., like in VSCode (but more like Emacs since VSCode does not currently support both vertical and horizontal splitting).
The current methodology I'm considering to create a custom LayoutManager that has a json object and injects views into recursive Row and Column objects in a binary-tree-like structure. Before I start this undertaking, I'm wondering if there is a library or set of QtQuick (QML) elements that would let me do this a lot more easily or if there is a better approach to this problem. Or, please just tell me why I'm wrong in doing it this way.
Note: The technologies I'm using are C++ with Qt 5 (currently 5.10).
If you want highly customizable GUI, with the option of persisting UI configuration, it is best to design the whole thing model driven.
QML already has the necessary stuff - list views, repeaters and so on. QML is a little at odds with tree models, but you can essentially fake a tree by using lists of lists.
The model provided here will do the trick, with the added benefit it also supports declarative instantiation. So you can easily set up an initial GUI state declaratively, like you would with normal QML, but then allow the user to make modifications to that initial state which can then be saved and recalled.
Then all you have to do is bind the desired GUI elements to the underlying model data source objects.

Can I "instantiate" (parametrize) prepared ui form by placing it in another form in Qt designer?

I created a simple form object representing some arbitrary name and value pair:
It's intentionally very simple - any design is to be applied through the CSS styles. Apart from the UI, the form consists of C++ class that handles the data loading and custom property defined in Designer too - the name of the data source (eg. hostname).
So I have two values I need to parametrize:
the Name - the QLabel text
the datapoint - some hostname. This will be later loaded by the C++ class and tell it what data should be loaded
I was Imagine I would be able to create "instances" of my form manually in designer and assign parameters to them. Like in this fictional image:
Don't forget datapoint is the custom dynamic attribute. I am sure the described design can be easily carried out programatically, but I feel like the designer solution will be prettier - provided there's a legit way to do it.
Maybe it's wrong, but the only way I can think of is making your Qt Designer aware of your widget. Something like this.
AFAIK, widget propagation doesn't allow you to edit custom properties without code, so you need a full widget support. My colleagues intentionally do whatever posiible from sources, not IDE& Forms, so that the project becomes more portable, but you are to choose yourself.

Qt GUI in designer or in code?

When it comes to designing a GUI in Qt, I am hesitating between using the designer in Qt Creator, or doing everything in source code. I'm using Qt widgets and not QML.
If I use the designer I can easily create a GUI using qt standard widgets. But as soon as I need to subclass a widget to extend its functionality I have to build a Designer plugin to support my new widget. Is that correct? Or is there another way to it?
You can build all the GUI in Designer including custom widgets, and you can also build your custom widgets in Designer.
Designer does not need to interpret your custom widgets. Just use the promote functionality. With promote, you start with a plain widget within Designer and then tell the "real" class of it (your custom one) and the header file where it is decleared. The only drawback is that within Designer, it will stay looking like an empty widget.
In my experience, it is much better to use Designer for the GUI than writing source code yourself. You can easily change all the properties afterwards etc., and it is helpful even if you rely on custom widgets. Source code is not a good declarative language for GUI objects, with all the properties etc. Also you cannot play around, you would need to compile all the time just to tell "Is it better to have this text label in bold font?".
Sometimes I edit the XML files that are created by Designer by hand. For example, if I want to put a widget somewhere else in the object tree. If you don't mess up the XML, Designer will still read it and not destroy your changes. The only reason I see for writing GUI in source code is when you have repetitive elements, or dynamic changes based on data input, e.g. a for()-loop that produces elements. In my project I have some Selector Boxes that are filled with options in the source code.
And btw: If you prefer to write your GUI in code instead of using Designer, maybe you are not the right person to craft the GUI. Most programmers don't understand that while they are technically able to design a GUI, they are not always also competent in doing it.
http://hallofshame.gp.co.at/index.php?file=shame.htm&mode=original
It is a bit of a shortcut, but I often use a simple QWidget as a container for my custom widget. This way, I can setup sizing policies, put the whole thing in the layout I want before my custom widget is even in. Then, in C++, I add the custom widget as a child of the container widget.
edit: As ypnos mentioned you can promote the placeholder directly. You can find guidelines here

Best solution for user inputs like text input

I just wonder what will be the best solution for receiving text input from user in PlayN.
I didn't find anything that i can use to achieve this, i think that the best solution will be to render something like HTML inputs to write a text, but it will be not that simple because we need to be able to use in example virtual keyboard from android (on android platform) and regular keyboard on HTML backend. Even then i think it will be very difficult (or impossible) to evoke android keyboard in game...
I'm thinking about creating a widget in tripleplay UI library (because i will use it), but this will end with rendering virtual keyboard on screen for user inputs.. buttons from a-z etc...
I wonder is there any better solution for this, or i need to implement something like i wrote above (like tripleplay widget)?
There is already a Tripleplay widget for receiving text input called Field.
However, it is very primitive and does not yet work on mobile platforms (it will work on an Android device with a hardware keyboard). We need to provide an API in PlayN to display the virtual keyboard, but until then there's no way for it to trigger the display of the virtual keyboard.
I don't recommend using this for any substantial text input, however. It doesn't (and never will) support cut and paste, or language input methods, or anything of the other extremely complex features that users expect for text input.
I would like to add an API to PlayN like:
Keyboard.requestTextInput(String label, Callback<String> callback)
which would pop up the virtual keyboard, with an attached (native) text box, and allow the user to enter a single line of text using all of the machinery of the platform's native text entry support. This will allow them to cut-and-paste, and use language input methods, and provide an experience with which they are comfortable on the platform in question.
If your game needs more sophisticated text input (like a chat interface, or the ability to take pages of notes), you will probably have to create a separate interface for each platform that you wish to support, using native multiline text editing widgets and then "wire" those into your PlayN game. This will be more complicated than can be described in a simple SO answer, so you'll have to do some research and learn how PlayN manages the display on each of the backends that you wish to support.

Using QT to build a WYSIWYG Editor for a Custom Markup Language

I'm new to QT, and am trying to figure out the best means of creating a WYSIWYG editor widget for a custom markup language that displays simple text, images, and links. I need to be able to propagate changes from the WYSIWYG editor to the custom markup representation.
As a concrete example of the problem domain, imagine that the custom markup might have a "player" tag which contains a player name and a team name. The markup could look like this:
Last week, <player id="1234"><name>Aaron Rodgers</name><team>Packers</team></player> threw a pass.
This text would display in the editor as:
Last week, Aaron Rodgers of the Packers threw a pass.
The player name and the team name would be editable directly within the editor in standard WYSIWYG fashion, so that my users do not have to learn any markup. Also, when the player name is moused-over, a details pop-up will appear about that player, and similarly for the team.
With that long introduction, I'm trying to figure out where to start with QT. It seems that the most logical option would be the Rich Text API using a QTextDocument. This approach seems less than ideal given the limitations of a QTextDocument:
I can't figure out how to capture navigation events from clicking on links.
Following links on click seems to only be enabled when the QTextEdit is readonly.
Custom objects that implement QTextObjectInterface are ignored in copy-and-paste operations
Any HTML-based markup that is passed to it as Rich Text is retranslated into a series of span tags and lots of other junk, making it extremely difficult to propagate changes from the editor back to the original custom markup.
A second option appears to be QWebKit, which allows for live editing of HTML5 markup, so I could specify a two-way translation between the custom markup and HTML5. I'm not clear on how one would propagate changes from the editor back to the original markup in real-time without re-translating the entire document on every text change. The QWebKit solutions looks like awfully bulky to me (Learning WebKit along with QT) to what should be a relatively simple problem.
I have also considered implementing the WYSIWYG with a custom class using native QT containers, labels, images, and other widgets manually. This seems like the most flexible approach, and the one most likely not to run into unresolvable problems. However, I'm pretty sure that implementing all the details of a normal text editor (selecting text, font changes, cut-and-paste support, undo/redo, dragging of objects, cursor placement, etc.) will be incredibly time consuming.
So, finally, my question: are there any QT gurus out there with some advice on where to start with this sort of project?
BTW, I am using QT because the application is a desktop application that needs platform independence.
Given that I got no advice here, I decided to go with the QTextEdit approach, although I'm actually using a QTextBrowser that is set to be editable so that I can capture link navigation events. I will be using QTextCharFormat's with the link names set to unique identifiers in order to convert from the QTextEdit back to the custom markup. The QTextEdit supports images already, so I won't have to deal with those.
I think I will hit the biggest roadblocks with the fact that I need to be able to insert/grow/shrink tables whose cells can have Excel-style functionality. I have not yet figured that whole process out.

Resources