在 Qt 中将子部件添加到另一个部件

发布于 2024-11-09 07:11:18 字数 2002 浏览 2 评论 0原文

尽管 stackoverflow 上也发布了与我类似的问题,但他们的解决方案都没有真正解决我的问题。我有 2 个独立的小部件,我想组合起来(将一个小部件作为子部件插入另一个小部件):一个是仅使用 Qt Creator 创建的 UI(拖放),另一个是在 Qt 中完成的动画OpenGL。我正在尝试在 UI 中添加动画,代码如下:

glwidget.h(动画):


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;
}

但是当我尝试运行它时,我收到了大约 24 个错误:对 `imp_ZN9QGLFormatD1Ev 的未定义引用全部指向 glwidget.cpp 中的构造函数和析构函数。

我做错了什么?我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我三岁 2024-11-16 07:11:18

您是否正在尝试将中央小部件更改为 GL 小部件?因为指定 GL 小部件的父级不会改变它们。如果您想将一个小部件更改为另一个小部件(使用设计器),我建议您使用“升级到”功能,通过该功能您可以在设计器中更改小部件的实际类。因此,在 UI 上添加一个 QWidget,然后将其提升到您的类 (GLWidget)。

问题似乎出在 GLWidget 构造函数中的 GLFormat 构造函数调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文