Skip to content

Instantly share code, notes, and snippets.

@mortbopet
Created April 16, 2023 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mortbopet/a1641dfc4c0f09d8002232058ea8a7e8 to your computer and use it in GitHub Desktop.
Save mortbopet/a1641dfc4c0f09d8002232058ea8a7e8 to your computer and use it in GitHub Desktop.
Qt dialog example
cmake_minimum_required(VERSION 3.5)
project(diagtest VERSION 0.1 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
qt_add_executable(diagtest
MANUAL_FINALIZATION
main.cpp
)
target_link_libraries(diagtest PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
# https://doc.qt.io/qt-6/wasm.html#asyncify
target_link_options(diagtest PUBLIC -sASYNCIFY -Os)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(diagtest)
endif()
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <iostream>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QMainWindow w;
auto button = new QPushButton("Click me");
auto label = new QLabel("Result");
auto layout = new QVBoxLayout;
layout->addWidget(button);
layout->addWidget(label);
auto *widget = new QWidget;
widget->setLayout(layout);
w.setCentralWidget(widget);
auto *wptr = &w;
button->connect(button, &QPushButton::clicked, label, [=]() {
auto res = QMessageBox::question(wptr, "Question", "press yes or no");
std::cout << "Hello!" << res << std::endl;
// A new message box which shows the value of the result
QMessageBox::information(wptr, "Result", QString::number(res));
label->setText(QString::number(res));
});
w.showMaximized();
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment