Unicode characters in qt app dont show up - qt

I'm trying to display different language strings in my qt app by inserting each language into a QMap<QString, QString> so it can be re-used in several places and put into different combo Boxes across the application. I do this by
creating the QMap like so in the CTOR:
m_langMap.insert(QString::fromWCharArray(L"English"), "english");
m_langMap.insert(QString::fromWCharArray(L"Dansk"), "dansk");
m_langMap.insert(QString::fromWCharArray(L"Nederlands"), "dutch");
m_langMap.insert(QString::fromWCharArray(L"Čeština"), "czeck");
m_langMap.insert(QString::fromWCharArray(L"Slovenský"), "slovak");
m_langMap.insert(QString::fromWCharArray(L"Magyar"), "hungarian");
m_langMap.insert(QString::fromWCharArray(L"Român"), "romanian");
m_langMap.insert(QString::fromWCharArray(L"Latviešu"), "latvian");
m_langMap.insert(QString::fromWCharArray(L"Lietuvių"), "lithuanian");
m_langMap.insert(QString::fromWCharArray(L"Polski"), "polish");
m_langMap.insert(QString::fromWCharArray(L"Português"), "portuguese");
m_langMap.insert(QString::fromWCharArray(L"Español"), "spanish");
m_langMap.insert(QString::fromWCharArray(L"Français"), "french");
m_langMap.insert(QString::fromWCharArray(L"Italiano"), "italian");
m_langMap.insert(QString::fromWCharArray(L"Svenska"), "swedish");
m_langMap.insert(QString::fromWCharArray(L"Русский"), "russian");
m_langMap.insert(QString::fromWCharArray(L"Українська"), "ukranian");
m_langMap.insert(QString::fromWCharArray(L"Русский"), "russian");
m_langMap.insert(QString::fromWCharArray(L"中文"), "chinese");
m_langMap.insert(QString::fromWCharArray(L"日本語"), "japanese");
I then insert them into the combo box:
QMap<QString, QString>::const_iterator it = m_langMap.begin();
while (it != m_langMap.end())
{
ui->comboBox->addItem(it.key());
++it;
}
When the app runs, I see the following:
However, if I create a separate .ui file and insert the map the same way, I see the following (even if I include this separate Dialog class into the same application), so clearly there is no font issue as far as the App not knowing how to render the different character sets....yet I cant figure out why the first one won't render the character sets?
Can someone tell me why the first doesn't work but the second does? I checked the Designer and its Locale is set to 'C, Default' in both ui files I've shown below. I can't seem to figure out what else is causing the difference for the first not to work, and the second does work within the same application.
Thanks for any help!
The other test Dialog:

Your code is correct, but the problem is that your source file cannot contain Unicode characters - apparently it is using different coding.
Save file as UTF-8 and everything should work!

In the first screenshot the font used by the combobox is much larger than in the second screenshot. My guess is that you have changed the font either in the GUI designer or in the code and the second (working) screenshot is using the default font. It might be that when you have changed the font size, you have also changed the font to something that doesn't contain all the required Unicode characters. Try changing the font used by the combobox to something else.

Related

Explanation of "hardcoded" font on a piece of text, and how to remove it via CSS etc

