What's the different between txt and QTextEdit about their style - qt

When I copy the content from txt to the QTextEdit, I find the style of the content has changed.I'm so puzzled.Is this the reason for coding?How can I solve it?(System: Mac OS)

You either need to use a fixed-width font in your QTextEdit, or you need to handle text pasting yourself and append the text in HTML format, wrapped in <pre> </pre> tags.
It seems you don't have a need for complex, rich text editing, so just setting the font is probably what you want here:
#include <QFontDatabase>
// ...
myTextEdit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
This will pick a suitable fixed-width font and should work on all platforms.

Related

Margins on parts of QML TextEdit

In a QML TextEdit, I'm trying to have parts of the text indented and/or formatted otherwise (force uppercase, italic, bold, etc...) while other parts have different styling (screenplay formatting). My initial idea was to wrap every part into a <p class = 'dialogue'>...</p> or <span class = 'bold'>...</span> and then have a CSS file with margins etc. set accordingly.
However, I see no way to set a stylesheet for TextEdits (there's a function setDefaultStylesheet for QTextDocuments, but its properties can't be modified). Additionally, there is the question if e.g. CSS's margin-left: 50% would work correctly. From what I have seen, CSS does not seem to be supported for most of QML (although some discussions have hinted at partial support for TextEdits).
Styling only in QML would be okay, too, though I don't see how this would work for parts of the TextEdits contents.
Is there a way to make this concept work or am I going in the wrong direction?

Monospaced font to align text using spaces makes it look odd

I was using the default html font - Liberation Serif—30 glyphs in my page. Then i faced a problem, that multiple spaces to align two columns of text is not working properly.
I found out, spaces are not displayed at the same width as other characters and hence the same number of characters doesn't use same width. so, i started using monospaced font. It solves the alignment problem.
The alignment looks good now but the font is not the commonly used one as per the corporate standards.
How can i use a font like Liberation Serif and still do alignment using space characters?
If you want to align text in columns, how about using a <table>? Or if it's not tabular data, some CSS and float: left;.
To use a monospace font for primitive formatting in HTML, you can use the <pre> ... </pre> element. In fact, this tag will automatically select a monospace font for you:
<!-- the following shows the HTML, and the output will have the same column
formatting as it appears between the 'pre' tags -->
<pre>
9 328
2,345 208
xyz 4
</pre>
<pre> stands for "preserve." You can also apply it as a css style to other types of HTML tags. In this case, you would need to explicitly specify the monospace (such as "Consolas" on Windows) font to use:
<div style='font-family:Consolas; white-space:pre;'>
9 a
2,345 quick
xyz f o x
</div>
Make sure the text editor you use to create the HTML file is set to insert space characters. The layout might not look the same in the browser if it has any tab characters, or a mixture of tab and space.
Also, since the column and row formatting of the entire text inside a <pre> tag is preserved exactly, the examples shown above this would include an initial "line break" before the first row which (unlike the final line break) you might not want. It's because the text that's included actually starts immediately after the > character of the opening tag. There a few ways to address this, which are left as an exercise for the reader.

Apply stylesheet to HTML content in QTextEdit

I have a QTextEdit where I do display some HTML. Could I apply a stylesheet to that very HTML content?
Do not confuse it with applying a Qt stylesheet to the QTextEdit (that I know). I want to change the appearance of what is in the QTextEdit widget ("HTML").
--- edit ----
Related: Default HTML style for controls in the Qt library
You can have a look at Qt ignores CSS in QTextDocument which uses QTextDocument and the defaultStyleSheet property / setDefaultStyleSheet() http://doc.qt.io/qt-5/qtextdocument.html#defaultStyleSheet-prop
Note that QTextEdit only supports the following html subset http://doc.qt.io/qt-5/richtext-html-subset.html if you want to do more then that you are probably best of using the QWebkit or QWebEngine modules depending on which Qt version your targeting.
As per Qt5, many CSS properties are supported:
The following table lists the CSS properties supported by Qt's rich text engine.
The best way to apply them is to start your document with <style> and use classes. However the <node style=""> attribute also works.

CSS: font-family, if not one font, then none at all

So, I know that this isn't something that is normally a good idea for a website, but I have a special purpose/intent for such a use:
I have a multilingual dictionary that I'm working with online, where I need one of the languages to be in a specific font, from a file that I specify locally. However, I want this language to be rendered ONLY in this font, as if it is rendered using any other font, it will render incorrectly. That's all fine and dandy, and I can load the file in CSS and whatnot.
But I want to make it so that if it can't load that file, either for one reason or another, or something goes wrong, it can't go to another font. Basically, render this text using this font, and if you can't do that, don't just try and render it with Arial or whatever is the default -- show me blocks, show me a stark something.
I've spent a bit looking around, but am not sure what in CSS I would be using for this. Suggestions/help? Thanks :)
As an update to this question, since April 2013 there exists the Adobe Blank Font, which can be used for that purpose.
You may build a cross-browser css with FontQuirrel WebfontGenerator and the Adobe Blank font files.
If you just need the font in OpenType format you can use this single css file with the already embedded font
You can't do this. Text is text and text has to have a font that it is to be rendered in. If you really want, there's probably some weird JavaScript function that can detect the actual font being used for the text and if it doesn't match the one you want, then you can hide it or something. But in the end, your only option is to have the text displayed in some obscure font, or completely hide the text. If the text is visible, it has to be rendered using some font.
You could also theoretically create your own font where all the characters are just blank, but that seems highly illogical and such a waste of resources to make people download a font just so it can display meaningless emptiness.
There is no "don't render fonts" option. It's a font, it needs to be rendered, or else it's hidden visually in the DOM.
You could use Javascript to find out the font being applied to a certain block, and if it's not the font you want, just hide it. Or display a message.
Another solution is somehow specify the content to be empty. For example, I'm trying to override the +/- character that a Webix tree displays using Font Awesome:
#lhn-tree-container .webix_tree_open:before {
content: '';
}
This only works with the :before and :after pseudo-elements though.

Flex s:text htmltext missing out/cutting the full html text

I am trying to implement htmlText for a text component as the returned string is in html format (with the html tags etc). If I put a text=.... i get the full text, but with the tags (which i want converted to html). So i use htmlText=.... and it formats it fine, but cuts half the text from the variable. The text im supposed to get back has tons of html tags, and maybe its cutting it somewhere because of the tag its not able to escape... How do i fix this? Any solutions?
Not all tags are supported in the htmlText property. Here's a list of the tags that can be handled :
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#htmlText
An example though would make debugging easier :)

Resources