How to change background of Text block in QPlainTextEdit - c++

Given a QTextBlock retrieved from QPlainTextEdit, I want to change background of the text in that block. I know how to do this with the help of textCursor() but in this case textCursor could be somewhere else. I am traversing through the file text line by line and would like to change background of the current line irrespective of the cursor position. Please let me know if it can be done.

What's the problem with moving the cursor to the line you want to mark?
You can save an old cursor position if you need.
int oldPos = edit->textCursor().position();
QTextCursor cursor = edit->textCursor();
int oldPos = cursor.position();
int linePos = // get the line position
cursor.setPosition(linePos);
cursor.select(QTextCursor::LineUnderCursor);
cursor.setCharFormat(format);
cursor.setPosition(oldPos);
edit->setTextCursor(cursor);

Related

Why is QLineEdit::setCursorPosition(int) selecting(highlighting) text?

I have an editable QComboBox that allows a user to type in a name for a new object and add it to the list. They can also edit names for existing items in the list. The problem is...say I have an item in the list called "AF" and I want to rename it to "ABCDEF". My first problem was if I placed the cursor in between 'A' and 'F' and started typing the cursor would jump to the end after typing 1 letter. So I would get "ABFCDE" unless I manually moved the cursor after each letter typed.
I fixed this by using
// slot connected to textEditChanged(QString) signal from QComboBox
void textChanged(const QString &text)
{
int pos = QComboBox->lineEdit()->custorPosition();
stuff...
QComboBox->setItemText(idx, text);
QComboBox->lineEdit()->setCursorPosition(pos);
}
and that works but unfortunately this caused a new problem.
setCursorPosition will subsequently select (highlight) all text beyond the new cursor location. So in the "AF" to "ABCDEF" example... I place the cursor between 'A' and 'F', type B and the cursor stays after "AB" and before 'F' but 'F' is highlighted. The next key press will replace the 'F' entirely. It will highlight more than 1 character, it highlights every character to the right of the cursor after it is moved.
I tried this to no avail.
QComboBox->lineEdit()->deselect();
I also tried this just as a test and it incorrectly exhibited the same behavior.
QComboBox->lineEdit()->moveCursorBackward(false,2);
The false parameter is supposed to not select the text the cursor moves past but it does anyway.
Anyone have any ideas on what's causing this?
I also encountered this problem. Here's what solved it for me for anyone interested:
First connect the signal but make sure it is queued!
connect(_comboBox, SIGNAL(editTextChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)), Qt::QueuedConnection);
and for the slot:
void ViewListWidget::slotViewNameChanged(const QString& /*name*/) {
int index = _viewComboBox->currentIndex();
int cursorPosition = _viewComboBox->lineEdit()->cursorPosition();
// Since we are using a queued connection, get the current QLineEdit text
// instead of relying on the signal argument, which might be out of sync
QString name = _viewComboBox->lineEdit()->text();
_viewComboBox->blockSignals(true);
_viewComboBox->setItemText(index, name);
_viewComboBox->blockSignals(false);
_viewComboBox->lineEdit()->setCursorPosition(cursorPosition);
}
You should also probably disable auto-complete:
_comboBox->setCompleter(0);

EditBox Text Alignment using Cocos2d-x 3.2

I am using CCEditBox in the latest cocos2d-x 3.2 as textBox for user input.
now , i want to align text which i am typing in Textbox area , but it's always stays left align. i want to set it in the Center of my textBox.
I tried one Function named : setLabelAnchorPoint but it's not giving any effect to my label alignment. please help.
cocos2d::extension::Scale9Sprite *Playername_bgtemp = cocos2d::extension::Scale9Sprite::create("Name_bg.png");
auto _editName = EditBox::create(Size(1000,164), Playername_bgtemp);
Point absolutePosition = Point(ReferenceFrameSprite->getContentSize().width/2,4*ReferenceFrameSprite->getContentSize().height*0.20);
_editName->setPosition(absolutePosition);
_editName->setFontName("fonts/HelveticaLTStd-Cond_0.ttf");
_editName->setFontColor(Color3B::WHITE);
_editName->setLabelAnchorPoint(Vec2(0.0f,0.0f));
_editName->setPlaceHolder(" Name ");
_editName->setPlaceholderFontColor(Color3B::WHITE);
_editName->setPlaceholderFontName("fonts/HelveticaLTStd-Cond_0.ttf");
_editName->setMaxLength(10);
_editName->setReturnType(EditBox::KeyboardReturnType::DONE);
_editName->setFontSize(BgContentFontSize);
_editName->setDelegate(this);
bgFrameSprite->addChild(_editName,PopUpTag);
I did the trick with a text Label in the same position than the Editbox. I keep the Editbox with an empty string.
When start editing the edit box:
Hide the label.
Set the Editbox text with the label content.
When end editing the Editbox:
Set label text to Editbox content.
Show the label.
Set Editbox text to an empty string.

