I'm fairly new to Qt4.5, and I'm unsure of the approach I should take to the following problem.
I'm trying to create a QListWidget style widget, that can dynamically expand to an arbitrary length, probably no more than 300 elements. I tried to use the QListWidget, but I need to store three QStrings per line, and QListWidget is fairly limited in that regard. I need to be able to add, edit and delete lines. Sorting is not necessary.
I tried to use QTableWidget, but it's not fitting the bill either. I could make it work, but it would be an ugly hack.
I've tried to 'roll my own' version of QListWidget, but with three QStrings, rather than one, but it's a tedious procedure. Doable though.
My Question is this, should I go with the last option, or is there a better approach? I've been browsing through Trolltech's documentation, but there's a lot there, and the stuff I've done so for has been unsuccessful (obviously).
Any recommendations? Thanks in advance!
That sounds like a table. 300 rows and 3 columns. If that indeed is the case you should retry using the QTableWidget. (Preferably without any hacks.)
If you're trying to do something else, (Can't quite imagine what.) you should try QTableView combined with a QAbstractTableModel that you subclass to provide the content.
Related
In Sublime Text, if you hold control while clicking, you can add another cursor to the document, allowing you to type and edit in multiple places at once. I'm trying to recreate this with Qt's QPlainTextEdit. The logic seems pretty simple...
On Ctrl+click, create and store a new cursor at location of the click
Draw all cursors
When any key events happen, make sure the other cursors also receive them
However, it seems that in order to do these things, I have to understand exactly how QPlainTextEdit works, and while I have a loose understanding, I find the source to be very dense and difficult to navigate.
While this feature is very important to me, I can't afford to spend a month on it. I don't know how to reasonably proceed. Is there some open source text editor built with Qt that has this feature? Can I in fact implement it without thoroughly understanding how QPlainTextEdit works? Is there some other solution I have not considered? I thought this would be trivial, or at the very least someone might have solved it before, but that doesn't appear to be the case. Any advice is appreciated.
Is there some open source text editor built with Qt that has this feature?
QtCreator is open-source (source code) and has partial multiple-cursor functionality. While it does not support Ctrl+Click to add a new cursor, you can do Shift+Alt+DownArrow to add a cursor to the following line (same column), like in SublimeText, and then insert text to all the lines at once.
It seems implemented in their class TextEditorWidget, which inherits QPlainTextEdit and reimplements plenty of its virtual methods. I don't exactly know how they implemented the multi-cursor functionality, but probably looking at the code of TextEditorWidget can give hints.
However, keep in mind that QtCreator does not have full multi-cursor functionality like in Sublime Text or CLion: the mutliple cursors are necessarily in the same column, and you cannot move the multiple cursors left/right. For example something I often do in Sublime is multiple selection at the beginning of lines, followed by Ctrl+RightArrow to jump all cursors "one word to the right". You cannot do that in QtCreator as of August 2020.
There is an open feature request for having true multiple-cursor functionality in QtCreator:
https://bugreports.qt.io/browse/QTCREATORBUG-16013
However, it seems that in order to do these things, I have to
understand exactly how QPlainTextEdit works, and while I have a loose
understanding, I find the source to be very dense and difficult to
navigate.
To some extent, but not a lot. Once you get the cursor positions, you know the positions in the document where text needs to be inserted. QTextEdit already supports multiple cursor positions:
https://doc.qt.io/qt-5/richtext-cursor.html
Rich text documents can have multiple cursors associated with them,
and each of these contains information about their position in the
document and any selections that they may hold. This cursor-based
paradigm makes common operations, such as cutting and pasting text,
simple to implement programmatically, yet it also allows more complex
editing operations to be performed on the document.
and
Multiple Cursors
Multiple cursors can be used to simultaneously edit the same document,
although only one will be visible to the user in a QTextEdit widget.
The QTextDocument ensures that each cursor writes text correctly and
does not interfere with any of the others.
Since this feature already exists, it may not be as difficult as thought to read the documentation and implement the feature.
As QPlainTextEdit is implemented by using most of the tech behind QTextEdit and QTextDocument, maybe it will meet your requirements.
Well, in my current project, the records are loaded from a database directly into QStandardItemModel and then view via QTreeView(). Everything works fine too. But I thought about the ram-memory. I think that the QStandardItemModel, including records, will be loaded into ram-memory. Similar to the data structure (for instance list), right? But what happens when I have large data sets? I read in Qt Documentation: Fetch More Example that I have to add items to the model in batches and preferably only when the items are needed by the view. It sounds good, but I have no idea how to do that. Does anyone of you know a few examples in python codes? Unfortunately, I do not understand the C ++ examples. Or does anyone know a better solution?
Don't get me wrong. I don't want a ready solution because I'm too lazy. No! I want to learn and work on it.
I am terrible new to Qt programming, so I wan't to code some simple first projects.
I am trying to create a soduko solver. The problem is, that I now use 9^2 text edits to creat the "sudoke field". I think this is a terrible way to solve this.
Is there a good way in Qt to solve my problem? Like a matrix-text-edit-class or some shortcuts i could use. Working with 9^2 text edits is a little bit annoying.
Thanks for any suggestions and your time.
You can use a QTableWidget (or a QTableView) with 9 columns and 9 rows:
I'm trying to create structure to draw many graphs in separate sub windows one below another, something that looks like that :
I need to change the size of these graph by dragging lines separating them.
I tried using panes in wxAUI but resizing one affects other, and it seems to be rather unstable. The main problem is that when I'm moving one sash to another it starts pushing it. Maybe there is some way to solve it?
I also tried using multiple wxSplitterWindow each nested in another, but this strategy also seems to fail because resizing one window affects all nested inside and their splitters are moving due to sizing even if I try to cach event EVT_SPLITTER_SASH_POS_CHANGED.
Do you have any ideas how to solve that?
I'm afraid there is indeed no out of the box solution doing what you need. wxSplitterWindow itself is implemented using wxWidgets API, so you could adapt its code to create your own window supporting multiple splitters, AFAICS it should be quite straightforward but, still, will require some work.
I have an application that has some sort of log viewer, that needs to be able to render formatted log records that are inserted to it in real time.
I am writing this in Qt and so far I was using QTextEdit for rendering of the text, which was beautified using some html tags and later inserted either with setHtml or insertHtml.
The problem is however, that these functions are insanely CPU intensive and run for ages, hanging whole application.
A similar question was asked here: QTextEdit.insertHtml() is very slow
Apart it has no really useful answer, other than stating it's really slow, it actually asks a different thing. I don't need to speed up setHtml or insertHtml. I am willing to use entirely different technique if possible.
Is there any mechanism within Qt that would allow for really fast insertions of text? Or maybe even completely different component than QTextEdit?
Is there any way to append new line to QTextEdit which contains rich text (generated in any way) that is really fast?
I also noticed QTextBrowser but it seems to be just an extension of TextEdit, could it be faster?
You should give QPlainTextEdit a try. It uses the same technology as QTextEdit but is a lot faster. It is optimized for plain text handling but do not let that fool you, it still has some basic support for formatting using HTML. You can append HTML formatted text with appendHtml().
In my application, I also need to display a large log of the task, approximately 3500 lines. Some lines of the log should be colored. For this, I used HTML formatting. QTextEdit.setHtml with this amount of text, freezed my GUI.
I replaced QTextEdit with QListWidget, in which QListWidgetItem is created for each line of the log.
It began to work much faster, without friezes.
And I saved colored text, just simple by using the QListWidgetItem.setForeground.
[This post][1]
[1]: Performantly appending (rich) text to QTextEdit or QTextBrowser in Qt contains an answer to this problem. The gist: instead of simply appending an HTML snippet, manipulate the underlying document directly.
However, I suggest that if your display really is a list of single lines, you create a QAbstractListModel QAbstractTableModel derived class instead and show it in an item view.