Qt文檔閱讀筆記-QCustom3DLabel使用及Q3DSurface基本信號

此實例主要是QCustom3DLabel的基本使用,以及Q3DSurface發射信號與C++後端的交互工作。

 

QCustom3DLabel:這個自定義的label類可以設置文本,字體,位置,放縮,旋轉,顏色。看得見的邊框和背景是可以被觸發的。這裏顏色、邊框、背景有默認值,這個默認值是根據主題會變的(估計是windows或者Linux,或者各個系統的不同版本)

 

selectedElementChanged這個信號是Q3DSurface從QAbstract3DGraph父類繼承下來的。當界面有Item被選中的時候會被觸發。

 

 

程序運行截圖如下!

當點擊X,Y,Z軸座標的時候,LineEdit會被設置。

當點擊那個Label時,會對Label進行動態放縮。

當在LineEdit中輸入文本時,按下pushbutton,會對Label的數據進行設置

關鍵代碼如下:

#include "widget.h"
#include "ui_widget.h"
#include <QtDataVisualization>
#include <QCustom3DLabel>
#include <QList>
#include <QPropertyAnimation>
#include <QFont>
#include <QVector3D>
#include <QDebug>

using namespace QtDataVisualization;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_surface = new Q3DSurface;
    this->setWindowTitle("CSDN IT1995");
    createLabel("Hello World");
    m_selectionAnimation = new QPropertyAnimation(this);
    m_selectionAnimation->setPropertyName("scaling");
    m_selectionAnimation->setDuration(500);
    m_selectionAnimation->setLoopCount(-1);

    QWidget *w = QWidget::createWindowContainer(m_surface);
    QHBoxLayout *lay = new QHBoxLayout;
    lay->addWidget(w);
    ui->widget->setLayout(lay);

    connect(m_surface, &QAbstract3DGraph::selectedElementChanged, this, &Widget::handleElementSelected);
    connect(ui->pushButton, &QPushButton::clicked, [=](){

        m_surface->removeCustomItem(m_label);
        createLabel(ui->lineEdit->text());
    });
}

Widget::~Widget()
{
    delete m_surface;
    delete ui;
}

void Widget::handleElementSelected(int type)
{
    if(type == QAbstract3DGraph::ElementCustomItem){

        QCustom3DItem *item = m_surface->selectedCustomItem();
        QCustom3DLabel *p = qobject_cast<QCustom3DLabel *>(item);
        if(p != nullptr){

            ui->lineEdit->setText(p->text());
            m_selectionAnimation->setTargetObject(item);
            m_selectionAnimation->setStartValue(item->scaling());
            m_selectionAnimation->setEndValue(item->scaling() * 1.5f);
            m_selectionAnimation->start();
        }
    }
    else if(type == QAbstract3DGraph::ElementSeries){

    }
    else if(type == QAbstract3DGraph::ElementAxisXLabel){

        ui->lineEdit->setText("X Label clicked");
    }
    else if(type == QAbstract3DGraph::ElementAxisYLabel){

        ui->lineEdit->setText("Y Label clicked");
    }
    else if(type == QAbstract3DGraph::ElementAxisZLabel){

        ui->lineEdit->setText("Z Label clicked");
    }
}

void Widget::createLabel(const QString text)
{
    QFont titleFont = QFont("Century Gothic", 30);
    titleFont.setBold(true);
    m_label = new QCustom3DLabel(text, titleFont, QVector3D(0.0f, 1.2f, 0.0f), QVector3D(1.0f, 1.0f, 0.0f), QQuaternion());
    m_label->setPositionAbsolute(true);
    m_label->setFacingCamera(true);
    m_label->setBackgroundColor(QColor(0x66cdaa));
    m_surface->addCustomItem(m_label);
}

源碼下載地址如下:

https://github.com/fengfanchen/Qt/tree/master/Qt3DLabel

 

發佈了1261 篇原創文章 · 獲贊 1964 · 訪問量 178萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章