我是 Qt 的新手,在 C++ 方面也没有太多经验。
我创建了简单的 Qt GUI 应用程序,但随后我必须在
mousepressevent
类型对象上添加 QLabel
函数,因此我使用以下代码创建了具有头文件的类:
#ifndef IMAGEACTION_H
#define IMAGEACTION_H
#include <QLabel>
#include <QMouseEvent>
#include <QDebug>
#include <QEvent>
class imageaction : public QLabel
{
Q_OBJECT
public:
explicit imageaction(QWidget *parent = 0);
void mousePressEvent(QMouseEvent *ev);
signals:
void Mouse_Pressed();
public slots:
};
#endif // IMAGEACTION_H
.cpp
文件有以下代码:
#include "imageaction.h"
imageaction::imageaction(QWidget *parent) :
QLabel(parent)
{
}
void imageaction::mousePressEvent(QMouseEvent *ev)
{
emit Mouse_Pressed();
}
在
mainwindow.cpp
文件中添加了行 #include "imageaction.h"
以包含头文件,在 .pro
文件中还添加了以下行:
SOURCES += main.cpp\
mainwindow.cpp \
imageaction.cpp
HEADERS += mainwindow.h \
imageaction.h
但是程序总是报如下错误:
C1083: Cannot open include file:'imageaction.h': No such file or directory
.
你能指出我哪里出错了吗?为了上这门课,我遵循了这个video
我认为,“C1083:无法打开包含文件:'imageaction.h':没有这样的文件或目录”错误来自您的 ui_*.h 文件。如果是这种情况,您的问题是关于推广 imageaction 小部件。
This may work
1. while promoting imageaction widget, uncheck "globalinclude".
or
2. Update pro file with "INCLUDEPATH += path where mywidget.h"
请查看更多信息推广小部件
对于 CMake,它需要包含要使用的目录路径。在 CMakeLists 中,这可以通过包含目录指令来完成。例如:
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
如果头文件位于单独的包含文件夹中,则应将其写入 CMakeLists 中:
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
问题只是你在cmake中的路径,所以只需将此代码放入你的CMakeLists.txt中:
target_include_directories(<YOUR PROJECT NAME> PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})