ウィジェットをそのクラスに昇格させた後、Qtcreator はクラス ヘッダー ファイルを見つけることができませんか?



私は 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 .


私がどこで間違いを犯しているのか教えていただけますか?このクラスを作成するために、私はこのビデオに従いました


答え:


ui_*.h ファイルから「C1083:インクルード ファイル:'imageaction.h':そのようなファイルまたはディレクトリはありません」というエラーが表示されると思います。その場合は、imageaction ウィジェットのプロモーションに関する問題です。


This may work
1. while promoting imageaction widget, uncheck "globalinclude".
or
2. Update pro file with "INCLUDEPATH += path where mywidget.h"

詳細については、ウィジェットのプロモーションを確認してください


いくつかのコードの回答


#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
#include "imageaction.h"  imageaction::imageaction(QWidget *parent) :
QLabel(parent) { } void imageaction::mousePressEvent(QMouseEvent *ev) {
emit Mouse_Pressed();
}
SOURCES += main.cpp\
mainwindow.cpp \
imageaction.cpp HEADERS += mainwindow.h \
imageaction.h
This may work 1. while promoting imageaction widget, uncheck "globalinclude".
or 2. Update pro file with "INCLUDEPATH += path where mywidget.h"