Count number of tab stops at beginning of a line QPlainTextEdit
-
Hi,
I am playing around with Qt and try to build a code editor. One feature shall be that if you hit enter in a QPlainTextEdit the number of tab stops (indent) is kept the same as in the line before. Somehow I am not able to find a code snippet that allows me to count the number of tab stops at the beginning of a line. How can this be done?
Thanks for any hints!
-
Hi,
One possibility could be to use a QRegularExpression with a capture expression to find the white spaces at the start of the line and put the same content at the start of the new line.
Hope it helps
-
Hi,
One possibility could be to use a QRegularExpression with a capture expression to find the white spaces at the start of the line and put the same content at the start of the new line.
Hope it helps
@SGaist said in Count number of tab stops at beginning of a line QPlainTextEdit:
One possibility could be to use a QRegularExpression with a capture expression to find the white spaces at the start of the line and put the same content at the start of the new line.
According to your suggestion build this code:
void CodeEditor::matchTabstop(QString lastChar){ // Get number of tab stops QString tabs = ""; QTextCursor curs = textCursor(); curs.select(QTextCursor::BlockUnderCursor); QRegularExpression rx("\\s+"); QRegularExpressionMatch match = rx.match(curs.selectedText()); qDebug() << curs.selectedText(); if(match.hasMatch()) tabs = match.captured(0); this->insertPlainText("\n"+tabs); if(lastChar == "{"){ this->insertPlainText("\t\n"+tabs+"}"); this->moveCursor(QTextCursor::Left, QTextCursor::MoveAnchor); this->moveCursor(QTextCursor::Left, QTextCursor::MoveAnchor); } return; }
It does what I want but I am wondering if it is a good way to do it or if I need to improve my code. For example I would not need the lastChar if I could get the character left of curs.
-
Do you mean the character at the line end ?
-
I'm using the following code in my source editor:
// auto indent void SourceEditor::addNewLine() { QTextCursor cursor; QString line=textCursor().block().text(); QString space; QChar c; int size=line.size(); // search for white spaces at the beginning of the line // recherche les espaces en début de ligne for(int i=0; i<size; i++) { c=line[i]; if(! c.isSpace()) { space=line.left(i); break; } } insertPlainText("\n"); insertPlainText(space); }
-
@SGaist said in Count number of tab stops at beginning of a line QPlainTextEdit:
Do you mean the character at the line end ?
Not necessary. I would like to know the character left of the current curser position.
this is some text.| And some more
In the case above the "." Is there a build in function for that?
-
I'm using the following code in my source editor:
// auto indent void SourceEditor::addNewLine() { QTextCursor cursor; QString line=textCursor().block().text(); QString space; QChar c; int size=line.size(); // search for white spaces at the beginning of the line // recherche les espaces en début de ligne for(int i=0; i<size; i++) { c=line[i]; if(! c.isSpace()) { space=line.left(i); break; } } insertPlainText("\n"); insertPlainText(space); }
-
You need to call setTabsStopWidth to fix the tab width
Of course it's only working well with fixed fontsvoid SourceEditor::setDefaultFont(const QFont &font) { document()->setDefaultFont(QFont(font)); // tab stop QFontMetrics metrics(font); setTabStopWidth(vTabWidth*metrics.width(' ')); }
vTabWidth is the number of spaces you like ( 2, 4, 8, ...)