QML을 이용하여 계산기프로그램 만들기(버튼 구성하는법..)
글쓴이: 익명 사용자 / 작성시간: 목, 2019/11/28 - 5:14오후
//헤더파일
#ifndef DIALOG_H
#define DIALOG_H
#include <QQuickView>
#include <QObject>
#include <iostream>
class dialog:public QObject
{
public:
Q_OBJECT
public:
dialog();
~dialog();
void cppSignaltoQmlSlot();
void setWindow(QQuickWindow* Window);
private:
QQuickWindow* mMainView;
signals:
void cppSignaltestData(QVariant);
public slots:
void cppSlotStringData(QVariant stringData);
void cppSlotintData(QVariant intdata);
};
#endif // DIALOG_H
//class 부분
<code lang="c++>
#include "dialog.h"
dialog::dialog()
{
}
dialog::~dialog()
{
}
void dialog::cppSignaltoQmlSlot()
{
QObject::connect(this, SIGNAL(cppSignaltestData(QVariant)), mMainView, SLOT(qmlSlotTestData(QVariant)));
QObject::connect(mMainView, SIGNAL(qmlSignalStringData(QVariant)), this, SLOT(cppSlotStringData(QVariant)));
QObject::connect(mMainView, SIGNAL(qmlSignalintData(QVariant)), this, SLOT(cppSlotintData(QVariant)));
}
void dialog::cppSlotStringData(QVariant stringData)
{
}
void dialog::cppSlotintData(QVariant intdata)
{
}
//c++main 부분
<code lang="c++">
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "dialog.h"
#include "QtMath"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
qRegisterMetaType<dialog*>("dialog*");
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
dialog *CE = new dialog();
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
//qml부분
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
//import dialog
Window {
visible: true
width: 640
height: 480
title: qsTr("Calculator")
signal qmlSinalStringData(var stringData);
signal qmlSignalintData(var intData);
function qmlSlotTestData(data){
console.log("qmlSlotTestData data:"+data);
}
// property int value1: 0
Rectangle{
x: 65
y: 55
color: "gray"
width: 510
height: 273
anchors.fill: parent
Rectangle {
x: 8
y: 42
width: 162
height: 61
color: "#ffffff"
Label {
id: firstLabel
x: 8
y: 8
width: 146
height: 45
text: "1"
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignLeft
// horizontalCenter: Text.AlignHCenter
font.pixelSize: 15
}
}
Rectangle {
x: 215
y: 42
width: 123
height: 61
color: "#ffffff"
Label {
id: secondLabel
x: 0
y: 8
width: 123
height: 45
text: "1"
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignLeft
// horizontalCenter: Text.AlignHCenter
font.pixelSize: 15
}
}
Rectangle {
x: 380
y: 42
width: 123
height: 61
color: "#ffffff"
Label {
id: thirdLabel
x: 0
y: 8
width: 123
height: 45
text: "1"
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignLeft
// horizontalCenter: Text.AlignHCenter
font.pixelSize: 15
}
}
Rectangle {
id : button0
x: 105
y: 233
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: button00
text: qsTr("00")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData(0);
// dialog
}
}
}
Rectangle {
id : button1
x: 8
y: 125
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: button01
text: qsTr("01")
color: "black"
anchors.centerIn: parent
}MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData(1);
}
}
}
Rectangle {
id : button2
x: 105
y: 125
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: button02
text: qsTr("02")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData(2);
}
}
}
Rectangle {
id : button3
x: 202
y: 125
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: button03
text: qsTr("03")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData(3);
}
}
}
Rectangle {
id : button4
x: 8
y: 161
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: button04
text: qsTr("04")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData(4);
}
}
}
Rectangle {
id : button5
x: 105
y: 161
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: button05
text: qsTr("05")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData(5);
}
}
}
Rectangle {
id : button6
x: 202
y: 161
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: button06
text: qsTr("06")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData(6);
}
}
}
Rectangle {
id : button7
x: 8
y: 197
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: button07
text: qsTr("07")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData(7);
}
}
}
Rectangle {
id : button8
x: 105
y: 197
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: button08
text: qsTr("08")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData(8);
}
}
}
Rectangle {
id : button9
x: 202
y: 197
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: button09
text: qsTr("09")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.topMargin: 0
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData(9);
}
}
}
Rectangle {
id : buttonc
x: 8
y: 233
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: buttonC
text: qsTr("C")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalStringData("c")
}
}
}
Rectangle {
id : buttonEq
x: 202
y: 233
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: buttoneq
text: qsTr("=")
font.pointSize: 12
renderType: Text.QtRendering
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalStringData("=")
}
}
}
Rectangle {
id : buttonPlus
x: 299
y: 197
width: 91
height: 66
color: "beige"
radius: 5
Text {
id: buttonplus
text: qsTr("+")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalStringData("+")
}
}
}
Rectangle {
id : buttonMin
x: 299
y: 161
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: buttonmin
text: qsTr("-")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalintData("m")
}
}
}
Rectangle {
id : buttonGop
x: 299
y: 125
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: buttongop
text: qsTr("*")
font.pointSize: 15
fontSizeMode: Text.VerticalFit
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalStringData("g")
}
}
}
Rectangle {
id : buttonNa
x: 396
y: 161
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: buttonna
text: qsTr("/")
font.pointSize: 13
font.family: "Verdana"
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalStringData(n)
}
}
}
Rectangle {
id : buttonAc
x: 396
y: 125
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: buttonac
text: qsTr("AC")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.rightMargin: 0
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalStringData(A)
}
}
}
Rectangle {
id : buttonJe
x: 396
y: 197
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: buttonje
text: qsTr("^")
font.pointSize: 12
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalStringData(j)
}
}
}
Rectangle {
id : buttonClose
x: 396
y: 233
width: 91
height: 30
color: "beige"
radius: 5
Text {
id: buttonclose
text: qsTr("close")
color: "black"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
console.log("qml signal click the Button")
qmlSignalStringData(e)
}
}
}
Text {
id: textcal
x: 176
y: 60
width: 33
height: 34
text: qsTr("연산자")
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
font.pixelSize: 12
}
Text {
id: textEq
x: 344
y: 60
width: 30
height: 34
text: qsTr("=")
font.bold: true
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
font.pixelSize: 20
}
}
}
//혹시 이거 버튼 이벤트 추가해주실분 게신가요?Forums:

댓글 달기