(C++) QtExample10
February 24, 2017 · View on GitHub
(C++) QtExample10




This Qt example shows how to change a QLabel when a QDialog is resized, like this screenshot (png).
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
Qt project file: ./CppQtExample10/CppQtExample10.pro
exists(../../DesktopApplication.pri) { include(../../DesktopApplication.pri) } !exists(../../DesktopApplication.pri) { QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TEMPLATE = app CONFIG(debug, debug|release) { message(Debug mode) } CONFIG(release, debug|release) { message(Release mode) DEFINES += NDEBUG NTRACE_BILDERBIKKEL } QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra unix { QMAKE_CXXFLAGS += -Werror } } exists(../../Libraries/Boost.pri) { include(../../Libraries/Boost.pri) } !exists(../../Libraries/Boost.pri) { win32 { INCLUDEPATH += \ ../../../Projects/Libraries/boost_1_55_0 } } SOURCES += main.cpp SOURCES += dialog.cpp HEADERS += dialog.h FORMS += dialog.ui
./CppQtExample10/dialog.h
#ifndef DIALOG_H #define DIALOG_H #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QDialog> #pragma GCC diagnostic pop namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); Dialog(const Dialog&) = delete; Dialog& operator=(const Dialog&) = delete; ~Dialog(); protected: void resizeEvent(QResizeEvent *e); //Added private: Ui::Dialog *ui; }; #endif // DIALOG_H
./CppQtExample10/dialog.cpp
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } //Added void Dialog::resizeEvent(QResizeEvent*) { static int n_times = 0; QString s; s = s.number(n_times); ui->label->setText("Resized " + s + " times"); ++n_times; }
./CppQtExample10/main.cpp
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QApplication> #include "dialog.h" #pragma GCC diagnostic pop int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }