CMake编译Widget UI Qt程序

自从CMake被引入到KDE项目的编译系统中后,CMake的使用者日益增多,Qt也不例外,除了使用QMAKE编译Qt程序外,也可以使用CMake来编译Qt程序,并且CMake在使用上更灵活,特别是大型程序。

CMake对于Qt4和Qt5都支持,不过使用上有点差异,这里主要看下Qt5下使用CMake编译Qt程序。

官方文档链接: http://qt-project.org/doc/qt-5.0/qtdoc/cmake-manual.html

这里是针对CMake 2.8.9版本以及之后的版本。

对于一个Widget UI的Qt程序, 首先:

[plain] view plain copy
 
  1. # Find includes in corresponding build directories  
  2. set(CMAKE_INCLUDE_CURRENT_DIR ON)  
  3.   
  4. # Instruct CMake to run moc automatically when needed  
  5. set(CMAKE_AUTOMOC ON)  
  6.   
  7. set(EXECUTABLE_OUTPUT_PATH  "${PROJECT_SOURCE_DIR}/bin")  
  8.   
  9. # Find the QtWidgets library  
  10. find_package(Qt5Widgets)  


假设我们的UI程序中的界面是通过Qt Designer 设计的,则接下来CMake的内容如下:

[plain] view plain copy
 
  1. qt5_wrap_ui(ui_FILES gotocelldialog.ui)  
  2. add_executable(gotocelldialog  
  3.      main.cpp  
  4.      ${ui_FILES}  
  5.  )  
  6.   
  7.  # Use the Widgets module from Qt 5  
  8.  qt5_use_modules(gotocelldialog Widgets)  


另外, 如果创建的资源文件,则需要

qt5_add_resources来生成对应的CPP文件。

最后,编写我们的main函数:

[cpp] view plain copy
 
    1. #include <QApplication>  
    2. #include <QDialog>  
    3.   
    4. #include "ui_gotocelldialog.h"  
    5.   
    6. int main(int argc, char *argv[])  
    7. {  
    8.     QApplication app(argc, argv);  
    9.     Ui::GoToCellDialog ui;  
    10.     QDialog *dialog = new QDialog;  
    11.     ui.setupUi(dialog);  
    12.     dialog->show();  
    13.   
    14.     return app.exec();  
    15. }  

http://blog.csdn.net/fuyajun01/article/details/8987706

原文地址:https://www.cnblogs.com/findumars/p/6052199.html