Controlling keyboard position in QTextEdit - qt

Is there any way to control the keyboard cursor in QTextEdit? For instance move the cursor one line up, or two positions back. I have looked at the QCursor class, but is this only for the mouse cursor? Thanks!

QTextCursor::movePosition(MoveOperation operation, MoveMode mode=MoveAnchor, int n=1)
This method allows you to move the cursor in various ways, such as one word to the right or up one line.
You can use it like this:
QTextCursor c = textEdit->textCursor();
c.movePosition(QTextCursor::Up);
textEdit->setTextCursor(c);
If you need to select some text, not just move the cursor, specify the MoveMode as KeepAnchor.

Yes there is:
http://doc.qt.nokia.com/4.7-snapshot/qtextedit.html#moveCursor

Related

How do I find the position in the document that is visible in a QTextBrowser?

When the user scrolls in a QTextBrowser in my application, I want to retrieve the position in the document that they've scrolled to (offset in the document, not the GUI position.)
If I can make the cursor jump to that location, I can get QTextCursor.position(). But I don't see a way to make the cursor jump to the visible location in the browser. The cursor stays where it is when I scroll.
I do not completely understand the description of your problem, but maybe you can determine the cursor (i.e. the position in text document) of the beginning and the end of the visible area by calling https://doc.qt.io/qt-5/qtextedit.html#cursorForPosition
QRect rect = textBrowser->rect();
QTextCursor firstVisible = textBrowser->cursorForPosition(rect.topLeft());
QTextCursor lastVisible = textBrowser->cursorForPosition(rect.bottomRight());
I have not tested it, but I think you get the idea. Maybe you will need to use the rect of textBrowser->viewport() instead of the rect of textBrowser. You need to experiment a bit with this to find what works for you.
Based on V.K.'s answer, here is my solution in Python:
browserRect = self.mainText.rect()
newCursor = self.mainText.cursorForPosition(browserRect.topLeft())
self.mainText.setTextCursor(newCursor)
textPos = mainText.textCursor().position()
Actually, just using QPoint(0,0) probably would work just as well, since the upper left of the browser's rectangle is pretty close to (0,0).

why we use cursorposition function of qlineedit?

I was reading the work of cursorposition of qlineedit in documentation it was written that
This property holds the current cursor position of this lineedit.
setting the cursor position causes a repaint when appropriate.
By default, this property contains a value of 0.
what does this lines mean.
The cursor position is the index of the character in the displayed string that will be changed if you type something. Usually, it is indicated by a (blinking) vertical bar and advances as you type.
If the string is Hello World, and the cursor position is 3, then the line edit will look like Hel|lo World, and when you type, e.g., x, it will change to
Helx|lo World.
By default, the cursor is placed at the beginning of the string, i.e., at position zero (0).
If the cursor position changed, a repaint is usually required, since the line edit is supposed to look differently (the vertical bar has moved).
I'm not sure when a repaint would not be appropriate. Maybe if the cursor is not visible.

How to set cursor shape to '>' in a QTextEdit?

I am trying to mimic a command-line client. I wish to set the cursor shape to '>', to show messages to user. I don't see that shape in the options provided by QCursor. Is there a way to set custom shapes to widget cursors?
You need to set the QTextEdit's viewport's cursor: http://doc.qt.nokia.com/stable/qtextedit.html
"The shape of the mouse cursor on a QTextEdit is Qt::IBeamCursor by default. It can be changed through the viewport()'s cursor property."
e.g. To hide the cursor completely:
ui.textEdit->viewport()->setCursor(Qt::BlankCursor);
are you talking about mouse's shape
or about the text caret
Check QTextLayout::drawCursor
You may think you want to do this, but you really don't. What will it gain you to change the mouse cursor to '>'? It will certainly confuse the user.

How to program scrollbar to jump to bottom/top in case of change in QPlainTextEdit or QTextEdit area?

How to program scrollbar to jump to bottom/top in case of change in QPlainTextEdit or QTextEdit area?
It looks like it doesn't have any controlling function.
QTextEdit and QPlainTextEdit are both inherited from QAbstractScrollArea. The QAbstractScrollArea object provides access to the scrollbar through the verticalScrollBar() method.
Thus, to jump to the top:
ui.textEdit->verticalScrollBar()->setValue(0);
And to jump to the bottom:
ui.textEdit->verticalScrollBar()->setValue(ui.textEdit->verticalScrollBar()->maximum());
This should work for both QTextEdit and QPlainTextEdit.
You can use the 'ensureCursorVisible' method:
void QTextEdit::ensureCursorVisible ()
Ensures that the cursor is visible by scrolling the text edit if necessary.
This is not a slot, though, so you can't connect it to any signal -- you'll have to create something yourself that you can connect to the void textChanged() signal.
Disclaimer: I may have misunderstood your question -- I assume you want to scroll down when some text is appended to the text.
When a text edit control is resized, QWidget::resizeEvent is called. You just have to override this function in your subclass, and call verticalScrollBar -> setValue (verticalScrollBar -> minimum()) (or maximum()).
I have done in Pyqt.
self.scrollArea.verticalScrollBar().rangeChanged.connect(self.change_scroll)
--------
#pyqtSlot(int, int)
def change_scroll(self, min, max):
print("cambio", min, max)
self.scrollArea.verticalScrollBar().setSliderPosition(max)
Here I am posting my Solution as above solution dint work in my case.
I want to get the cursor at the beginning of QTextbrowser.
By using QTextEdit::setTextCursor, you can move the visible cursor where you want:
// Go to beginning
QTextCursor textCursor = ui->textBrowser->textCursor();
textCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1);
ui->textBrowser->setTextCursor(textCursor);
Hope, it will help to some one and save their precious time.
I am using QTextEdit and textEdit.verticalScrollBar().setValue(0) doesn't work for me.
In my case, textEdit.moveCursor(QTextCursor.Start) can scroll to the top.

Individual QTreeWidgetItem indentation

Is it possible to have individual indentation of items in a QTreeWidget?
In specific, I have have a column containing both text, icon and for some of them a CheckBox. The items without a CheckBox gets shifted to the left so the indentation of the icon and the text is not inline with the others. Could maybe be fixed with a hidden CheckBox if that is possible?
Maybe the use of Delegates will give you a nice and proper implementation. You'll have the opportunity to re-implement the paint() and sizeHint() methods, and therefore, choose the way your QTreeWidgetItem are being drawn...
More documentation here : http://doc.trolltech.com/4.6/model-view-delegate.html
An example : http://doc.trolltech.com/4.6/itemviews-pixelator.html
Hope it helps a bit !
You can try using the QWidget::setContentMargins() on the widget returned by QTreeWidget::itemWidget().

Resources