Skip to content

Qt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
if (NOT CMAKE_PREFIX_PATH AND NOT Qt5Widgets_DIR)
    message("CMAKE_PREFIX_PATH is not defined, you may need to set it "
            "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")

    message("CMAKE_PREFIX_PATH is not defined."
            "find_package(Qt5) is probably not going to work. "
            " Set the CMAKE_PREFIX_PATH environment variable to the install prefix of Qt 5"
            " - Set it in the command line as -DCMAKE_PREFIX_PATH=\"path/to/Qt5/lib/cmake\""
            " - Or set it in your build script with set(CMAKE_PREFIX_PATH path/to/Qt5/lib/cmake)")
endif ()

1
find_package(Qt5 COMPONENTS Widgets QUIET)

1
2
3
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

1
2
add_executable(qt_helloworld qt_hello.cpp)
target_link_libraries(qt_helloworld Qt5::Widgets)

1
2
3
set(CMAKE_AUTOMOC OFF)
set(CMAKE_AUTORCC OFF)
set(CMAKE_AUTOUIC OFF)

Share Snippets


1
2
#include <QApplication>
#include <QPushButton>

1
QApplication app(argc, argv);

1
2
QWidget window;
window.setFixedSize(100, 80);

1
2
auto *buttonInfo = new QPushButton("Info", &window);
buttonInfo->setGeometry(10, 10, 80, 30);

1
2
QApplication::connect(buttonInfo, &QPushButton::clicked,
                      [] { std::cout << "Info" << std::endl; });

1
2
auto *buttonQuit = new QPushButton("Quit", &window);
buttonQuit->setGeometry(10, 40, 80, 30);

1
2
QApplication::connect(buttonQuit, &QPushButton::clicked,
                      [] { QApplication::quit(); });

1
2
window.show();
return app.exec();

Share Snippets