c++ - 在 Eclipse 中使用 Qt Designer 创建的 Qt GUI

标签 c++ eclipse qt user-interface qt-designer

我是 Qt 的新手,但我设法让一些简单的窗口和框架正常工作。我在 64 位 Kubuntu 系统上运行 Eclipse。在 Eclipse 中编辑我的代码后,我运行 qmake 并从命令行生成。

昨天我尝试了 Qt Designer,与在 eclipse 中“硬编码”小部件和按钮相比,它对我来说效果很好。 但是,我不会切换到 QT Creator,因为我已经非常习惯 Eclipse。

我的目标是在 Qt Designer 中创建 GUI 并在 Eclipse 中使用生成的代码。

运行 qmake -project、qmake 和 make 为我的主窗口生成一个头文件,其中包含类代码。

但是当我尝试在我的 Eclipse 代码中包含这个头文件时,make 停止并出现错误,因为 g++ 找不到头文件(似乎头文件尚未创建)。

我不得不承认,我仍然对 Qt 构建过程感到困惑。 那么,我如何才能通过 Qt Designer 设计我的 GUI 并仍然使用 Eclipse 作为我的编码 IDE?

您好, 行李箱

编辑: 我的 Eclipse 项目是一个“空 Makefile 项目”。据我所知,这意味着我必须负责整个构建过程。 Eclipse 不会为我做任何事情。

我没有尝试从 Eclipse 运行“make”,因为 Makefile 必须由 qmake 生成。因此,在 Eclipse 中编辑并保存我的代码后,我运行 qmake 并从命令行生成。

Qmake 感知到有一个.ui 文件并为这个类生成头文件。 问题是如果我尝试在 Eclipse 中包含此 header ,则无法编译我的源代码文件。

对我来说,当“make”用我的源代码文件调用 g++ 时,GUI 的标题还没有创建。

那我为什么要这样做呢? 好吧,我不想沉迷于 Qt Creator 之类的 IDE。因为这意味着,任何试图编译我的项目的人都需要元信息,例如该特定 IDE 的配置文件或项目特定设置。

在我的 C 项目中,我使用手写的 Makefile,因此您只需要安装 GCC(您通常已经安装)并输入“make”即可。

我正在尝试通过 Qt 项目尽可能接近这一点。

最佳答案

你需要 4 样东西:

  • Qt Designer生成的.ui

=> 关于.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>About</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>A Propos</string>
</property>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
  • 与 .ui 关联的 cpp/h,例如:

关于窗口 (about.cpp) =>

#include "about.h"
#include "ui_about.h"

About::About(QWidget *parent) : QDialog(parent), ui(new Ui::About)
{
    ui->setupUi(this);
}

About::~About()
{
    delete ui;
}

(about.h) =>

#ifndef ABOUT_H
#define ABOUT_H

#include <QDialog>

namespace Ui {
    class About;
}

class About : public QDialog
{
    Q_OBJECT
public:
    explicit About(QWidget *parent = 0);
    ~About();

private:
    Ui::About *ui;
};

#endif // ABOUT_H

在 .pro 文件中

SOURCES += about.cpp
HEADERS += about.h
FORMS   += about.ui

关于c++ - 在 Eclipse 中使用 Qt Designer 创建的 Qt GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13857393/

相关文章:

qt - QNetworkAccessManager:将响应与请求相关联

c++ - IE打开新标签页的方法

c++ - 指针递增递减语法差异

java - 如何访问http ://localhost:8080/pdf/abc123. pdf tomcat eclipse

java - Eclipse RCP : Get rid of "reset perspective" message

qt - 主动调整主窗口内小部件的大小

c++ - 菱形继承(钻石问题)

c++ - LLVM 运行 PassManager(非遗留)

eclipse - cabal install scion-browser 在 Ubuntu 12.04 上失败,因为 haskeline 需要 Cabal 库版本 >= 1.16

c++ - 如何设置与背景图片大小相对应的窗口大小?