schmimona
schmimona

Reputation: 869

Adding a child widget to another widget in Qt

Although there are similar questions to mine posted on stackoverflow, none of their solutions actually respond to my problem. I have 2 independent widgets that I would like to combine (insert one widget into the other as a child): one is a UI created only with Qt Creator (drag-and-drop), and the other one an animation done in Qt with OpenGL. I am trying to add the animation in the UI and here is the code:

glwidget.h (animation):


class GLWidget : public QGLWidget
{
public:
    GLWidget(QWidget *parent);
    ~GLWidget();
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
    void drawCube(int i, GLfloat z, GLfloat ri, GLfloat jmp, GLfloat amp);
    QGLFramebufferObject *fbo;
};
and glwidget.cpp:

GLWidget::GLWidget(QWidget *parent)
  : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
    makeCurrent();
    fbo = new QGLFramebufferObject(512, 512);
    timerId = startTimer(20);
}
GLWidget::~GLWidget()
{
    glDeleteLists(pbufferList, 1);
    delete fbo;
}
void GLWidget::initializeGL()
{....
As for the UI, I have the header file:

class ClaraTeCourseSimulator : public QMainWindow
{
    Q_OBJECT
public:
    explicit ClaraTeCourseSimulator(QWidget *parent = 0);
    ~ClaraTeCourseSimulator();
private:
    Ui::ClaraTeCourseSimulator *ui;
    GLWidget *defaultAnim;
protected:
    void setupActions();
protected slots:
    void addAnimWidget();
};
and the .cpp file:

ClaraTeCourseSimulator::ClaraTeCourseSimulator(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ClaraTeCourseSimulator)
{
    ui->setupUi(this);
    defaultAnim = new GLWidget(ui->centralWidget);
}
void ClaraTeCourseSimulator::setupActions()
{
    connect(ui->actionCar_Modelling, SIGNAL(triggered(bool)), ui->centralWidget,
            SLOT(addAnimWidget()));
}
void ClaraTeCourseSimulator::addAnimWidget()
{
    ui->centralWidget->layout()->addWidget(defaultAnim);
}
ClaraTeCourseSimulator::~ClaraTeCourseSimulator()
{
    delete ui;
}

But when I try to run it I get about 24 of these errors: undefined reference to `imp_ZN9QGLFormatD1Ev all pointing to the constructor and destructor in glwidget.cpp.

What am I doing wrong? How can I fix this problem?

Upvotes: 0

Views: 7092

Answers (1)

azyoot
azyoot

Reputation: 1172

Are you trying to change the central widget to the GL one? Because specifying the parent for the GL widget does not change them. If you'd like to change a widget to another (using the Designer), I recommend the "promote to" feature, with which you can change a widget's actual class in the designer. So add a QWidget on the UI, and than promote it to your class (GLWidget).

It seems like the problem is with the GLFormat constructor call in the GLWidget constructor.

Upvotes: 2

Related Questions