QtCreator手动编译qt项目的步骤如何做?

How to do QtCreator's steps in compiling qt project manually?

我是 Qt 的新手,正在尝试从 Qt 5 开始 a text editor example。但是,我在没有 QtCreator 的情况下这样做。当然我安装了 QtCreator,我只是想尝试在没有 QtCreator 的情况下做那个例子。我这样做的步骤是:

  1. main.cppnotepad.cppnotepad.h 例子。 (除了 include 预处理器,我写了完整的 路径如:

    #include <qt/QtWidgets/QMainWindow>
    

    不只是:

    #include <QMainWindow>
    
  2. 使用 QtDesigner 创建 notepad.ui 文件。

  3. uic-qt5 notepad.ui > ui_notepad.h 生成 ui_notepad.h 文件 命令。
  4. 使用 qmake-qt5 -project 命令生成 notepad.pro 文件。
  5. notepad.pro 文件中添加这些行:

    QT += core gui
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
  6. 使用 qmake 命令生成 Makefile

  7. 执行make命令。

这些步骤是我对 QtCreator 如何自动完成任务的理解。然后,make 投诉不完整的类型和前向声明。但是,如果我使用 QtCreator 执行此操作,则项目编译得很好。

我错过了什么?

这些是我收到的错误消息:

notepad.cpp: In constructor ‘Notepad::Notepad(QWidget*)’:
notepad.cpp:4:72: error: invalid use of incomplete type ‘class Ui::Notepad’
 Notepad::Notepad (QWidget* parent) : QMainWindow (parent), ui (new Ui::Notepad) {
                                                                    ^
In file included from notepad.cpp:1:0:
notepad.h:4:8: error: forward declaration of ‘class Ui::Notepad’
  class Notepad;
    ^
notepad.cpp:5:4: error: invalid use of incomplete type ‘class Ui::Notepad’
  ui->setupUi (this);
    ^
In file included from notepad.cpp:1:0:
notepad.h:4:8: error: forward declaration of ‘class Ui::Notepad’
  class Notepad;
    ^
notepad.cpp: In destructor ‘virtual Notepad::~Notepad()’:
notepad.cpp:9:9: warning: possible problem detected in invocation of delete operator: [-Wdelete-incomplete]
  delete ui;
     ^
notepad.cpp:9:9: warning: invalid use of incomplete type ‘class Ui::Notepad’
In file included from notepad.cpp:1:0:
notepad.h:4:8: warning: forward declaration of ‘class Ui::Notepad’
  class Notepad;
    ^
notepad.cpp:9:9: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
  delete ui;
     ^
Makefile:660: recipe for target 'notepad.o' failed
make: *** [notepad.o] Error 1

更新

ui_notepad.h 文件:

/********************************************************************************
** Form generated from reading UI file 'notepad.ui'
**
** Created by: Qt User Interface Compiler version 5.4.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_NOTEPAD_H
#define UI_NOTEPAD_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralwidget;
    QVBoxLayout *verticalLayout_2;
    QVBoxLayout *verticalLayout;
    QTextEdit *textEdit;
    QPushButton *quitButton;
    QMenuBar *menubar;
    QStatusBar *statusbar;

    void setupUi(QMainWindow *MainWindow)
    {
    if (MainWindow->objectName().isEmpty())
        MainWindow->setObjectName(QStringLiteral("MainWindow"));
    MainWindow->resize(800, 600);
    centralwidget = new QWidget(MainWindow);
    centralwidget->setObjectName(QStringLiteral("centralwidget"));
    verticalLayout_2 = new QVBoxLayout(centralwidget);
    verticalLayout_2->setObjectName(QStringLiteral("verticalLayout_2"));
    verticalLayout = new QVBoxLayout();
    verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
    textEdit = new QTextEdit(centralwidget);
    textEdit->setObjectName(QStringLiteral("textEdit"));

    verticalLayout->addWidget(textEdit);

    quitButton = new QPushButton(centralwidget);
    quitButton->setObjectName(QStringLiteral("quitButton"));

    verticalLayout->addWidget(quitButton);


    verticalLayout_2->addLayout(verticalLayout);

    MainWindow->setCentralWidget(centralwidget);
    textEdit->raise();
    quitButton->raise();
    menubar = new QMenuBar(MainWindow);
    menubar->setObjectName(QStringLiteral("menubar"));
    menubar->setGeometry(QRect(0, 0, 800, 27));
    MainWindow->setMenuBar(menubar);
    statusbar = new QStatusBar(MainWindow);
    statusbar->setObjectName(QStringLiteral("statusbar"));
    MainWindow->setStatusBar(statusbar);

    retranslateUi(MainWindow);

    QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
    MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
    quitButton->setText(QApplication::translate("MainWindow", "Quit", 0));
    } // retranslateUi

};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_NOTEPAD_H

notepad.ui 文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout_2">
    <item>
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
       <widget class="QTextEdit" name="textEdit"/>
      </item>
      <item>
       <widget class="QPushButton" name="quitButton">
    <property name="text">
     <string>Quit</string>
    </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
   <zorder>textEdit</zorder>
   <zorder>quitButton</zorder>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>27</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Ui:: 中的命名空间由 Qt UIC 元编译器在编译 .ui 文件后自动生成。您的代码需要生成 Ui::Notepad 并使用 qmake/uic 自动链接到项目中。

查看 QtDesigner 创建的 .ui 文件的 XML 后,您可以看到以下行:

<class>MainWindow</class>

即生成了classUi::MainWindow。如果您想改为生成 Ui::Notepad,请在 QtDesigner 中打开 .ui 表单并将顶级小部件从 MainWindow 重命名为 Notepad。然后Ui::Notepad就会出现在你的项目中。

这会使 XML 看起来像 <class>Notepad</class>,这将使 Ui::Notepad 在您的代码中可用