QGLWidget cannot be converted to QPixmap

296 views Asked by At

I am basically trying to implement drag and drop. I have a derived widget from QGLWidget. This widegt draws a triangle. I try to render a pixmap from it and use this pixmap on a Qlabel to show up in the drag section. However I am not able to do this as nothing shows up in the drag section. However if I use a QPainter to draw another QPixmap (something like a rounded rectangle) and use the pixmap on a QLabel,it shows up in the drag section. This means that instead of a triangle and a rounded rectangle only a rounded rectangle shows up. There are no errors in the program and I can assure you that the QGlWidget (derived) is being correctly created.

Here is a sample code for derived widget but this exactly depicts what is my problem: glwidget.h

#ifndef _GLWIDGET_H 
#define _GLWIDGET_H

#include <QtOpenGL/QGLWidget>

class GLWidget : public QGLWidget {

Q_OBJECT // must include this if you use Qt signals/slots

public:
GLWidget(QWidget *parent = NULL);

protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
};

#endif  /* _GLWIDGET_H */

Next is the glwidget.cpp:

#include <QtGui/QMouseEvent>
#include "glwidget.h"
#include <GL/glu.h>

GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) {
setMouseTracking(true);
}

void GLWidget::initializeGL() {
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
}

void GLWidget::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h); // set origin to bottom left corner
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_POLYGON);
glVertex2f(0,0);
glVertex2f(100,500);
glVertex2f(500,100);
glEnd();
}

void GLWidget::mousePressEvent(QMouseEvent *event) {

}
void GLWidget::mouseMoveEvent(QMouseEvent *event) {
printf("%d, %d\n", event->x(), event->y());
}

void GLWidget::keyPressEvent(QKeyEvent* event) {
switch(event->key()) {
case Qt::Key_Escape:
    close();
    break;
default:
    event->ignore();
    break;
}
}

Next I create a QGlWidget(derived object) and a QLabel object in dragwidget.cpp. For that the code I have taken is from Qt examples of drag and drop. The link to the example is : http://doc.qt.io/qt-5/qtwidgets-draganddrop-draggableicons-example.html In this example I just change the constructor function of dragwidget : DragWidget::DragWidget(QWidget *parent): QFrame(parent) by adding the QGlWidget.

DragWidget::DragWidget(QWidget *parent)
: QFrame(parent)
{
setMinimumSize(50, 100);
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
setAcceptDrops(false);

GLWidget *glWidget = new GLWidget;

QPixmap pixies = glWidget->renderPixmap(glWidget->width(),glWidget->height());
QLabel sample;
sample.setPixmap(pixies);
qInfo("here");
QPoint pStick3;
pStick3.setX(17);
pStick3.setY(10);
sample.move(pStick3);
sample.show();




QPixmap *StickVert = new QPixmap(120,120);
StickVert->fill(Qt::transparent);
QPainter painter2(StickVert);
painter2.setPen(QPen(QBrush(QColor(Qt::blue)),2,Qt::DashLine));
painter2.setBrush(QBrush(QColor(Qt::yellow)));

painter2.translate(50, 50);
painter2.rotate(90.0);
// painter2.scale(0.6, 0.9);
painter2.translate(-50, -50);

QPoint pStick1;
pStick1.setX(10);
pStick1.setY(10);

QRect rect1(20, 10, 90, 20);

painter2.drawRoundedRect(rect1, 10, 10);
QLabel *labelStickvert = new QLabel("", this);

labelStickLeft->setPixmap(*StickLeft);
labelStickLeft->move(pStick3);

I get output as :

actual output

Please help!!

0

There are 0 answers