Add String from textEdit to a QStack - qt

I am trying to capture the contents from textEdit and add it to a QStack. The content I am splitting so to be able to reverse the order of the sentence captured. I already have that part covered, but I want to be able to convert from QStringList to be pushed to the QStack. This is what I have:
void notepad::on_actionReversed_Text_triggered()
{
QString unreversed = ui->textEdit->toPlainText();
QStringList ready = unreversed.split(QRegExp("(\\s|\\n|\\r)+"), QString::SkipEmptyParts);
QStack<QString> stack;
stack.push(ready);
QString result;
while (!stack.isEmpty())
{
result += stack.pop();
ui->textEdit->setText(result);
}
}

QStack::push only takes objects of it's template type.
i.e. In your case you must push QString's onto the QStack<QString>, and not a list.
So, iterate the list of strings pushing each one in turn.
foreach (const QString &str, ready) {
stack.push(str);
}
Something else that is wrong is that you are updating textEdit inside the for loop. You actually want to build up the string in the loop and afterwards update the text.
QString result;
while (!stack.isEmpty()) {
result += stack.pop();
}
ui->textEdit->setText(result);
An alternate answer could be to do away with the stack and just iterate the QStringList ready in reverse order to build up result string.

Related

QPlainTextEdit: How to overwrite the text at the cursor instead of moving it forward

I have a QPlainTextEdit that is supposed to display process output. But when the process outputs CR to reset the line, i struggle to implement the same behaviour the terminal would do.
I basically need to do the same as if user pressed Home, then Insert, and then Ctrl+V with the new text in the clipboard.
I tried
textEdit->setOverwriteMode( true );
textEdit->moveCursor( QTextCursor::StartOfLine );
textEdit->insertPlainText( newText );
but it does not work as expected. The text is still inserted, moving the existing text in front of the cursor forward, instead of overwriting it.
What else can i do to achieve what i need?
I hope I understood your question correctly. So you actually only want to replace a word or text without the existing text being moved in one direction but being replaced.
So you can do that as follows. With an input (eg QLineEdit) you insert the word. The QPlainTextEdit has a find() function that returns true or false if the word is found.
with cursor.movePosition() you iterate over the position you need. The text to be replaced is swapped with the new text.
here is a small example:
void MainWindow::on_pushButton_clicked()
{
QString searchWord = ui->lineEdit->text();
if(ui->plainTextEdit->find(searchWord, QTextDocument::FindWholeWords))
{
QString updateWord = "YourWord/text"; // Your new Word here
QTextCursor cursor = ui->plainTextEdit->textCursor();
// for lines just use: QTextCursor::StartOfLine
cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor, 1);
cursor.insertHtml("<p style=\"background-color:red;color:white;white-space:pre\">" + updateWord + "</p>");
ui->plainTextEdit->insertPlainText(cursor.selectedText());
ui->plainTextEdit->setTextCursor(cursor);
}
}
Or even shorter:
QString searchWord = ui->lineEdit->text();
if(ui->plainTextEdit->find(searchWord, QTextDocument::FindWholeWords))
{
QString updateWord = "YourWord/text"; // Your new Word here
QTextCursor cursor = ui->plainTextEdit->textCursor();
// for single words, just use: QTextCursor::StartOfWord
cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor, 1);
ui->plainTextEdit->insertPlainText(updateWord);
}

QTextStream - What exactly does the position method return

