c++ - cmake 和 Qt 样板,无法初始化类型为 'QMainWindow *' 的参数错误

标签 c++ qt qt4 cmake

我正在尝试使用 cmake 构建样板 C++ 应用程序,主窗口定义在名为 main_window.xml 的 .ui 文件中。我遵循了一些教程并尝试拼凑出一种方法,但我的知识充其量是零散的,而且我遇到了一个我不知道如何修复的错误。

有谁知道我哪里出错了,或者我什至是找对了树。

这是main.cpp

#include <QApplication>
#include "ui_main_window.h"

int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     QWidget *widget = new QWidget;
     Ui::MainWindow ui;
     ui.setupUi(widget);

     widget->show();
     return app.exec();
 }

这是 CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(test_proj)
FIND_PACKAGE(Qt4 REQUIRED)

# include the current binary output directory as thats where the intermediate Qt fiels will be placed
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})

# set sources of program
# only files that need preprocessgin by Qt need be included
SET(test_proj_SOURCES main.cpp)
SET(test_proj_HEADERS ui_main_window.h)
SET(test_proj_FORMS main_window.ui)

# this block creates .cpp and .h files from the .ui files
QT4_WRAP_CPP(test_proj_HEADERS_MOC ${test_proj_HEADERS})
QT4_WRAP_UI(test_proj_FORMS_HEADERS ${test_proj_FORMS})
# QT4_ADD_RESOURCES(test_proj_RESOURCES_RCC ${test_proj_RESOURCES})


ADD_EXECUTABLE(test_proj ${test_proj_SOURCES} 
                          ${test_proj_HEADERS}
                          ${test_proj_FORMS})

TARGET_LINK_LIBRARIES(test_proj ${QT_LIBRARIES})

这是我得到的编译错误

Jonathans-MacBook-Pro:build jonathantopf$ make clean;make
[ 50%] Generating ui_main_window.h
Scanning dependencies of target laser_scan
[100%] Building CXX object CMakeFiles/laser_scan.dir/main.cpp.o
/projects/laser_scanner/src/main.cpp:49:17: error: cannot initialize a parameter of type 'QMainWindow *' with an lvalue of type
      'QWidget *'
     ui.setupUi(widget);
                ^~~~~~
/projects/laser_scanner/build/ui_main_window.h:48:31: note: passing argument to parameter 'MainWindow' here
    void setupUi(QMainWindow *MainWindow)
                              ^
1 error generated.
make[2]: *** [CMakeFiles/laser_scan.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/laser_scan.dir/all] Error 2
make: *** [all] Error 2
Jonathans-MacBook-Pro:build jonathantopf$ 

最佳答案

您的主窗口继承自QMainWindow,因此您应该在您的主函数中将QWidget 替换为QMainWindow

然而,没有表单类的表单是不常见的。我建议您在 Qt Creator 中使用“Add new - Qt - Designer Form Class”创建一个类。

关于c++ - cmake 和 Qt 样板,无法初始化类型为 'QMainWindow *' 的参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17616889/

相关文章:

c++ - 重载函数和继承

c++ - 我是否必须手动实现比较运算符的交换性?

python - qtextbrowser 右对齐或居中对齐

c++ - 如何在 Qt QImage 中使用颜色 LUT

c++ - 使用 boost::geometry::append 时,自定义点类中的 ID 字段间歇性丢失

c++ - 模板专业类中的模板特化

c++ - EnhTableWidget 字段的值更改(返回前)时发出信号

c++ - Qt、按钮、id 属性?任何知道点击哪个按钮的方法

python - 无法发送另一个线程中对象的已发布事件

python - 加载了错误的 Mac OS X 框架