I'm unable to understand how a piece of text has some kind of hardcoded font in it, that cannot be removed even after pasting into an RTE, application of CSS etc. See 2 examples below copied from various places:
Sample 1 - this font is apparently Cambria Math, and will not / cannot be overridden by CSS when displaying or removed when pasting into an RTE. Eg take this text and dump into CKEditor, QuillJS and any other, it will remain the same. In fact the same is happening right here in this editor - the font is not getting removed, and I'm unable to explain why.
𝐈𝐧𝐝𝐮𝐬𝐭𝐫𝐲 𝐚𝐧𝐚𝐥𝐲𝐬𝐢𝐬 𝐢𝐬 𝐚 𝐜𝐫𝐮𝐜𝐢𝐚𝐥 𝐬𝐭𝐞𝐩 𝐢𝐧 𝐭𝐡𝐞 𝐜𝐮𝐫𝐫𝐞𝐧𝐭 𝐜𝐨𝐦𝐩𝐞𝐭𝐢𝐭𝐢𝐯𝐞 𝐦𝐚𝐫𝐤𝐞𝐭 𝐬𝐩𝐚𝐜𝐞 𝐭𝐡𝐚𝐭 𝐡𝐞𝐥𝐩𝐬 𝐢𝐝𝐞𝐧𝐭𝐢𝐟𝐲 𝐭𝐡𝐞 𝐫𝐢𝐠𝐡𝐭 𝐭𝐚𝐫𝐠𝐞𝐭 𝐜𝐮𝐬𝐭𝐨𝐦𝐞𝐫𝐬 𝐚𝐧𝐝 𝐚𝐜𝐜𝐨𝐫𝐝𝐢𝐧𝐠𝐥𝐲 𝐩𝐫𝐨𝐯𝐢𝐝𝐞 𝐭𝐚𝐢𝐥𝐨𝐫𝐞𝐝 𝐬𝐨𝐥𝐮𝐭𝐢𝐨𝐧𝐬 𝐟𝐨𝐫 𝐭𝐡𝐞𝐢𝐫 𝐛𝐮𝐬𝐢𝐧𝐞𝐬𝐬 𝐧𝐞𝐞𝐝𝐬. 𝐄𝐯𝐞𝐫𝐲 𝐚𝐬𝐩𝐞𝐜𝐭 𝐚𝐧𝐝 𝐮𝐧𝐢𝐪𝐮𝐞 𝐜𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞𝐬 𝐩𝐮𝐭 𝐮𝐩 𝐛𝐲 𝐭𝐡𝐞 𝐩𝐚𝐫𝐭𝐢𝐜𝐮𝐥𝐚𝐫 𝐢𝐧𝐝𝐮𝐬𝐭𝐫𝐲 𝐚𝐫𝐞 𝐜𝐚𝐫𝐞𝐟𝐮𝐥𝐥𝐲 𝐭𝐚𝐤𝐞𝐧 𝐢𝐧𝐭𝐨 𝐜𝐨𝐧𝐬𝐢𝐝𝐞𝐫𝐚𝐭𝐢𝐨𝐧 𝐰𝐡𝐢𝐥𝐞 𝐟𝐨𝐫𝐦𝐮𝐥𝐚𝐭𝐢𝐧𝐠 𝐭𝐡𝐞𝐬𝐞 𝐬𝐨𝐥𝐮𝐭𝐢𝐨𝐧𝐬. 𝐈𝐭 𝐚𝐥𝐬𝐨 𝐭𝐚𝐤𝐞𝐬 𝐢𝐧𝐭𝐨 𝐜𝐨𝐧𝐬𝐢𝐝𝐞𝐫𝐚𝐭𝐢𝐨𝐧 𝐯𝐚𝐫𝐢𝐨𝐮𝐬 𝐠𝐨𝐯𝐞𝐫𝐧𝐦𝐞𝐧𝐭 𝐫𝐞𝐟𝐨𝐫𝐦𝐬, 𝐜𝐨𝐦𝐩𝐞𝐭𝐢𝐭𝐢𝐯𝐞 𝐞𝐧𝐯𝐢𝐫𝐨𝐧𝐦𝐞𝐧𝐭, 𝐜𝐮𝐬𝐭𝐨𝐦𝐞𝐫 𝐛𝐞𝐡𝐚𝐯𝐢𝐨𝐫, 𝐞𝐱𝐢𝐬𝐭𝐢𝐧𝐠 𝐚𝐧𝐝 𝐮𝐩𝐜𝐨𝐦𝐢𝐧𝐠 𝐛𝐮𝐬𝐢𝐧𝐞𝐬𝐬 𝐦𝐨𝐝𝐞𝐥𝐬, 𝐚𝐧𝐝 𝐞𝐯𝐞𝐫-𝐞𝐯𝐨𝐥𝐯𝐢𝐧𝐠 𝐭𝐞𝐜𝐡𝐧𝐨𝐥𝐨𝐠𝐢𝐜𝐚𝐥 𝐝𝐞𝐯𝐞𝐥𝐨𝐩𝐦𝐞𝐧𝐭𝐬. 𝐈𝐧𝐝𝐮𝐬𝐭𝐫𝐲 𝐚𝐧𝐚𝐥𝐲𝐬𝐢𝐬 𝐡𝐞𝐥𝐩𝐬 𝐚𝐧 𝐨𝐫𝐠𝐚𝐧𝐢𝐳𝐚𝐭𝐢𝐨𝐧 𝐟𝐨𝐫𝐦𝐮𝐥𝐚𝐭𝐞 𝐬𝐭𝐫𝐚𝐭𝐞𝐠𝐢𝐞𝐬 𝐚𝐧𝐝 𝐩𝐨𝐥𝐢𝐜𝐢𝐞𝐬 𝐨𝐟 𝐚 𝐛𝐮𝐬𝐢𝐧𝐞𝐬𝐬.
Sample 2 - this is how it should be. When rendered the target CSS font is used.
Allowing PUT as create operations is problematic, as it necessarily exposes information about the existence or non-existence of objects. It's also not obvious that transparently allowing re-creating of previously deleted instances is necessarily a better default behavior than simply returning 404 responses.
More info if needed:
I saw this occur here https://www.althealth.me/topics/792/article/2888/rigid-knee-braces-market-is-expected-to-. This is an instance on the MainCross platform which is created by me.

