Qt文档阅读笔记-QCustom3DLabel使用及Q3DSurface基本信号

此实例主要是QCustom3DLabel的基本使用,以及Q3DSurface发射信号与C++后端的交互工作。

 

QCustom3DLabel:这个自定义的label类可以设置文本,字体,位置,放缩,旋转,颜色。看得见的边框和背景是可以被触发的。这里颜色、边框、背景有默认值,这个默认值是根据主题会变的(估计是windows或者Linux,或者各个系统的不同版本)

 

selectedElementChanged这个信号是Q3DSurface从QAbstract3DGraph父类继承下来的。当界面有Item被选中的时候会被触发。

 

 

程序运行截图如下!

当点击X,Y,Z轴坐标的时候,LineEdit会被设置。

Qt文档阅读笔记-QCustom3DLabel使用及Q3DSurface基本信号_第1张图片

Qt文档阅读笔记-QCustom3DLabel使用及Q3DSurface基本信号_第2张图片

Qt文档阅读笔记-QCustom3DLabel使用及Q3DSurface基本信号_第3张图片

当点击那个Label时,会对Label进行动态放缩。

Qt文档阅读笔记-QCustom3DLabel使用及Q3DSurface基本信号_第4张图片

当在LineEdit中输入文本时,按下pushbutton,会对Label的数据进行设置

Qt文档阅读笔记-QCustom3DLabel使用及Q3DSurface基本信号_第5张图片

关键代码如下:

#include "widget.h"
#include "ui_widget.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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(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

 

你可能感兴趣的:(Qt3D,C/C++,文档阅读笔记)