QML을 이용하여 계산기프로그램 만들기(버튼 구성하는법..)

익명 사용자의 이미지

//헤더파일

#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
        }
    }
}
 
 
//혹시 이거 버튼 이벤트 추가해주실분 게신가요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.