我是Qt的新手并尝试从Qt 5开始a text editor example但是,如果没有QtCreator,我就是这样做的。当然我安装了QtCreator,我只是想在没有QtCreator的情况下尝试这个例子。我这样做的步骤是:
将main.cpp
,notepad.cpp
和notepad.h
完全一样写在
例。 (除了include
预处理器,我写完整版
路径如:
#include <qt/QtWidgets/QMainWindow>
不仅仅是:
#include <QMainWindow>
使用QtDesigner创建notepad.ui
文件。
ui_notepad.h
生成uic-qt5 notepad.ui > ui_notepad.h
个文件
命令。notepad.pro
命令生成qmake-qt5 -project
文件。在notepad.pro
文件中添加以下行:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
使用Makefile
命令生成qmake
。
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>
答案 0 :(得分:1)
编译Ui::
文件后,Qt UIC元编译器会自动生成.ui
中命名空间的类。您的代码需要生成Ui::Notepad
并使用qmake / uic自动链接到项目中。
查看QtDesigner创建的.ui
文件的XML后,您可以看到以下行:
<class>MainWindow</class>
这意味着生成了类Ui::MainWindow
。如果您想要生成Ui::Notepad
,请在QtDesigner中打开.ui
表单,并将顶级窗口小部件从MainWindow
重命名为Notepad
。然后Ui::Notepad
将出现在您的项目中。
这会使XML看起来像<class>Notepad</class>
,这会使代码中的Ui::Notepad
可用