I don't understand. I setup char format, block format, root frame format, and page size for all of the text in QTextEdit control. And then if I manually delete all the text, and start to type new one, or if I select all text and paste new one from buffer, then voilà! - all the formatting loses.
Is it possible to set some default format for QTextEdit (char's, block's, page, etc.)?
I've solved it the next way.
Handled QTextEdit::currentCharFormatChanged signal(as vahancho promted), and call QTextEdit::setTextCursor with needed formatting cursor. It solves the problem with char and block format.
For the pageSize and rootFrame's format, I've handled QTextEdit::document::documentLayout's update signal and if rootFrame format or pageSize of the document was changed, then resetup the needed sizes again.
Related
I am getting error in compile mode for the statement text1=text2. What is the issue with the statement and how can we resolve it?
There is a section in the Wynsure Development Rules dedicated to this question, look for the section "14.3 Texts" I'll summarize:
A text variable is a pointer to a buffer that gets automatically reallocated as we write to the text, therefore the memory location differs as the text grows, changes or shrinks. For this reason the pointer isn't contant. eWAM automatically allocates a new larger buffer and copies the content of the old buffer to the new buffer, frees the old buffer and then updates the MyText pointer to point to the new buffer.
Shallow Assignment
It is the most dangerous operation if you really do not understand how text variables work.
For example the following code will crash (as a general rule what will cause a crash is either accessing to a memory location that has been freed or freeing the same memory location twice.)
MyText2 = MyText1
Write(MyText1, ….)
Blank(MyText1)
Blank(MyText2) // Crash
The shallow assignment MyText2 = MyText1 simply copy pointers. It does not duplicate buffers.
When copying a text from one variable to another one, use a deep copy :=
If you use = it points to the original text var address. And then when you set the second text to blank, it in fact sets the original text to blank (dispose).
Dedicated Text Methods
Text has a number of custom methods that handle them correctly such as the var.type.write function
var.type.blank function
Procedure Test
var tmpText : Text
tmpText.type.Write(‘Hello’, #tmpText)
tmpText.type.blank(#tmpText)
endProc
Note that we always need to blank the old variable text after we move the content so that there isn't a memory leak.
procedure AnotherProcedure
Var MyText : Text
Var tmpText : text
tmpText= GetXXXText
Write(MyText, tmpText )
tmpText.type.blank(#tmpText)
endproc
how can I enter a 2KB formatted TXT (line breaks) that should get URL-Encoded? Just pasting it into the input line doesn't work as the result is not formatted in any way.
https://www.dropbox.com/s/z934uvy6bjz98e3/Screenshot%202016-04-13%2014.28.16.PNG?dl=0
If you have a really complex text, I recommend you to just keep the text in a plain text file somewhere in your disk, and use a File Dynamic Value to reference it, and wrap the whole thing in your URL-Encode Dynamic Value.
Some steps:
Create a file with your content
In your URL-Encode dynamic value, right-click on the Input field and pick File > File Content
Click on the newly created File (Not specified) token, and you'll be prompted to either pick or drag-and-drop a file
You should be good to go. And whenever the file changes, you can send the request, and it should be up-to-date, it's just a pointer to the file.
If you use proper URL encoding, the linebreaks \n should be encoded as %0A. This
foo
bar
baz
will be encoded as
foo%0Abar%0Abaz%0A
I have a QLineEdit for Date in mm/dd/yyyy format. I am getting input using the keyboard and not using QDateEdit because of the requirement. And when the lineEdit comes to view, it has to show to the user the current date. I need the following for the lineEdit.
I need the two slashes always to be displayed and the cursor has to skip while entering or deleting.
I should not allow the user to enter an invalid date i.e while entering itself the lineEdit should not get invalid numbers.
I have to set the current date as the default text when the lineEdit comes to view.
For the first point, I tried using setInputMask("99/99/9999") but with this I can't set the current date using setText(). And how to use QRegExp to not to allow lineEdit get an invalid number while employing setInputMask()?
QDateEdit will serve your purpose.
use setDisplayFormat("dd/MM/yyyy").
QDateEdit wont allow invalid dates
You can use QDateEdit::setDate() obtained from
QDateTime::currentDateTime()
For setting text into QLineEdit with setInputMask("99/99/9999") you should format text depending on your mask:
lineEdit.setText("{:02d}/{:02d}/{:04d}".format(m, d, y))
Alternatively, you can temporary disable InputMask, format your date without /, set it and re-enable InputMask. But make sure that number of symbols in every part is correct.
lineEdit.setInputMask("")
lineEdit.setText(date_str.replace("/", ""))
lineEdit.setInputMask("99/99/9999")
I have a QPlainTextEdit widget that holds text that the user entered. The text might contain \n characters or it may all be on one very long line. My objective is to print this text on a printer (on paper) with word wrapping. The functions QPlainTextEdit::print() and QTextDocument::print() are not suitable to me because they both print the page number at the bottom of the page, which I don't want, and second, I can't seem to be able to control which pages to print (for example if the user only wants to print page #2 out of 5 pages) - the entire document is always printed.
Basically I am using a QPainter object to paint the text on the printer. The main difficulty I am facing is determining when to call QPrinter::newPage() function. How do you determine how much text will fit on a page? If the text is on one long line and the line is being word wrapped, how do you know when the first page is full and when to start a second page? I use the following code to draw:
painter.drawText(printer->pageRect(), Qt::TextWordWrap, ui->plainTextEdit->toPlainText());
painter is of type QPainter; printer is of type QPrinter; plainTextEdit is of type QPlainTextEdit.
To get the vertical size of your text, call painter.boundingRect( painter.window(), myText ).height();. When that exceeds painter.window.height(), it's time to call newPage().
Now it's just a matter of building up your text word-by-word until the boundingRect height exceeds the page height. I'd suggest keeping a "safe" QString that you know will fit on a page and an "unsafe" QString that you just added the new word to. If the new word didn't exceed the height, then assign the safe string to the unsafe one. (Qt has some optimizations like shared copying to keep this from being too compute intensive).
To deal with individual words in a QString, you'll want to play with indexOf() or split() using their QRegExp variants so you can search for whitespace like spaces, tabs, newlines.
You'll have to account for a single "word" that itself won't fit on a page, and split it up mid-word. There may be other devils in the details but hopefully that gets you a good start.
I'm trying to display Chinese characters encoded in UTF-8 in a PlainTextEdit control, but it doesn't render them properly.
My data comes from a database and I know that the string I get in Qt is correct (the bytes are the same as in the database). Once I have the Chinese character in a QString, I tried various things to display it but always results in either question marks or random ASCII characters:
QString chineseChar = query.value(fieldNo).toString(); // get the character
ui->plainTextEdit->appendPlainText(chineseChar); // doesn't work
ui->plainTextEdit->appendPlainText(chineseChar.toUtf8()); // doesn't work
ui->plainTextEdit->appendPlainText(QString::fromUtf8(chineseChar.toAscii()); // doesn't work
Any suggestion on how to handle that?
"My data comes from a database and I know that the string I get in Qt is correct (the bytes are the same as in the database)."
How did you check that? Try with chineseChar.toUtf8().toHex().
Once your string data is in a QString, all UI elements accepting a QString will handle it correctly. Usually the error happens when converting from plain text data(const char*/QByteArray) to the QString.
The conversions here:
ui->plainTextEdit->appendPlainText(chineseChar.toUtf8()); // doesn't work
ui->plainTextEdit->appendPlainText(QString::fromUtf8(chineseChar.toAscii()); // doesn't work
convert the unicode string to a bytearray, and then implicitely back to a QString, as those methods expect a QString.
I suggest you define QT_NO_CAST_FROM_ASCII and QT_NO_CAST_TO_ASCII to avoid any unwanted QByteArray<->QString conversions.
If the string is wrong, the error usually happened before, when converting from QByteArray/const char* to QString, i.e. in query.value(fieldNo).toString(). Try with:
QString chineseChar = QString::fromUtf8( query.value(fieldNo).toByteArray() );
If that doesn't help, the problem is somewhere in QtSQL assuming the wrong encoding for the data it receives from the database.