自从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程序, 首先:

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

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

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

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

qt5_add_resources来生成对应的CPP文件。

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

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

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

05-11 16:55
查看更多