Write on the same line in TextEdit in Qt - c++

When we use ui->TextEdit->append("...") the text entered will be shown in a new line in the TextEdit section. What I want to do is to write on the same line... I've tried this code:
ui->textEdit->append("My name is:");
ui->textEdit->append("\b bla");
but it seems that "\b" isn't recognised by Qt

Maybe an option is to do the append outside of the QTextEdit to avoid having a new paragraph created:
QString text = ui->textEdit->text();
text.append("bla");
ui->textEdit->setText(text);

Related

File Not Being Created in QT C++ using QFileDialog

there I'm trying to make a simple notepad application. I've got a new file menu item. When the user clicks on that, it checks whether the text field is empty or not. If that is not empty then it prompts to save the work. If the user opts for yes then it asks for the location using QFileDialog. However, this code doesn't create the file at the provided destination. Can anyone figure out this code?
if(this->checkTextField()==false){ //checkks whether the textField is empty or not
QMessageBox::StandardButton reply = QMessageBox::question(this,this->appName,this->document_modified);
if(reply == QMessageBox::Yes){
QString filename = QFileDialog::getSaveFileName(this,"Save the File",QDir::homePath(),this->textFilter);
QFile file(filename);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.flush();
file.close();
}
}
Simply creating a QFile will neither create nor open a file on the filesystem. You have to open it to write something to it/read from it.

How do i load a textfile to QListwidget and also save it back

How do i load a text file into QListWidget , such that each line in the textfile is an item in QListWidget,
and once i populate it, i also want to save all items of listwidget into a text file , such that each item of listwidget is in a line in the text file , to accomplish that i am attempting this
QString temp;
for(int i=0; i< ui->mylistwidget->count();i++)
{
QListWidgetItem* item = ui->mylistwidget->item(i);
temp += item->text();
}
std::string total_script = temp.toStdString();
i got all the text of listwidget items in a string , what do i do next if i want to save it in a text file?
and also how do i load text file and populate my qlistwidget with items ?
Qt provides QFile and QSaveFile classes for convenient file input/output operations. QSaveFile is special in as it will only (over)write the destination file when you commit it. For both parsing and writing the file contents, you can use QTextStream, which exposes the file contents as a stream you can read from or write to, including conversion of various variable types.
For importing from file:
Use a QFile to open the file
Construct QTextStream with the file as argument
Use QTextStream::readLine() in a while-loop to read lines and create items
For exporting to file:
Use a QSaveFile, again construct QTextStream on the file
In your loop, use QTextStream::operator<< to append the item text to the stream
Call commit() on the file so it is written
Example without the loop:
QSaveFile fileOut(filename);
QTextStream out(&fileOut);
out << "Qt rocks!" << Qt::endl; // do this for every item
fileOut.commit()

How to indent multiline text behind lists in QMLs TextEdit

In QML Im using a TextEdit item for a text editor with highlighter code running behind it (QSyntaxHighlighter). When the user types a dash (-), it will be recognised by the highlighter code and formatted (like Markdown). But additionally I want the text to be indented behind the dash, when it is multiline. Just like it behaves with HTML lists.
This is how it looks like right now:
This is how I want it (the text aligns properly behind the dash):
I know this can indent the text:
QTextCursor cursor(currentBlock());
QTextBlockFormat textBlockFormat = currentBlock().blockFormat();
textBlockFormat.setIndent(1);
cursor.setBlockFormat(textBlockFormat);
An idea is to indent all the text by default and un-indent the lines with a dash or similar, but couldn't quite figure out yet how to achieve it.
Any other ideas?
Ok apparently there is also a list styling option. This is how you can change the style of a block:
QTextBlock block = textDoc->findBlockByNumber(i);
QTextCursor cursor(block);
cursor.beginEditBlock();
QTextListFormat::Style style = QTextListFormat::ListDecimal;
QTextBlockFormat blockFmt = cursor.blockFormat();
QTextListFormat listFmt;
if (cursor.currentList()) {
listFmt = cursor.currentList()->format();
} else {
listFmt.setIndent(blockFmt.indent() + 1);
blockFmt.setIndent(0);
cursor.setBlockFormat(blockFmt);
}
listFmt.setStyle(style);
cursor.createList(listFmt);
cursor.endEditBlock();
This could be used in a slot linking to the contentsChange signal from QTextDocument or in a highlighter.

What is the method to set the text for a QTreeWidget's header?

I've checked the documentation here and I can't seem to find a method for setting the text of a QTreeWidget's title or header.
Without setting the title QTreeWidget automatically uses the number '1' in my code. An example of what it looks like outputted is below. I'm presuming QTreeWidget has a method for this and I just can't find it.
You're looking for setHeaderLabel.
Note that the documentation says it adds a new column, so if your view already has column 0 with text "1", you may instead have to do the following:
if(QTreeWidgetItem* header = treeWidget->headerItem()) {
header->setText(0, "My Text");
} else {
treeWidget->setHeaderLabel("My Text");
}
Here is an another method to set header texts
QStringList headerLabels;
headerLabels.push_back(tr("text1"));
headerLabels.push_back(tr("text2"));
headerLabels.push_back(tr("text3"));
..
headerLabels.push_back(tr("textN"));
treeWidget->setColumnCount(headerLabels.count());
treeWidget->setHeaderLabels(headerLabels);

How to format the selected text in a QTextEdit by pressing a button

I want to format a selected text in a QTextEdit by clicking a button. For axample I want to make it bold if it is not-bold, or not-bold if it is bold. Please help me with an example.
EDIT:
Actually I have found already a code - qt demo for text editor which does what I need:
void
MyTextEdit::boldText(bool isBold) //this is the SLOT for the button trigger(bool)
{
QTextCharFormat fmt;
fmt.setFontWeight(isBold ? QFont::Bold : QFont::Normal);
mergeFormatOnWordOrSelection(fmt);
}
void
MyTextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
QTextCursor cursor = m_textEdit->textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
m_textEdit->mergeCurrentCharFormat(format);
}
But I can't understand what returnes the textCursor() method, and how the merging of properties is being done? Just some formats are being changed, some of them stay constant. How mergeCharFormat function understands what to change and what to leave as is. Please explain me just these 2 things.
Thanks.
The textCursor() returns a textCursor that contains the position of the cursor you use in the textEdit, see QTextCursor in Qt classes. So by selecting the text that is contained by the cursor start and end position, you have the text that is currently highlited.
As for the mergeCharFormat, I guess that it is used to apply a new state (bold, italic, underlined) and to keep the existing ones. Say your text is already underlined and you apply bold, you would want to keep both.
Hope this helps.