Reading multiple lines from QTextedit

I'm trying to write my own code-editor, I figure its a good way to learn pyQt.
I am using a qtextedit, in which i can write code(it's not real code, more pseudo code).
Each line represents ending in a semi-colon represents some command
e.g.
PSEUDO->FWD->90;
PSEUDO->STOP;
PSEUDO->RIGHT 90;
PSEUDO->FWD 10;
These are relatively easy to read, as the user presses the [ENTER] the current line is read, parsed and checked for errors so the following
PSEUDO->RIGHT -pi/2
would generate an error because the line doesn't end in a semi-colon and the value following RIGHT needs to be a number.(my editor, my rules).All this I have more or less got working.
I would like to know how to do multiple lines though. I am familiar with editors such as Eclipse,sublime or visual studio which handle muliple lines very well, in my case
PSEUDO->DO:
FWD->90
RIGHT->45
FWD->10
LEFT->55
FWD->50
STOP;
Should all be read in and treated as one statement, starting at the keyword PSEUDO and ending at the semi-colon.
However the following should be read as 3 separate statements.
PSEUDO->DO:
FWD->90
RIGHT->45
FWD->10
LEFT->55
FWD->50
STOP;
PSEUDO->DO:
FWD->90
RIGHT->45
STOP;
PSEUDO->BACK 10;
My question how can I go about reading muliple lines as described above from QTextEditor as discreet statements.
Should I do the parse/check whenever I press the [ENTER] key for a new line?
I'm using python2.7,pyQT, and QTextEdit.
Basically what you are trying to do, can be done with all the text of the document and some regular expressions. I would be careful with this course, because it may slow things down.
So to stay lean and to use the right classes of Qt, I would use anything related to the Rich Text Processing, QTextDocument, and QTextCursor.
I have used QSyntaxHighlighter quite a bit, and QRegExp. There is now QRegularExpression, too. Here are the classes and general documentation I'd look at to get started with Rich Text processing.
http://doc.qt.io/qt-5/qtextblock.html#details
http://doc.qt.io/qt-5/richtext-structure.html
http://doc.qt.io/qt-5/richtext.html
http://doc.qt.io/qt-5/richtext-cursor.html
http://doc.qt.io/qt-5/richtext-common-tasks.html#finding-text
Hope that helps.

Qt-Application is killing characters accidentally (drawText produces bug)

I've got a really simple application for adding watermarks to pictures. So you can drop your pictures in a QListWidget which shows you a thumbnail and the path, adjust some things like the text, the transparency, the output format and so on.. and after pressing start it saves the copyrighted picture in a destination of your choice. This works with a QPainter which paints the logo and text on the picture.
Everything is able to work fine. But here's the misterious bug:
The application kills random letters. It's really strange, because I can't reproduce it. With every execution and combination of options it's different. For example:
Sometimes I can't write some letters in the QLineEdit of my interface (like E, 4 and 0 doesnt exist, or he changes the letters so some special signs).
The text of the items in the QListWidget aren't completly displayed, sometimes completley missing. But I can extract the text normally and use the path.
While execution I have a QTextBrowser as a log to display some interesting things like the font size. Although, the font is shown normaly on the resulting picture, it says " 4" or "6" instead of much higher and correct sizes. Betimes strange white blocks appear between some letters
When drawing text on the picture with a QPainter, there also letters missing. Sometimes, all the letters are printed over each other. It seems like this bug appears more often when using small pixelsizes (like 12):
//Text//
int fontSize = (watermarkHeight-(4*frame));
int fontX = 2*frame;
int fontY = (result.height()-(watermarkHeight-2*frame));
int fontWidth = watermarkWidth;
QRect place(fontX,fontY,fontWidth,fontSize);
QFont font("Helvetica Neue", QFont::Light);
font.setPixelSize(fontSize);
emit log(QString::number(fontSize));
pixPaint.setFont(font);
pixPaint.setPen(QColor(255,255,255,textOpacity));
pixPaint.drawText(place,text);
Not all of these bugs appear at once! Sometimes I haven't got any bugs...
Perhaps someone had a similar bug before. Unfortunately I didn't found something like this in the internet. I didn't post a lot of code snippets because I think (and hope) that this is a gerneral problem. If you need something specific to help me, please let me know =)
I've added a example picture:
In the lineEdit I simply wrote ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 (look what he has done with the 7 and 9)
This small square in the lower corner of the picture should be the "ABC..." thing
The "62" looks very strange in the textBrowser
I'm using Qt 5.0.1 on a Windows 7 64Bit computer.
EDIT: Everytime after adding the first picture to the list, he's finding these warnings:
QFontEngine::loadEngine: GetTextMetrics failed ()
QWindowsFontEngine: GetTextMetrics failed ()
But when I change the height (and with it the pointSize of the font) its not emitted anymore, even with the start-parameters.
EDIT 2: Thank you for your help! I corrected my code so that he only uses correct fonts and correct sizes, but it still doesn't work. When I remove the QPainter::drawText() function it works fine (without the text). But as soon as I am adding text everything is bugged. I have something like this now:
//Text//
QList<int> smoothSizes = fontDatabase->smoothSizes("Verdana","Standard");
int fontSize = (watermarkHeight-(4*frame))*0.75;
emit log("Requested: "+QString::number(fontSize));
if(!smoothSizes.contains(fontSize)){
for(int i = 0; i<smoothSizes.length(); i++){
if(smoothSizes.at(i) > fontSize && i>0){
fontSize = smoothSizes.at(i-1);
break;
}
}
}
int fontX = 2*frame;
int fontY = (result.height()-(watermarkHeight/2)+frame);
QFont font = fontDatabase->font("Verdana","Standard",fontSize);
QFontInfo info(font);
emit log("Corrected: "+QString::number(fontSize));
emit log("Okay?: "+QString::number(info.exactMatch()));
pixPaint.setFont(font);
const QFontMetrics fontMetrics = pixPaint.fontMetrics();
if(info.exactMatch()){
pixPaint.setPen(QColor(255,255,255,textOpacity));
pixPaint.drawText(fontX,fontY+(fontMetrics.height()-fontMetrics.ascent()),text);
}
It almost sounds like you are corrupting random memory in your process, or you have a badly broken Windows install. Possibly your font request is matched by a very poorly chosen system font.
Whatever is set on a QFont is merely a request. To obtain the parameters of the actual font that was selected, you must create a QFontInfo, and get your information from there.
Imagine that you request a QFont that doesn't exist on a system, or that can't be scaled to a particular size. At some point the font object would need to morph to reflect what really happened - this would be very confusing. Thus, the QFontInfo provides the information about the font that was actually used. Think of QFontInfo as a response, and QFont as a request.
I finally found a solution: I simply updated Qt from 5.0.1 to 5.2.1, now it works. Perhaps someone has a similar bug and this post helps him. Thank you for your help!

