Qt自动生成的表单提供了错误的源代码文件路径

Qt auto generated form provides wrong path to source code files

本文关键字:错误 源代码 文件 路径 自动生成 表单 Qt      更新时间:2023-10-16

我已经将qcustomplot.h/.c文件添加到我的Qt项目中。它们位于:"QT_PROJECT_DIR/QCustomPlot/"。

每次我在Qt Creator中使用设计器并构建时,都会在ui_mainwindow.h中收到此错误:

error: ../../qcustomplot.h: No such file or directory
#include "../../qcustomplot.h"

这当然是正确的,因为它位于:"QT_PROJECT_DIR/QCustomPlot/"

如何更改Qt Designer自动生成此路径的方式?

如果有帮助,这是我的 .pro:

#-------------------------------------------------
#
# Project created by QtCreator 2014-01-12T00:44:44
#
#-------------------------------------------------
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets widgets opengl printsupport
TARGET = Test
TEMPLATE = app
SOURCES += main.cpp
        mainwindow.cpp 
    QCustomPlot/qcustomplot.cpp
HEADERS  += mainwindow.h 
    QCustomPlot/qcustomplot.h
FORMS    += mainwindow.ui
CONFIG += mobility
MOBILITY = 

您的包含似乎是错误的:

#include "../../qcustomplot.h"

由于您没有将 QCustomPlot 文件夹添加到INCLUDEPATH 中,因此您需要包含该文件,如下所示:

#include "QCustomPlot/qcustomplot.h"

或者,您可以按如下方式更改INCLUDEPATH

INCLUDEPATH += $$QT_PROJECT_DIR/QCustomPlot/

然后,您可以包含没有丑陋../../相对引用的标题,如下所示:

#include "qcustomplot.h"
根据经验,避免在

源文件和头文件本身中使用此类相对包含路径,因为结构可以随时更改,或者在不同的计算机上可能不同,等等。它最好由构建系统解决并包含路径。

我遇到了同样的问题并解决了它。

它是由 xxx.ui 中 qcustomplot 对象的路径引起的。

步骤1:打开包含qcustomplot项目的***.ui文件。

步骤2:右键单击qcustomplot项目

步骤3:双击QCustomPlot的路径,然后将其更改为QCustomPlot/qcustomplot.cpp from ../../customplot .

最后,ui_***.h的标头可以自动包含QCustomPlot/qcustomplot.cpp,而不是../../qcustomplot.h

我的母语不是英语。如果您在我的回复中发现语法错误,请通知我。让我纠正一下。