Qt Text Edit with a fixed number of input lines - c++

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();

Related

Is it possible for me to append a string to a specific line of a file?

I'm still fairly new to stackoverflow so I apologize if there is anything that I have done incorrectly. I have a csv file that is constantly being updated, but I want to be able to have the option to add a string to the end of a specific line. I have a method that is able to find the line that I want to append the string to, but I am not sure on how to add that string to the end of that line without creating a new line at the bottom of the csv file.
'''
QFile listOfStudents("listOfStudents.csv");
QTextStream read(&listOfStudents), write(&listOfStudents;
QStringList studentInformation;
QString junkLine = read.readLine(), line, studentLine,
appendingString = "Ralph Emerson";
if(!listOfStudents.open(QFile::ReadWrite | QFile::Text))
{
QMessageBox::warning(this, "title", "not open!");
}
while(!read.atEnd())
{
line = read.readLine();
if(line.contains(IDNumber, Qt::CaseSensitive))
{
studentLine = line;
studentLine += ", ";
studentLine += appendingString;
line.clear();
write << studentLine;
break;
}
}
'''
If studentLine were to be
12345678, Gary Bob, Kennedy Johnson
and I wanted to append the string "Ralph Emerson,"
the expected result would be
12345678, Gary Bob, Kennedy Johnson, Ralph Emerson"
However, the code above will just append the updated studentLine to the bottom of the file, rather than changing the line where it was initially at in the file.
Files are typically consecutive, bounded streams of bytes. Therefore, if you want to write in the middle of one, you will have to move forward all the rest to make space for the new one.
If your files are small enough, a simple solution is to parse them entirely, hold them in memory as a data structure, modify it, and dump it back.
The most performant solution, however, is to write as you are reading the file into a new one, then add the new one as you reach the point of insertion, and then continue copying the rest of the file into the output.

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);

Autohotkey if statement not working, no error msg

I'm trying to use autohotkey to gather a chuck of data from a website and then click a certain spot on the website depending on what the text is. I'm able to get it to actually pick up the value but when it comes to the if statement it won't seem to process and yields no error message. Here is a quick sample of my code, there is about 20 if statement values so for brevity sake I've only included a few of the values.
GuessesLeft = 20
Errorcount = 0
;triple click and copy text making a variable out of the clipboard
;while (GuessesLeft!=0) part of future while loop
;{ part of future while loop
click 927,349
click 927,349
click 927,349
Send ^c
GetValue = %Clipboard%
if ( GetValue = "Frontal boss")
{
click 955,485
Guessesleft -= 1
}
else if ( GetValue = "Supraorbital Ridge")
{
click 955,571
Guessesleft -= 1
}
;....ETC
else
{
Errorcount += 1
}
;} part of future while loop
Any tips on what I might be doing wrong. Ideally I'd use a case statement but AHK doesn't seem to have them.
Wait a second -- you are triple clicking to highlight a full paragraph and copying that to the clipboard and checking to see if the entirety of the copied portion is the words in the if statement, right? And your words in the copied portion have quotes around them? Probably you will have to trim off any trailing spaces and/or returns:
GetValue = % Trim(Clipboard)
If that doesn't work, you may even have to shorten the length of the copied text by an arbitrary character or two:
GetValue = % SubStr(Clipboard, 1, (StrLen(Clipboard)-2))
Now, if I am wrong, and what you are really looking for is the words from the if statement wherever they may be in a longer paragraph -- and they are not surrounded by quotes, then you will want something like:
IfInString, Clipboard, Frontal boss
Or, if the quotes ARE there,
IfInString, Clipboard, "Frontal boss"
Hth,

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.

How to put new line in QTableWidgetItem

I am having trouble to put new line in QTableWidgetItem. Here is the code :
QTableWidget* item = new QTableWidgetItem;
item->setText("Line1 \n Line2");
With this code the item text is not displayed in two lines, i.e it seems that \n character is ignored.
I have a workaround to do this, with using QPlainTextEdit and the function setCellWidget, in that case everything work as expected but is more ugly, and I prefer not to make additional widget just to show text in multiple lines.
I think your code is fine to separate lines. But you should change the size of each row to show multi-lines.
And after resizing row's height:
You see it's OK.