I have a question about what a QTextStream is calculating with the pos() method. I assumed it was the number of bytes, but it seems that this might not be the case.
The reason I ask, is that I am processing rows in a file, and once the number of rows read reached some arbitrary number or stream.atEnd() is true, I break out of the loop and save stream.pos() to a qint64* variable. Once the processing is complete, I go back to the file and seek(*filePosition) to get back to my last position and grab more data until stream.atEnd() is true. This works in the sense that can keep track of where I am, but it is very slow calling stream.pos() as is noted in the Qt docs.
What I am attempting is to update the file position after each line is read in an efficient manner. However, it is not working and when the program goes back to read the file again, the position is not correct as the first line it reads starts in the middle of line previously read on the last iteration.
Here is what is have so far:
QTextStream stream(this);
stream.seek(*filePosition);
int ROW_COUNT = 1;
while (!stream.atEnd()) {
QString row = stream.readLine();
QStringList rowData = row.split(QRegExp(delimiter));
*filePosition += row.toUtf8().size();
/*
processing rowData...
*/
if (ROW_COUNT == ROW_UPLOAD_LIMIT) {
break;
}
ROW_COUNT++;
}
/*
close files, flush stream, more processing, etc...
*/
QTextStream::pos returns position in bytes. I see the following problems:
You are not accounting for the line ending character (or 2 characters)
In UTF-8, a single character might take more than 1 byte
Also, why save buffer position after reading each line? This might be faster:
if (ROW_COUNT == ROW_UPLOAD_LIMIT) {
*filePosition = stream.pos();
break;
}
The solution was to create the QTextStream outside of the function and pass it in as a parameter. Doing this allows me to not have to worry about tracking the position on each iteration because I keep the stream in scope until I have completely finished processing the file.
class FeedProcessor {
...
void processFeedFile() {
IntegrationFile file("big_file.txt");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream stream(&file);
while(!stream.atEnd()) {
file.extractFileContents(&stream);
/*
do more processing with file
*/
}
}
...
}
class IntegrationFile : public QFile {
...
void extractFileContents(QTextStream* stream) {
int ROW_COUNT = 1;
while (!stream.atEnd()) {
QString row = stream.readLine();
QStringList rowData = row.split(QRegExp(delimiter));
/*
processing rowData...
*/
if (ROW_COUNT == ROW_UPLOAD_LIMIT) {
break;
}
ROW_COUNT++;
}
/*
close files, flush stream, more processing, etc...
*/
}
...
}

QStringList "index out of range"

