How to show line number on side right QPlainTextEdit in Qt - qt

I am just learning Qt. I want to show line number of QPlainTextEdit. I found this link http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
and it worked. But now I want QPlainTextEdit to show line number with other controls on the form as in the image below.
Thanks!

The example from doc.qt.io that you've cited does exactly what you are asking. They subclassed QPlainTextEdit and extended it accordingly. If you just need line numbers, simply delete extra functionality from that example.

Related

How to do a context dependant layout in Qt Designer and PyQt5?

I have a group box or the like containing a combobox and some more widgets that shall not all be visible at the same time. Depending on the selection I want at the same place to be displayed a button or a line edit or a (second) combobox. So if combobox entry one is selected it shows the button, if entry two is selected it shows the line edit, if entry three is selected it shows the second combobox, while not showing the other two.
I know that I can do it all dynamically but this results in a lot of code lines. I want to use QT Designer and a grid layout and I can't see how to put two widgets at the same place and then make only one visible programmatically.
I hope I could explain properly.
I played with Qt Designer but could not place two widgets over one another in a grid layout.
Thanks for any help.
Thank you G.M.
Yes, I was looking for QStackedWidget. I wasn't aware of this at all. I tried it and it works easily out of the box.

Qt: Tab Icon when QDockWidget becomes docked

Qt's QMainWindow has a ability to dock windows derived from QDockWidget. It also would put one on top on the other if few of them are stacked, producing a tab bar. Whenever a QDockWidget's state changes a signal topLevelChanged() is emmitted. At this point I would like to get access to underlying QTabWidget to set an icon for a a newly added tab. How can I do it? My patience is over trying to dig the answer out from Qt's documentation and source code. Thank you in advance.
So icon I want to be on Contents/Index tabs.
Once at least one dockwidget has been tabified, the main-window will create a QTabBar to provide the dock-tabs. Each dock-area can have its own tab-bar. These tab-bars will become children of the main-window, so you can use findChildren() or children() to get references to them.
The main difficulty will be in finding which dock-widget belongs to which tab and in which tab-bar. If the dock-widget window-titles are all unique, you can just search using the tabText(). Otherwise, you might be able to use the tabData(), which Qt sets internally to a quintptr from the dock-widget.
Once you have the correct tab, you can of course use setTabIcon() to add your icon. But note that every time a dock-widget is untabified or moved to another tab-bar, the icon will be lost.

What is the best way to implement this using Qt

I'm totally new to Qt, so I'd be glad to have a wide answer.
Here I drew up some model:
We have a kind of table that contains:
an integer value with a spinbox.
a cell with three(not specifically) grouped radio buttons
Editbox
A button that interacts with this particular editbox.
Also we have 2 buttons to add and remove items from the table.
I did some google search and found out that it can be done via QTableView.
Is there any way to put such complex structures into a cell? Must it be a separate class inherited from QTableView?
If you're going to have up to a hundred or maybe a few hundreds of elements in the table, then use QTableWidget.
If you're going to have too many elements (about thousands), then go for QTableView, and learn model-view programming.
The reason why I recommend QTableWidget is because you're a beginner. All you have to do there is create a widget, and use setCellWidget() and you're done.
If you have thousands of rows, then you're gonna have to draw the widgets yourself using QStyledItemDelegate, which will paint the widgets inside your QTableView. This is a very painful thing to do, but there's no way around it. The reasons you can find here.
I see at least three options to implement that in Qt:
Use a QtableView or QTableWidget and insert some custom controls in it. See comments made be other persons to your post
Use a QGridLayout and fill it with your controls by line and column
Make your own QWidget to store and manage the line elements (the spinbox, edit field, radio button) using a QHBoxLayout. You can design this in QtCreator, it can have it's own .ui. This could make it easy to handle the interaction between each QWidget of a line (directly handled by your QWidget class). Later, you can put an instance of it for every line you need in a QVBoxLayout.
Personnaly, I would go with the last option, but it may not work smartly if the controls of each line have different content/size (see comments), then first options should be prefered.

QTextEdit: scroll beyond last line

I display some rich text contents using a subclass of QTextEdit.
Some editors - especially code editors & IDEs offer a possibility to scroll beyond the last line all the way until the last line gets to the top of the view.
I'd love to be able to do the same thing using QTextEdit. Is this somehow possible? I've been reading the doc but could not find anything.
Thank you.
I never had used QTextEdit that way, but maybe you can solve your problem with scrollToAnchor(QString arg) (which ensure the 'arg' presented in text will be visible), or ensureCursorVisible().

QTextEdit word blocks and text formatting

I need to make a QLineEdit with non-editable word blocks.
For instance, imagine I drop a word (that actually represents a value) in a QLineEdit.
I would like to write normally on the QLineEdit, BUT if I try to move the cursor inside a word it would move over it, ie, place the cursor at the end/begining of the word.
Also, I would like the word to always show a certain highlight over it, with different colors (to emulate a box that would wrap it).
Any ideas on how to do this? Would a QTextArea be better for this? I was thinking of a QLineEdit because it would actually be inside a QTableWidget cell, so I think it may have more or less the same capabilities, am I wrong?
UPDATE I found these two functions that actually might be helpful for QLineEdit: cursorWordBackward() and cursorWordForward(), so this might help move over the words. Anyone has examples of this?
UPDATE 2 As QLineEdit does not support rich-text, the "highlight" feature I required can only be achieved with a QTextEdit. This has other issues: QTextEdit does not have the functions I mentioned in the first update above, and I don't know if it is possible to put a QTextEdit inside a QTableWidget cell.
QLineEdit can contain only plain text and certainly can't do this. I was thinking about QTextEdit, it can display HTML. But looking at the documentation, I realized that there is no way to insert non-editable block in QTextDocument (which is used by QTextEdit). I think there is no simple solution.
You can try to implement it manually. Catch textChanged() and cursorPositionChanged() signals of QTextEdit, analyze its content and cursor position and modify them if user moved the cursor into non-editable block or changed its content.
Maybe you could achieve this by using an inputMask...

Resources