Qt renderText() functions bug - text crashes - c++

I use QGLWidget to draw my Graphics in QT, and I need to render some text inside my widget.
The problem is after some time, it turns from:
Fine version http://imageshack.com/a/img823/867/9ycf.png
to this:
Bugged version http://imageshack.com/a/img842/1774/57zs.png
And my code looks like this:
glPushMatrix();
qglColor(Qt::white);
renderText(-29, 11.0, -8, "Tablica wyników", QFont("Arial", 12, QFont::Light));
...
glPopMatrix();
What's wrong with that code/function?

Related

QOpenGLWidget without subclassing

I was just wondering if QOpenGLWidget that is on Qt (I am using version 5.4) can be used without subclassing.
In the Qt Creator, form editor, there is a Display widget that can be inserted into the form named "OpenGL Widget". The widget appears black when inserted and is QOpenGLWidget object. Compilation does not throw errors, such as the fact that the initializedGL() virtual function must be implemented.
The Qt help available and examples only show how to use this object by subclassing it. This kind of defeats the purpose of having an insertable object on the toolbar so I thought perhaps there could be a way.
I noticed that the widget has functions such as makeCurrent(), which I think could be useful.
Thanks
This kind of defeats the purpose of having an insertable object on the toolbar so I thought perhaps there could be a way.
You have to subclass, but you also want the .ui file to use the widget of the correct derived class, not of the base class.
There are two ways to go about it:
Insert the base class widget. Then promote it to the derived class. You can also do it manually by editing the .ui file.
Make a custom widget Designer plugin for your class. Note that this plugin (and the widget!) have to be compiled using the same version/configuration of Qt used to compile Qt Creator itself, so in most cases you have to compile Qt Creator itself first.
If you're serious about development in Qt, you'll probably have your own Designer and Creator plugins, and you'll build Creator yourself, as well as Qt.
You can do something like this by subclassing a shell widget
void OpenGLDisplayWidget::initializeGL()
{
m_renderer->initialise(context());
}
void OpenGLDisplayWidget::resizeGL(int w, int h)
{
m_renderer->resize(w, h);
}
void OpenGLDisplayWidget::paintGL()
{
m_renderer->draw();
}
So your rendering code isn't tied to the display widget.
I just made a few experiments and I found that the QOpenGLWidget can be used very easily, without subclassing.
Insert the "Open GL Widget" (QOpenGLWidget object)
Insert a button and go to "clicked()" slot.
Insert the following code
void MainWindow::on_pushButton_clicked()
{
ui->openGLWidget->makeCurrent();
//Paint a colorful triangle
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.0, 0.5, 0);
glEnd();
}
Run and click the button and you should see this
screenshot
So no need to subclass, however it may be useful to include an initialization function that sets the projection matrix and aspect ratio.
Also these GL drawing functions must be called within the mainwindow which may not be desirable for the modular fanatics.

QGLWidget rendering area not resizing proprely

I am developing an OpenGL application using Qt. Until yesterday I was subclassing QOpenGLWidget to create my custom widget but after adding It to the main application which is a QMainWindow with a few buttons and three QGraphicsView it ran really slow. I have tried using QGLWidget and the application runs the same as without the OpenGL widget.
The problem I have is that the widget that I've made subclassing QGLWidget does not resize properly (or at least the OpenGL rendering area doesn't).
I will give you an example using hellogl2 example from Qt 5.5.
Using QOpenGLWidget I get:
https://s32.postimg.org/u136s54ld/QOpen_GLWidget.png
Using QGLWidget I get:
https://s32.postimg.org/ahylis5tt/QGLWidget.png
I just changed the parent class from QOpenGLWidget to QGLWidget the rest of the code is the same. The same thing happens in my aplication.
I have tried to find a solution but I could not. May someone tell me why is this happening and how to solve it?
Thank you!
Firstly, its a good idea to stick with the up to date approach and stay away from depricated code as suggested by #ddriver.
Having said that I have written a lot of code using the QGLWidget and there is no problem with it. It does everything I need so I stick with it for now.
Here is the main issue with your approach. You took the hellogl example from qt 5.5 and simply replaced the parent from QOpenGLWidget to QGLWidget. Unfortunately they don't handle resize events similarly.
QOpenglGLWidget does change the viewport based on the width and height given by the resize event as mentioned (not so clearly with a double negative) in their documentation. Link : http://doc.qt.io/qt-5/qopenglwidget.html#resizeGL
check function resizeEvent() which in turn invokes resizeGL().
Now the QGL widget does not do this for you. All it does is makes the opengl context current which means you have to handle it old school style calling glViewport() inside the resizeGL() function. I have copied a code snippet from the hellogl example from qt4.8
void GLWidget::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#ifdef QT_OPENGL_ES_1
glOrthof(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
#else
glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
#endif
glMatrixMode(GL_MODELVIEW);
}
Here is a link for qt4.8 version of the hellogl : http://doc.qt.io/qt-4.8/qt-opengl-hellogl-example.html
Notice how the glViewport() is called to resize the framebuffer inside the resizeGL() function. This is done automatically by the newer QOpenGLWidget even before reaching this function. If you choose to stick with QGLWidget, you need to handle this yourself.
There may be other subtle differences as well which you will need to figure out.

