메모내용
Nav

QT

Qt in VisualStudio

쁜사의 아틀리에 :: VisualStudio 2022 + QT
배경 투명하게

Qt Widget{ DesignLogic (C++) FunctionLogic ( c++) } Qt Quick{ DesignLogic (QML) Qt Modeling Language FunctionLogic (C++) }

Qt Quick

c++ on the back, declarative UI design(QML) in the front for beautiful, modern touch-based user experience.

QML 은 인터프리터 언어고 c++ 은 컴파일 언어다. QML 은 소스코드 전체를 컴파일 하지 않고 QML 은 한줄한줄 파싱하는식으로 진행된다. Qt Declarative QT 파싱해주는 계층

tr : translation UI 를 c++ 을 통해서 작성할 수도 있고 QtCreator.Designer 를 가지고 작성할 수도 있다. (이는 내부적으로 cpp 파일로 만들어준다.) MOC ( Meta object compiler)

QDesigner 를 활용한 widget 구현

                        
// QT_VS_1.h

#pragma once

#include <QtWidgets/QWidget>
#include "ui_QT_VS_1.h"

class QT_VS_1 : public QWidget
{
    Q_OBJECT

public:
    QT_VS_1(QWidget *parent = nullptr);
    ~QT_VS_1();

private:
    Ui::QT_VS_1Class ui;

private slots:

    void slot_pushButton();
};

                        
                    
                        
// QT_VS_1.cpp

#include "QT_VS_1.h"
#include <qdebug.h>

QT_VS_1::QT_VS_1(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    // connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(slot_pushButton()));

    /*
    새로 추가된 방법
    이 경우에는 slot 함수(private slots: 에 선언된 함수)가 아니어도 일반함수도 쓸 수 있다.
    하지만, 인자(argument) 선언에 대해서 제한사항이 있다고 한다.
    */
    connect(ui.pushButton, &QPushButton::clicked, this, &QT_VS_1::slot_pushButton);
}

void QT_VS_1::slot_pushButton()
{
    // Q_FUNC_INFO 는 객체명과 함수명까지 출력하기 위한 매크로
    qDebug() << Q_FUNC_INFO << "push button pushed";
}

QT_VS_1::~QT_VS_1()
{}

                        
                    
                        
// main.cpp

#include "QT_VS_1.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QT_VS_1 w;
    w.show();
    return a.exec();
}

                        
                    

WebView

                        
#include <QtGui/QApplication>
#include <QWebView>

int main (int argc, char* argv)
{
    QApplication a(argc, argv);

    QWebView webView();
    webView.Show();
    webView.load(QUrl("http://www.google.com"));

    return a.exec();

}
                        
                    

HTML in Qt

qt 에서는 html 과 css 문법을 일부 지원한다. 너무 복잡한 css 는 지원되지 않지만, 간단한것은 구현된다.

                        
#include "MainWidget.h"
#include "qlabel.h"

#define Img_1 ":/MainWidget/Resource/Astronaut.jpg"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);


    QLabel* textBar = new QLabel(this);
    textBar->setText(R"(
        <h1> text area </h1>
        <p>
            hello world
            good
        </p>

        /*
            QLabel 은 네트워크를 사용할 수 없기 때문에 로컬에 있는 이미지만 가능하다.
            웹상의 이미지 주소를 사용하고 싶다면,
            QTextBrowser 를 사용해야 한다. 이는 내부적으로 QNetworkAccessManager 를 사용할 수 있어 http 이미지 지원이 된다.
            style = "width :100px ; height : 100px" 같은 인라인 CSS 는 무시된다고 한다.
            style 속성에 CSS 를 제대로 해석하지 못하기 때문에, HTML 속성 방식으로 해야 한다. 
            HTML 표준 속성은 지원한다.        
        */
        <img src=":/MainWidget/Resource/Astronaut.jpg" width="100" height="100"/>
        
        <p>
            submit<br/>yeas
        </p>

    )");

    textBar->setGeometry(0, 0, 500, 1200);

}

MainWidget::~MainWidget()
{}