I am trying to reverse the words within a QStringList. Below is the code up to now, but I keep on getting a "Index out of Range" error. From the error it would seem that I am trying to use data that is out of scope, but not able to figure out my problem.
QString reversed;
QStringList reversedlist;
QStringlist list = input.split(" ");
for (int i=0;i<list.length(); ++1)
reversedlist[i] = list[list.length() -1 -i];
reversed = reversedlist.join(" ");`
Thanks
As pointed out by #ThorngardSO, the reversedlist is initially empty, and you are trying to access the invalid index in your loop code. You should add values to the list using one of the following functions:
STL-compatible function push_back() (inserts value at the end of the list)
STL-compatible function push_front() (inserts value at the beginning of the list)
Qt function append() (Qt alternative for push_back)
Qt function prepend() (Qt alternative for push_front)
As you see, prepend() inserts the element at the beginning of the list, that is why it makes the reversing of the list very simple:
for (int i = 0; i < list.length(); ++i) {
reversedlist.prepend(list[i]);
}
Also, note that there is a typo in your loop: it should be ++i instead of ++1.
Your reversedList is initially empty. You have to actually append the items, like this:
reversedlist.push_back (list[list.length () - 1 - i]);
Naturally, trying to access non-existing items via reversedList[i] does not work and throws the index-out-out-range error.
You got the index out of range as there is no sting inside the QStringList reversedlist. So when your code reach the line reversedlist[0], it throw the "index out of range" error. And you can read the value using [0] and cant assign.
if you want to assign the value to a particular index of the QStringList.
QString reversed;
QStringList reversedlist;
QString input="123 456 789 000";
QStringList list = input.split(" ");
for (int i=0;i<list.length(); ++i){
//value to particular index
reversedlist.insert(i,list[list.length() -1 -i]);
}
reversed = reversedlist.join(" ");
qDebug() << reversed;

QTableView: how to edit non-editable cells in the program?

How should this be done by using the model->setData() method call?
I have derived a class called "MyStandardItemModel" from QStandardItemModel. I have made my third and fourth columns non-editable by overriding the protected virtual flags method. This is how it goes:
#define TX_PACKET_COLUMN (4u)
#define RX_PACKET_COLUMN (5u)
Qt::ItemFlags MyStandardItemModel::flags(const QModelIndex& index) const
{
if (index.column() == TX_PACKET_COLUMN || index.column() == RX_PACKET_COLUMN)
{
return (QStandardItemModel::flags(index) & ~Qt::ItemIsEditable);
}
else
{
return QStandardItemModel::flags(index);
}
}
...
//Set model
ui->testCaseTableView->setModel(model);
Having this done, I am not able to edit the cells in the third and fourth column.
Now, I want that when I double click on these cells, a pop-up dialog comes up. I will modify some data in the editable field of that dialog, and then copy it back to the non editable cells inside the code.
I tried to just write a doubleclick() handler for the QTreeView and just copy some data to the cells just to see if it is possible to copy data to the non-editable cells.
This operation is failing, and the data is not written into the non-editable cells.
Here you can find the double click handler:
void MainWindow::on_testCaseTableView_doubleClicked(const QModelIndex &index)
{
QVariant variant;
variant.toString() = "AA";
if((index.column() == TX_PACKET_COLUMN)||(index.column() == RX_PACKET_COLUMN))
{
model->setData(index, variant); // set new value
}
}
The setData(..) operation is clearing the already written data in the cells, but string "AA" is not getting written. Please suggest how to copy some data to non-editable cells inside the code.
QVariant set is empty. Nothing needs to be wrong in your model. Error is on this line:
variant.toString() = "AA";
change to:
QVariant variant("AA"); // just for testing anyway
As I indicated in my comment, you have to fix this first issue:
instead of:
QVariant variant;
variant.toString() = "AA";
you should write
QVariant variant = QLatin1String("AA");
In general, you would look into the setData(...) implementation for such cases whether you emit the data changed signal properly and so forth, but here you are entering a prior issue which can lead to problems, so let us fix that.
Note, you should use QLatin1String to avoid the unnecessary explicit conversion from raw char* to QString. This is a good practice in general, and this is available with Qt 4 as well as Qt 5.
Although, you could also use the QStringLiteral macro for creating a QString very efficiently with template magic out of your raw literal, but that requires C++11.

Filling some QTableWidgetItems with QString from file

I'm trying to fill a QTableWidget from a file that was exported by my program, but when I try to set text to the table cells they just ignore me, nothing happens.
void MainWindow::on_actionOpen_Project_triggered()
{
QString line, fileName;
fileName = QFileDialog::getOpenFileName(this,tr("Open Project"), "", tr("Project Files (*.project)"));
if(!fileName.isEmpty()){
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
QTextStream in(&file);
for(int i=0;i<ui->code->rowCount();i++){ // goes through table rows
for(int j=0;j<ui->code->columnCount();j++){ // through table columns
ui->code->setCurrentCell(i,j);
QTableWidgetItem *cell = ui->code->currentItem();
in >> line;
if(!line.isEmpty()){
cell = new QTableWidgetItem(line); // relates the pointer to a new object, because the table is empty.
ui->errorlog->append(line); // just for checking if the string is correct visually.
}
}
}
file.close();
}
}
The errorlog object shows on the screen the correct values opened from the file, however the table is not filled. Any problems found?
This line:
cell = new QTableWidgetItem(line);
doesn't do what you think it does. Assigning the cell doesn't change anything in the table – cell is just a local variable, overwriting it doesn't have any effect elsewhere.
You (probably) want to do something like this instead:
cell->setText(line);
Or (if you don't really need to change the current item):
ui->code->item(i,j)->setText(line);
If those give you a segfault, it's that you haven't set the table's widget items yet. In that case, you should do something like:
QTableWidgetItem *cell = ui->code->item(i,j);
if (!cell) {
cell = new QTableWidgetItem;
ui->code->setItem(i,j,cell);
}
if (!line.isEmpty()) {
cell->setText(line);
}
This will populate all the cells with QTableWidgetItems the first time this function is called, and reuse them subsequently.

Resources