C++ QT chip layout tool开发浅思

工作中需要利用padlist + Chip size + Chip Center + Pixel size + Pixel Center生成一副Chip Layout tool SVG图像

1.参数输入

cpp 复制代码
    QtColorPropertyManager       *m_colorManager      = nullptr;
    QtDoublePropertyManager      *m_doubleManager     = nullptr;
    QtBoolPropertyManager        *m_boolManager       = nullptr;
    QtStringPropertyManager      *m_stringManager     = nullptr;
    QtFontPropertyManager        *m_fontManager       = nullptr;

使用各个QT property manager去生成各个UI界面,确定输入的参数的类型,颜色,字体

2.使用QGraphicsView去画图,

所有的对象堆叠在Widget上,每个widget上会堆叠不同的group,group中包含一个个具有共同属性的Item

注意设定可交互性,背景大小,抗锯齿,可拖动性

cpp 复制代码
ChipLayoutWidget::ChipLayoutWidget(QWidget* parent): QGraphicsView(parent)
{

    m_scene = new QGraphicsScene(0, 0, 7200, 7200);
    QColor transparentColor(Qt::transparent); 
    QBrush transparentBrush(transparentColor);
    m_scene->setBackgroundBrush(transparentBrush);
    this->setInteractive(true);
    QMap<ChipLayoutGroup::ChipLayoutGroupType, ChipLayoutGroup *> m_groupList = {};
    for(int i = 0; i < ChipLayoutGroup::GroupTypeDefaut; i++)
    {
        m_groupMap[ChipLayoutGroup::ChipLayoutGroupType(i)] = new ChipLayoutGroup();

        m_groupMap[ChipLayoutGroup::ChipLayoutGroupType(i)]->setFlag(QGraphicsItem::ItemIsSelectable,
                                                                     false);
        m_scene->addItem(m_groupMap[ChipLayoutGroup::ChipLayoutGroupType(i)]);
    }
    this->setRenderHint(QPainter::Antialiasing);
    m_scene->setSceneRect(0, 0, 7200, 7200);
    this->setScene(m_scene);
    m_scene->setBackgroundBrush(QColor(0x33, 0x33, 0x33));
    this->setDragMode(QGraphicsView::ScrollHandDrag);
    this->setCacheMode(QGraphicsView::CacheBackground);
    m_scene->update();
}

3.遇到的需要注意的一些问题

  • 为什么有时候我的滑动没有能够及时刷新

QGraphicsItem内的一个函数必须要override正确

cpp 复制代码
QRectF boundingRect() const override;
  • 不需要单独的为每一个Item设置属性,比如是否被选中,可以直接用group去做操作
  • 放大缩小view的code
cpp 复制代码
qreal Widget::getScale()
{
    return syncScaledValue;
}

void Widget::setScale(qreal scaled)
{
    if (!qFuzzyCompare(scaled , this->syncScaledValue))
    {
        LOGD(QString("%1").arg(scaled));
        this->syncScaledValue = scaled;
        update();
    }
}

void Widget::setRelatedScale(qreal relatedScale)
{
    if (!qFuzzyCompare(relatedScale , this->relatedScaleValue))
    {
        relatedScaleValue = relatedScale;
        update();
    }
}

void Widget::resetRelatedScale()
{
    setRelatedScale(1.0);
}

void Widget::zoomIn()
{
    syncScaledValue = qBound(MIN_FACTOR , pow(1.2, 1) * syncScaledValue, MAX_FACTOR);
    update();
}
void Widget::zoomOut()
{
    syncScaledValue = qBound(MIN_FACTOR , pow(1.2, -1) * syncScaledValue, MAX_FACTOR);
    update();
}

void Widget::wheelEvent(QWheelEvent *e)
{
    qreal distance = (e->angleDelta() / 15 / 8).y();
    qreal val;
    if (distance != 0)
    {
        val = pow(1.2, distance);
        if (val < 2.0)
        {
            syncScaledValue *= val;
            syncScaledValue = qBound(MIN_FACTOR , syncScaledValue , MAX_FACTOR);
//            emit notifyScaledChanged(syncScaledValue, relatedScaleValue);
            update();
        }
    }
}
相关推荐
黑客老李19 分钟前
JavaSec | SpringAOP 链学习分析
java·运维·服务器·开发语言·学习·apache·memcached
开开心心就好27 分钟前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
特立独行的猫a33 分钟前
Nuxt.js 中的路由配置详解
开发语言·前端·javascript·路由·nuxt·nuxtjs
勤奋的知更鸟1 小时前
Java编程之原型模式
java·开发语言·原型模式
Unpredictable2221 小时前
【VINS-Mono算法深度解析:边缘化策略、初始化与关键技术】
c++·笔记·算法·ubuntu·计算机视觉
珂朵莉MM1 小时前
2021 RoboCom 世界机器人开发者大赛-高职组(初赛)解题报告 | 珂学家
java·开发语言·人工智能·算法·职场和发展·机器人
香蕉炒肉1 小时前
Java优化:双重for循环
java·开发语言
傍晚冰川2 小时前
FreeRTOS任务调度过程vTaskStartScheduler()&任务设计和划分
开发语言·笔记·stm32·单片机·嵌入式硬件·学习
PingdiGuo_guo2 小时前
C++智能指针的知识!
开发语言·c++
黄雪超2 小时前
JVM——打开JVM后门的钥匙:反射机制
java·开发语言·jvm