1

我要运行的简单代码:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
  class MainWindow;
}

class MainWindow : public QMainWindow  
{
  Q_OBJECT

public:  
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();

private:
  Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

我的 CMakeLists.txt:

project(SimpleProject)
# The version number
set (SimpleProject_VERSION_MAJOR 1)
set (SimpleProject_Version_MINOR 0)

cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

在 qtcreator 中运行时,它显示QMainWindow: No such file or directory 我使用的是 GCC 4.61(64 位)和 Qt 4.8.4(也出现在 qt5 中)。所以这似乎与我在其他地方读到的 Qt 中的变化无关。当我尝试运行 qtcreator 建议的简单 Qt 示例应用程序时,它工作正常。Qt 已安装并且 qtcreator 能够找到它。但是对于 CMake,它不会。我是否必须向我的 CMakeLists 添加一些内容以便 qtcreator 能够找到 Qt?

4

1 回答 1

1

我阅读了文档并写了这个并且它有效:

cmake_minimum_required(VERSION 2.8)
PROJECT(SimpleProject)

FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})

SET(SimpleProject_SOURCES main.cpp MainWindow.cpp)
SET(SimpleProject_FORMS MainWindow.ui)
SET(SimpleProject_HEADERS MainWindow.h)

QT4_WRAP_CPP(SimpleProject_HEADERS_MOC ${SimpleProject_HEADERS})
QT4_WRAP_UI(SimpleProject_FORMS_HEADERS ${SimpleProject_FORMS})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
ADD_EXECUTABLE(SimpleProject
    ${SimpleProject_SOURCES}
    ${SimpleProject_HEADERS_MOC}
    ${SimpleProject_FORMS_HEADERS}
)
TARGET_LINK_LIBRARIES(SimpleProject ${QT_LIBRARIES})
于 2013-10-12T12:28:58.503 回答