How to get dragged text position MFC

I'm trying to insert tag next to specific text.
I was get a knowhow to insert tag next to text..
It is to use Setsel() and ReplaceSel().
example,
char str = "< name >";
m_richedit.Setsel( position of start dragging of text, end position )
m_richedit.ReplaceSel( str, TRUE )
but, I don't know how to get position of start dragging of text in richEdit.
Is there anybody who has an idea?
Thank you.
You need to add the appropriate accelerator key like they explain here: Adding accelerators(shortcuts) in MFC - HOW? . Then in the handler of the accelerator key, use GetSelText, add your tags to the string you get and call ReplaceSel.

Retain the cursor position in QTextEdit

I have a QTextEdit control. It has a maximum limit (maximum number of characters it can hold). To implement this I have connected a slot to the textChanged() signal which removes the extra character when the total number of characters exceeds the allowed maximum.
With this I have some problems dealing with the cursor position. Can anyone tell me how to retain the cursor position in QTextEdit?
On your slot:
If num of chars exceeds maximum:
Ask the QTextEdit for the Cursor:
QTextCursor QTextEdit::textCursor() const
Set the return value as your textEdit cursor (cause it returns a copy). From doc:
Returns a copy of the QTextCursor that represents the currently visible cursor. Note that > changes on the returned cursor do not affect QTextEdit's cursor; use setTextCursor() to > update the visible cursor.
void QTextEdit::setTextCursor(const QTextCursor & cursor)
Ask the cursor to delete last char
void QTextCursor::deletePreviousChar()
(Edit)as code:
QTextCursor cursor = ui->textEdit->textCursor();
ui->textEdit->setTextCursor( cursor );
cursor.deletePreviousChar();
If number exceeds the limit or wrong character is typed I'm using:
ui->textEdit->textCursor().deletePreviousChar();

Qt Text Edit with a fixed number of input lines

I have a Qt "Text Edit" widget in my Gui and this widget is used to log something.
I add every line(s) this way:
QString str;
str = ...
widget.textEdit_console->append(str);
by this way the Text Edit height will increase more and more after each new line.
I want it act like a terminal in this case, I mean after some number (that I set) of lines entered, for each new line the first line of Text Edit being deleted to prevent it being too big!
should I use a counter with every new line entered and delete the first ones after counter reached it's top or there is better way that do this automatically after
widget.textEdit_console->append(str);
called ?
thank cmannett85 for your advise but for some reason I prefer 'Text Edit',
I solved my problem this way:
void mainWindow::appendLog(const QString &str)
{
LogLines++;
if (LogLines > maxLogLines)
{
QTextCursor tc = widget.textEdit_console->textCursor();
tc.movePosition(QTextCursor::Start);
tc.select(QTextCursor::LineUnderCursor);
tc.removeSelectedText(); // this remove whole first line but not that '\n'
tc.deleteChar(); // this way the first line will completely being removed
LogLines--;
}
widget.textEdit_console->append(str);
}
I still don't know is there any better more optimized way while using 'Text Edit'
One simple way is to turn the vertical scroll-bar off:
textEdit_console->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
This code move cursor to the first line and then select it until end of line, next it will delete the line:
widget.textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
widget.textEdit->moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
widget.textEdit->textCursor().deleteChar();
widget.textEdit->textCursor().deleteChar();