Stroking not working on CCLabelTTF in cocos2d-x ios in c++

I am tryng to make an outline of green color in my label but its not working..
my code is
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "HoboStd", 50);
pLabel->setPosition(ccp(200,200));
pLabel->enableStroke(ccGREEN, 5.0,true);
this->addChild(pLabel);
Its not providing the outline around the label text Hello World.Any one here who can help me
I got the fix in ios7.0 on how to enable stroking on labels
The normal stroking code of a label doesn’t work in IOS 7.0 but it works successfuly below IOS 7.0
The basic code for enabling stroking is provided below. We need to add a CCLabelTTF and then call the enableStroke function
CCLabelTTF *label=CCLabelTTF::create("Hello", "Arial.fnt", 50);
label->setPosition(ccp(300,300));
label->enableStroke(ccGREEN, 1.0,true);
this->addChild(label);
This works fine with IOS below 7.0. But in IOS 7.0 there is no stroking effect on the label. In order to fix the issue just follow some steps
Step1
Find the CCImage.mm file in your project and find the following code written there
//actually draw the text in the context
//XXX: ios7 casting
[str drawInRect:CGRectMake(textOriginX, textOrigingY, textWidth, textHeight) withFont:font
lineBreakMode:NSLineBreakByWordWrapping alignment:(NSTextAlignment)align];
Step2
Now add the following code just below this line
//New Code Start
if(pInfo->hasStroke)
{
CGContextSetTextDrawingMode(context, kCGTextStroke);
CGContextSetRGBFillColor(context, pInfo->strokeColorR, pInfo->strokeColorG, pInfo->strokeColorB,
1);
CGContextSetLineWidth(context, pInfo->strokeSize);
[str drawInRect:CGRectMake(textOriginX, textOrigingY, textWidth, textHeight) withFont:font
lineBreakMode:NSLineBreakByWordWrapping alignment:(NSTextAlignment)align];
}
//New Code End
Save the CCImage.mm file and then again re-run your project. It will redraw stroke with correct color.Tested on 7.0 simulator
I tested your code and I get the outline. Try to reduce the stroke width to 1.0.
pLabel->enableStroke(ccGREEN, 1.0,true);

Qt - QPainter.DrawText doesnt draw the text

I'm creating an analog clock in Qt-Creator 5, now I want to draw the numbers to the screen, but it doesn't work?
painter.drawText(QPoint(50, 50), "12");
I absolutely don't see the point why it doesn't work. when I replace this line in my code with a .drawEllipse, it works fine. So the position/color can't be the problem, except drawText would not use the setBrush() color.
Anyone knows how to correctly draw text on the screen with the QPainter?
//previous code only draws blue ellipses with white background
QColor secondColor(240,0,0);
painter.setPen(Qt::NoPen);
painter.setBrush(secondColor);
painter.save();
QFont font=painter.font() ;
font.setPointSize(18);
painter.setFont(font);
painter.drawText(QPoint(50, 50), "12");
because it's at the end of paintEvent it can't be overdrawn
setting the pen-style over
painter.setPen(colorStyle);
solved the problem. thanks to Mat

Cocos2d CCLayerColor and CCLayerGradient alpha not working under iOS6

I just upgraded to Xcode 4.5 / iOS6, and my Cocos2d game now has an issue with transparency on CCLayerColor and CCLayerGradient. Layers created with these subclasses appear to be all-white and opaque, when in fact they should be white with transparency.
ccColor4B topStartColor = ccc4(255, 255, 255, 150);
ccColor4B topEndColor = ccc4(255, 255, 255, 100);
CGPoint topVector = ccp(0, 1);
_topGradient = [CCLayerGradient layerWithColor:topStartColor
fadingTo:topEndColor
alongVector:topVector];
I am on Cocos2d 2.0 Beta2. I did have to rework my AppDelegate to deal with the screen rotation issues caused by iOS6, so it is possible I may have inadvertently forgotten to set something up correctly - though I have combed through it pretty carefully. I should add that sprites with alpha are working fine - it seems to be CCLayerColor and CCLayerGradient only. I tested CCLayerColor in a stock Cocos2d 2.0 project, and it seems to work correctly there, so it's something in my app - but I cannot figure out what's different about my project.
This was in fact a bug in Cocos2d 2.0 Beta 2, and has been fixed in the development branch.