Highlight a text Qt [duplicate] - qt

I'm a student programmer currently developing an application for work using Qt4. I am building an equation editor and I'm having issues attempting to highlight a string within my QTextEdit field. I have a function that parses through the QTextEdit string and returns an a start and end integer of where an error is located. My original strategy was to use HTML tags at these two points to highlight the error. Unfortunately there appears to be an issue with html tagging and the equation syntax.
What I think I need is a strategy that relies on Qt's library to set a background color between these two indices. I began looking a QSyntaxHighlighter; however I think that this is more for highlighting using a predefined set of laws and not for just grabbing up anything between a & b and setting the background color. If I can use syntax highlighter please provide me with and example or reference as I have already read through the documentation and didn't find anything.
Thanks for any help in advance!
P.S. Just to emphasize on the html compatibility issues; html becomes problematic due to multiple < and > signs used.

You can use QTextCursor and QTextCharFormat for it:
QTextEdit *edit = new QTextEdit;
...
int begin = ...
int end = ...
...
QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);
QTextCursor cursor(edit->document());
cursor.setPosition(begin, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.setCharFormat(fmt);

Related

Why is QLineWidth not taken into account?

In Qt3D certain properties of rendered objects are not just simply set on the renderer, but they are globally (per view) or locally (on the material of a rendered object) added to the renderPasses - or so is my comprehension at least. (I'm using PySide2 - but the code is almost the same in C++)
For example when adding a geometry-renderer and using its primitive type point (Qt3DRender.QGeometryRenderer.Point) instead of rendering triangle-faces it displays the points of the geometry.
Here is an example figure with the default type.
The same only showing the points (renderer.setPrimitiveType(Qt3DRender.QGeometryRenderer.Points))
Hard to guess, but here the point-size has been already been changed - using the following code:
material = Qt3DExtras.QPhongMaterial(e)
for t in material.effect().techniques():
for rp in t.renderPasses():
pointSize = Qt3DRender.QPointSize(rp)
pointSize.setSizeMode(Qt3DRender.QPointSize.SizeMode.Fixed)
pointSize.setValue(5.0)
rp.addRenderState(pointSize)
According to the documentation the same mechanism can be used to change the line-width when rendering the object with Lines (LineStrip) as primitive type. Adding
lineWidth = Qt3DRender.QLineWidth(rp)
lineWidth.setValue(5.)
lineWidth.setSmooth(True)
rp.addRenderState(lineWidth)
does not change the line-width.
Why? Where do I need to add QLineWidth? Is it the material I chose which ignores the QLineWidth-state?
I'm fighting with similar problems at the moment. I tried to reproduce the behaviour with Qt3D line width test. When setting format version to 4.6 with CoreProfile, the maximum of linewidth seems to be 1 (or equivalently width=3 displayed by the line test).
It might be possible that this is the maximum supported range.
See:
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glLineWidth.xhtml
opengl glLineWidth() doesn't change size of lines
Note: I deliberately chose version 4.6 as that is the supported openGL version on my environment.
I ran into the same issue. It appears the problem is caused by Qt3DExtras::Qt3DWindow, which constructs a QSurfaceFormat with an OpenGL core profile. The glLineWidth function is not supported in the core profile.
Unfortunately there is no way to pass a QSurfaceFormat to Qt3DWindow. Setting a new format after the window is created also does not work.
The only way around this is to write your own window class with a QSurfaceFormat in compatibility mode. For example:
setSurfaceType(QSurface::OpenGLSurface);
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setVersion(4, 3);
format.setProfile(QSurfaceFormat::CompatibilityProfile);
format.setDepthBufferSize(24);
format.setSamples(4);
format.setStencilBufferSize(8);
setFormat(format);
QSurfaceFormat::setDefaultFormat(format);
Fortunately Qt3DExtras::Qt3DWindow does not actually contain a lot of functionality and you can easily write a similar class with the QSurfaceFormat changes mentioned above.
You can find the original source here for reference:
https://code.woboq.org/qt5/qt3d/src/extras/defaults/qt3dwindow.cpp.html

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!

Unicode characters in qt app dont show up

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.

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.

Resources