UI widget in openGL - qt

I'm kinda new in Qt and openGL, I have a shape in my code in openGL and I need to place it on the UI widget. But the result of my code is like the picture below, my UI widget is on my shape. Can anyone help me with it?
Here is my code:
void Widget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix(); BackGround();
gambi();
if(is_dead==1) {
DrawSquare(.8,.8,.8,1,1,1,1,.8);
}
glPopMatrix();
}

Related

How to enable depth test with QOpenGLWidget? It renders black screen

Currently I am writing a small application with Qt and OpenGl and I choosed QOpenGLWidget for rendering graphics.
That's how I declared my widget:
class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions{
// Methods, slots, e.t.c
}
And that's the constructor:
GLWidget::GLWidget(QWidget *parent) : QOpenGLWidget(parent)
{
/* Tried this, it didn't help.
QSurfaceFormat format;
format.setDepthBufferSize(24);
this->setFormat(format);
*/
}
In my init function I set GL_CULL_FACE and GL_DEPTH_TEST:
void GLWidget::initializeGL()
{
/* Some initialization stuff regarding the scene */
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
And I really don't know why the widget renders the black screen.
Here are some pictures.
First one: with disabled GL_CULL_FACE and disabled GL_DEPTH_TEST.
Second one: with enabled GL_CULL_FACE and disabled GL_DEPTH_TEST. Maybe it's not a good picture, but I can assure you, you can see some surfaces through the other.
Third one: with enabled GL_CULL_FACE and enabled GL_DEPTH_TEST. Actually, it doesn't matter if GL_CULL_FACE is enabled. Anyway it renders the black screen.
And here's the image without any shading just to show you that the model is fine.
I tried to set the format manually, but it didn't help. Still the black screen:
QSurfaceFormat format;
format.setDepthBufferSize(24);
this->setFormat(format);
Oh and yes, I set glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); at the beginning of my paintGL() function.

GL render does not show because of the QPainter

When I create a QPainter object in paintGL() function the rendered scene does not shown. Here is my code:
void D3Display::paintGL(){
makeCurrent();
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
// some adjustment stuff here...
glPushMatrix();
// some rendering stuff here...
glPopMatrix();
QPainter painter(this);
// some overpainting stuff here...
}
Now the problem is: when I create the QPainter object, screen suddenly turns into black and does not show anything (including render and overpaint stuff). As you can see, I want to overpaint something on a QGLWidget , I am trying some stuff but I couldn't work it out. This function has been written similarly with the QT Overlay example project.

Show QWidget with QSlider on top of tray icon

I'm trying to implement some kind of volume slider in Qt.
I have added the QWidget with a QSlider on it. It works
fine for me... But! QWidget shows on the screen center.
But I need it on top of the tray icon.
Does anybody knows how to do that?
Code:
VolumeSlider::VolumSlider(QWidget *parent) : QWidget(parent)
{
setWindowFlags(Qt::Popup);
resize(20, 150);
slider = new QSlider(Qt::Vertical, this);
slider->setRange(0, 100);
slider->setSingleStep(5);
slider->setPageStep(10);
slider->setValue(currentVolume);
slider->resize(20, 150);
}
I'm showing the QWidget witha QSlider on middle click:
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
And a slot implementation is:
void VolumeSlider::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::MiddleClick) {
show();
}
}
Thank you for your attention!
Best regards!
You may consider the QSystemTrayIcon Class:
http://doc.qt.io/qt-5/qsystemtrayicon.html
And this example:
http://doc.qt.io/qt-4.8/qt-desktop-systray-example.html

Copy Bits to QtImage in QGLWidget

Maybe someone can help me with the following problem:
I want to draw the content of a QImage in a QGLWidget, but the widget is painted in black.
class QGLCanvas {
public:
QGLCanvas(QWidget* parent) : QGLWidget(parent) {
}
void setImage(const QImage* image) {
img = image;
}
void paintEvent(QPaintEvent*) {
// From Painter Documentation Qt
QPainter p(this);
p.setRenderHint(QPainter::SmoothPixmapTransform, 1);
p.drawImage(this->rect(), *img);
p.end();
}
public slots:
void rgb_data(const void *data) {
memcpy((void *)img->bits(), data, img->byteCount()); // data will be copied (sizes are known)
// img.save("text.png"); // saves right image
this->update(); // calls repaint, but does not draw the image.
}
private:
QImage *img;
}
The Bug:
When the slot is called, the memory is copied to the image. If the image is saved, the content is correct. But the repaint method just draws black content to the widget.
The Fix:
If the memcpy line is implemented outside the slot, the image content is drawn to the widget. This fix increased code complexity a lot. Thus, i've got the following question:
The Question:
Why does the memcpy not work within the slot? Is this a general problem with Qt?
There's nothing special about a slot which would stop your code from working.
What's probably the issue is that when you call update(), a repaint is scheduled but happens asynchronously. From the code you've provided the most likely cause is img being modified between the calls to rbg_data and paintEvent
You want to be sure about the format of the QImage. When you call bits and are expecting it to be RGB, you need to check the format.
if( img->format() != QImage::Format_RGB888 )
{
// convert the image format to RGB888
*img = img->convertToFormat(QImage::Format_RGB888);
}
This way, Qt will know the image format when trying to paint it. If you fill it with RGB data but the QImage is "formatted" as ARGB, you will get some painting errors.

While save out contents of a QGLWidget as an image, text in the widget are not saved

In My QGLWidet, I draw some text using method renderText(). Then, I want to save the contents of the widget as an image. But, it turns out that text drawn by renderText() are not saved out.
void MyGLWidget::paintGL()
{
qglClearColor(Qt::white);
glViewport(0,0, width(), height());
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2f(0,0);
glVertex2f(width(), height());
glEnd();
renderText(50, 50, "Hello");
glColor3f(0.0,1.0,0.0);
renderText(50, 150, "World");
}
Here is code to save image:
void MyGLWidget::saveImage()
{
QGLPixelBuffer pbuffer(width(), height());
pbuffer.makeCurrent();
paintGL();
QImage image = pbuffer.toImage();
image.save("test_image.tif","tif");
}
Any idea?
After debugging into Qt 4.8.0's source code, I found a few reasons this will not work.
renderText uses GLWidget's width & height rather than the QGLPixelBuffer (not an issue for your case as your screenshot & widget are the same size)
It constructs a QPainter on the QGLWidget to render the text
Unfortunately, it looks like renderText is simply incompatible w/ QGLPixelBuffers

Resources