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.
Related
The text I'm searching for is all contained within a CSS class called "content-center", and within that is a series of CSS classes all with the same name that old similar, but different information. It seems to only be returning [<JSHandle preview=JSHandle#node>] rather than returning the text itself as if saying "yes, this text is on the page X times".
page.wait_for_selector('.content-center')
print(page.query_selector_all(".content-center:has-text('Bob Johnson')"))
page.query_selector_all returns the ElementHandle[] values of the elements which got found. Over these you can loop and call the text_content() method to get the text out of that specific element.
Also in most cases, its enough to use the text-selectors to verify something is on the page or an element has text, see here for reference.
I'm writing automation tests for a windows application using Sikuli/Robotframework, I want to be able to validate text that I see on the application but the values change so would be using a variable created at the start of each test run, is there anyway to validate that the text value is present in the application using Sikuli if not where should I look for validation?
I just had a look at SikuliLibrary
There is a Keyword called "Get Text" where it returns the text when an image file is passed. You can then store it in a variable and then make use of String comparison operation.
Get text - from library
Two ways in which you can retrieve text.
If image is not given, keyword will get text from whole Screen
If image is given, keyword will get text from matched region Call keyword setOcrTextRead to set OcrTextRead as true, before using text recognition keywords.
Examples:
Set Ocr Text Read true
Get Text
Get Text test.png
String comparison operation
Should be equal as strings ${only value} ${sele}
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.
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
In my dtd file for my localization strings for the xul of my addon, I have a very long string in which I need a carriage return.
<!ENTITY myentity.label "THIS IS A TEST OF THE EMERGENCY BROADCAST SYSTEM. **break** REPEAT: THIS IS ONLY A TEST.">
What can I put in please of break in my example?
My dtd file is encoded as UTF-8 without BOM.
I've tried (in place of the break):
\u000D
\u000D\u000A
%0D%0A
And I've tried adding a literal carriage return, too.
<!ENTITY myentity.label "THIS IS A TEST OF THE EMERGENCY BROADCAST SYSTEM.
REPEAT: THIS IS ONLY A TEST.">
but when the string shows in the dialogue window in Firefox, it still shows as one long line with no breaks - which means the text runs off the edge of the dialogue box.
It seems like I should use the unicode code for the character, but when I add that, it just prints literally.
You cannot put a carriage return in an entity directly.
If the entity is the text content of a <description>, you can add xmlns:html="http://www.w3.org/1999/xhtml to your window or overlay definition and then use <html:br/> in your entity.
The preferred way to do what you are trying to do is to set a max width on the XUL description entry via CSS and allow it to wrap. For this to work, the text must be a child of the description (not the value attribute).
See:
https://developer.mozilla.org/en-US/docs/XUL/description