QTextDocument stylesheet for non-html content - qt

Qt 5.14 has introduced setting markdown directly in QTextDocument. My question is how I can style the rendered richtext e.g. the heading color?
The QTextDocument::seDefaultStyleSheet only works for html content. For non-html rich text, is there a way to modify the default QTextDocument formatting directly or do I need iterate over all blocks and set the formatting for each block manually?

I ran into the same issue, my workaround is:
document->setDefaultStyleSheet("h1 { color: red; }");
document->setMarkdown("# Hello World");
document->setHtml(document->toHtml()); // get markdown as HTML and set it as HTML, now the CSS is applied
It's not ideal, but it works :)

Related

Formatting text editor with Elementor

I'm using Elementor with an OceanWP theme. I'm trying to customize a text editor that has many lines of text. I have 2 questions about how to tailor the content of the text editor:
Is it possible to prevent Elementor from adding space between paragraphs (in a similar way to how MS Word handles this matter)?
When I click on bold font, the text editor is actually using the expression <strong> rather than <bold> as I was expecting. The thing is that I would like to increase the contrast with the regular font but <bolder> and <stronger> do not work.
So I'd add some custom CSS
You could add some custom CSS to override the margin on the paragraph elements within Elementor text block. Here's some psuedocode:
.elementor-text-editor-block p {
margin-bottom:0!important;
}
Similarly, add something like
strong {
font-weight: 600;
}

Tiny cme html editor with angular js

I am using TinyCME html editor with the angular directive
and I render the output of the editor -which is data-bidden to property "description" in the scope- into a div using ng-html-bind.
<div ng-bind-html="description" ></div>
everything is working fine but I didn't get in the div what I see in the editor
especially when it comes to styling like the background color and text color
here is what I get in the editor
and here is what I get in the div
it sounds like all the styles applied in the editor will eventually be overwritten by the styles in the div context
I don't have any experience in CSS so please excuse my lack of knowledge
What I really want to do is to render the editor output in a div in a way exactly the way it looks in the editor any help?
I have solved the problem the issue comes from that the ng-bind-html strips out all the styling info comes from the editor that's why there is no styling info
to solve the problem we should use angularjs service $sec which tells the ng-bind-html not to strip out anything from the html string
so to use it in the angular expression we should make it as a filter
app.filter('trustAsHtml', ["$sce", function ($sce) { return $sce.trustAsHtml; } ] );
then you can use this filter in the binding expression like the following:
<div ng-bind-html="currentModel.description | trustAsHtml" ></div>

Printing a page with inline styles

I have a page that I set the styles of it dynamically with all sort of settings
For example here is an element of a table that I am trying to print:
<td *ngFor="let attribute of attributes"
[class.table-vertical]="styles.template['displayVerticalLines']"
[style.border-left-width]="styles.template['displayVerticalLines']?styles.template['verticalLineSize'] + 'px':null"
[style.border-right-width]="styles.template['displayVerticalLines']?styles.template['verticalLineSize'] + 'px':null"
[style.border-left-color]="styles.template['displayVerticalLines']?styles.template['verticalLineColor']:null"
[style.border-right-color]="styles.template['displayVerticalLines']?styles.template['verticalLineColor']:null">
This is the function I print with:
public print(): void {
window.print();
}
I see the styles affect the element all fine but when I try to print all the inline styles are ignored and only the ones that are in css files are visible.
I realize it is because the media type of the inline css is not print, but I don't see a way to set it as such.
I also tried to add a dynamic <style> element but the compiler just ignores it and skims over it.
Here is a simple plunker of the phenomenon:
https://plnkr.co/edit/vm1AgWP33LL2Gslylvkp?p=preview
How can I overcome this problem?

Mix Github markdown language with CSS

How can I add CSS to github's markdown language?
I've been able to do so by using the style property inside html tags, like:
<p style="text-align: center;">This is some random text</p>
But if I move the css to the beginning, like:
<style>
p {
text-align: center;
}
</style>
<p>This is some random text</p>
Github doesn't recognize it, and just writes to the screen the css code.
I'm using Atom, and the package Markdown Preview actually recognizes this correctly, even though on the remote repository it shows wrong. And so does the Google Chrome extension Markdown Preview Plus.
Is there a way to do this? Writing css within html tags just feels plain wrong.
After GitHub converts Markdown to HTML,
The HTML is sanitized, aggressively removing things that could harm you and your kin—such as script tags, inline-styles, and class or id attributes. See the sanitization filter for the full whitelist.
style tags are not included in GitHub's whitelist, so they are removed. I'm actually surprised that inline style attributes work; they don't seem to be included in the whitelist, either, and are explicitly mentioned in the previous paragraph.
In any case, GitHub does not permit arbitrary HTML to be included in Markdown.
Here is how you can accomplish what you're looking for. As the other answer states, Github doesn't support this syntax, but if you pop this Markdown into another preview tool you'll see that the bullets are removed from this list.
|Signal|Description|
|---|---|
|DOP|Horizontal Dilution of precision|
|FIX|GPS Fix Quality indicator: <ul style="list-style-type:none;"><li>0 - fix not available</li><li>1 - GPS fix</li></ul>|
Signal
Description
DOP
Horizontal Dilution of precision
FIX
GPS Fix Quality indicator: 0 - fix not available1 - GPS fix
You can trivially override what CSS Github uses by supplying it with your own style.css file, nested as ./assets/css/style.css (which is stylesheet URL that gets pointed to in the HTML source code that Github build off of your markdown).
Note that if you want to just "add" any CSS, you'll want to copy Github's CSS first, so you can create a file with the same content after which you place your own rules. You can find this on any view-source:https://username.github.io/repo-name/assets/css/style.css with the obvious replacements for username and repo-name.
E.g.
/* CSS as copied from github's own stylesheet here, which is all one line anyway */
...
/* And then your own CSS */
/* remove the repo name as some kind of weird super-title */
h1:first-child { display: none }
/* and better emphasise the _real_ title */
h1:nth-child(2) { font-size: 3em; }
/* let's also give images a subtle border */
img { border: 1px solid #DDD; }

Set font color in a tr() function

I am thinking of changing the color of some text in a tr() function.I am thinking of
tableModel->setHeaderData(2, Qt::Horizontal, tr("<font color=red><i>Org. Name</i></font>"));
Will it be possible to change the font color?.
I don't think that providing an HTML string will change the appearence of the text and even if it works it is not the best approach to mix the content with the styling. You should use stylesheets and change the color of the header view of your QTableView.
QString styleSheet = "QHeaderView::section {"
"color: red;"
"background-color: black; }";
tableView->horizontalHeader()->setStyleSheet(styleSheet);
Yes, the whole HTML string will be offered for translation, including the markup. If you want translators to mess with the HTML, your approach just works. Usually one wouldn't want translators to mess with (and possibly break) the HTML markup, then one would use something like this:
QString::fromLatin1("<font color=red><i>%1</i></font>").arg(tr("Org. Name"))
I.e. mark only the actual text for translation, instad of the whole html string.

Resources