Skip to content
Snippets Groups Projects
Commit 01c26835 authored by tobiglaser's avatar tobiglaser
Browse files

Initial Commit

parents
No related branches found
No related tags found
No related merge requests found
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
# build directory
build*/
#vscode
.vscode/
# not using Qt Creator
CMakeLists.txt.user
\ No newline at end of file
cmake_minimum_required(VERSION 3.5)
add_subdirectory(Counter)
\ No newline at end of file
cmake_minimum_required(VERSION 3.5)
project(Counter VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
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)
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
myLineEdit.cpp
myLineEdit.h
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(Counter
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET Counter APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(Counter SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(Counter
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(Counter PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
set_target_properties(Counter PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(Counter)
endif()
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include <QPushButton>
#include <QLineEdit>
#include <QBoxLayout>
#include "myLineEdit.h"
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//general
setWindowTitle("Counter");
// Layout
QBoxLayout *layout = new QBoxLayout(QBoxLayout::Direction::TopToBottom);
QWidget *widget = new QWidget;
widget->setLayout(layout);
this->setCentralWidget(widget); // special for MainWindow
// Increment Button
QPushButton *plusButton = new QPushButton("Increase");
layout->addWidget(plusButton);
connect(plusButton, SIGNAL(clicked()), this, SLOT(onPlusButton()));
// Derived Display
myLineEdit *mydisplay = new myLineEdit("0"); //Constructors are inherited
mydisplay->setReadOnly(true);
mydisplay->setAlignment(Qt::AlignCenter);
QFont font1 = mydisplay->font();
font1.setPointSize(font1.pointSize() + 8);
mydisplay->setFont(font1);
layout->addWidget(mydisplay);
connect(this, SIGNAL(counterChanged(int)), mydisplay, SLOT(updateFromInt(int)));
// Decrement Button
QPushButton *minusButton = new QPushButton("Decrease");
layout->addWidget(minusButton);
connect(minusButton, SIGNAL(clicked()), this, SLOT(onMinusButton()));
// Q Display
QLineEdit *qdisplay = new QLineEdit("0");
qdisplay->setReadOnly(true);
qdisplay->setAlignment(Qt::AlignCenter);
QFont font2 = qdisplay->font();
font2.setPointSize(font2.pointSize() + 8);
qdisplay->setFont(font2);
layout->addWidget(qdisplay);
connect(this, SIGNAL(counterChanged(const QString&)), qdisplay, SLOT(setText(const QString&)));
}
void MainWindow::onPlusButton()
{
++Counter;
//For derived display
emit(counterChanged(Counter));
//For Q display
QString str = QString(std::to_string(Counter).c_str());
QString& strRef = str;
emit(counterChanged(strRef));
}
void MainWindow::onMinusButton()
{
--Counter;
//For derived display
emit(counterChanged(Counter));
//For Q display
QString str = QString(std::to_string(Counter).c_str());
emit(counterChanged(str));
}
MainWindow::~MainWindow()
{
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
int Counter = 0;
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void onPlusButton();
void onMinusButton();
signals:
void counterChanged(int);
void counterChanged(const QString &);
};
#endif // MAINWINDOW_H
#include "myLineEdit.h"
void myLineEdit::updateFromInt(int num)
{
QString str = QString(std::to_string(num).c_str());
this->setText(str);
}
\ No newline at end of file
#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H
#include <QLineEdit>
class myLineEdit : public QLineEdit
{
Q_OBJECT
public:
using QLineEdit::QLineEdit; // inherit constructors of QLineEdit
public slots:
void updateFromInt(int);
};
#endif // MYLINEEDIT_H
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment