creator 绘制框

//windous 鼠标拖动绘制框

const { ccclass, property } = cc._decorator;

@ccclass

export default class drawNode extends cc.Component {

   

    graphics : cc.Graphics = null;

    touchStartPos : cc.Vec2 = null;

    touchEndPos : cc.Vec2 = null;

    onLoad() {

        this.graphics = this.node.getComponent(cc.Graphics);

        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);

        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);

        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);

        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);

    }

    onTouchStart(EventTouch ){

        this.graphics.clear()

        let touch : cc.Touch  = EventTouch.currentTouch;

        this.touchStartPos = touch.getLocation();

    }

    onTouchMove(EventTouch){

        let touch : cc.Touch  = EventTouch.currentTouch;

        let endPos = touch.getLocation()

        this.drawRect(endPos)

    }

    onTouchEnd(){

        this.graphics.clear()

    }

    onTouchCancel(){

        this.touchStartPos = null;

        this.touchEndPos = null;

        this.graphics.clear()

    }

    drawRect(endPos : cc.Vec2){

        this.touchEndPos = endPos;

        this.graphics.clear()

        this.graphics.rect(this.touchStartPos.x , this.touchStartPos.y , endPos.x - this.touchStartPos.x , endPos.y - this.touchStartPos.y )

        this.graphics.stroke();

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Qt Creator中TreeView的自动换行,可以通过自定义ItemDelegate来实现。以下是一个简单的示例: 1. 在Qt Creator中打开.ui文件,找到TreeView部件,右键选择“添加Delegate”。 2. 在弹出的“添加Delegate”对话框中,选择“继承QStyledItemDelegate”,并点击确定。 3. 在生成的Delegate的头文件中添加以下代码: ```c++ class WrappedTextDelegate : public QStyledItemDelegate { public: WrappedTextDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {} void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { // 获取项数据 QString text = index.data(Qt::DisplayRole).toString(); // 获取项矩形框 QRect rect = option.rect; // 计算文本矩形框 QRect text_rect = painter->boundingRect(rect, Qt::TextWordWrap, text); // 如果文本超过项矩形框,则调整项矩形框的高度 if (text_rect.height() > rect.height()) { rect.setHeight(text_rect.height()); } // 绘制文本 painter->drawText(rect, Qt::TextWordWrap, text); } QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override { // 获取项数据 QString text = index.data(Qt::DisplayRole).toString(); // 获取项矩形框 QRect rect = option.rect; // 计算文本矩形框 QRect text_rect = QFontMetrics(option.font).boundingRect(rect, Qt::TextWordWrap, text); // 如果文本超过项矩形框,则调整项矩形框的高度 if (text_rect.height() > rect.height()) { rect.setHeight(text_rect.height()); } return rect.size(); } }; ``` 4. 在TreeView的构造函数中设置ItemDelegate: ```c++ ui->treeView->setItemDelegate(new WrappedTextDelegate(ui->treeView)); ``` 5. 运行程序,即可看到TreeView中的文本自动换行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值