QPlainTextEdit truncate history linewise

I have a GUI application whose main part is a QPlainTextEdit. It is used to display a log of the application, and as such the associated text grows line by line ad infinitum.
As the application is intended to run very long, I need to limit the memory that will be allocated for this log. Therefore I want to have some maxNumLines or maxNumCharacters parameter that will make sure the history will be truncated when reached, i.e. the head lines will be removed as new lines are appended (a.k.a. log rotation).
To achieve this I found the functions
// get the associated text
QString toPlainText () const
// set the associated text
void setPlainText ( const QString & text )
Therefore something like this untested code would probably do the trick:
QString &tmp = pte.toPlainText();
while (tmp.size() > maxNumCharacters) {
// remove lines from the head of the string until the desired size is reached
// removes nothing if "\n" could not be found
tmp.remove(0, tmp.indexOf("\n")+1);
}
pte.setPlainText( tmp );
Is this the way to go to remove the first line(s) from the QPlainTextEdit? Are there probably other Qt Text GUI elements that would better fit to this task (set a maximum number of lines and truncate at the head of the list), e.g. somehow display a QStringList in which I could store the lines (s.t. I could easily erase(0))?
Or does the QPlainTextEdit eventually implement such upper bound for the size of the associated QString after all?
Apparantly the property maximumBlockCount is exactly what I need:
If you want to limit the total number of paragraphs in a QPlainTextEdit, as it is for example useful in a log viewer, then you can use the maximumBlockCount property. The combination of setMaximumBlockCount() and appendPlainText() turns QPlainTextEdit into an efficient viewer for log text.
For reference:
http://doc.qt.io/qt-5/qplaintextedit.html#maximumBlockCount-prop
I had exactly the same problem a months back, and I ended up using a QListView. Although using the model/view/delegate architecture is a bit more fiddly, it scales much better in the long run. For example once the basic architecture is in place, adding a filter that displays only error or warning entries becomes trivial, or creating a delegate so that the background of error entries are painted red is also straightforward.

Programmatically adding new line in mx:TextArea

I have a flex 4.5 application. I want to add new line in an mx:TextArea when certain event occurs. I have been searching for the proper way to add a OS independent line ending. I found out that the File class has lineEnding property. However the documentation states that this class is not exposed when running inside a browser (which is my case).
I have searched, but I couldn't find any other class, which can provide this information. Actually I am not sure if the TextArea line ending is OS dependent or not.
So actually I have two questions: Are TextArea line endings OS dependent or not? And if so, how can I get the proper line ending in flex?
You can use String.fromCharCode(13). This will return a line ending.
This is the equivalent of PHP's chr() method.
Example:
var address_str:String = "dog" + String.fromCharCode(64) + "house.net";
trace(address_str); // output: dog#house.net
From my experience, "\r" works in both Windows and Mac.
Quite simply, you just need to add the newline character to the text of the textArea.
myTextArea.text+="\n"; //This should work, if not try the other two
myTextArea.text+="\r";
myTextArea.text+="\r\n";

Resources