【问题标题】:How to make the qgraphicsitem movable/not-movable dynamically?如何使 qgraphicsitem 动态移动/不可移动?
【发布时间】:2014-10-17 21:49:59
【问题描述】:

我有很多 qgraphicsitem,我想使用一个复选框来设置这些 qgraphicsitem 是否可移动。 我现在唯一能想到的就是对每个 qgraphicsitem 使用 setFlag(ItemIsMovable) 并使用来自复选框的信号来触发它。 有没有更好更有效的方法来做到这一点?

【问题讨论】:

    标签: qt qgraphicsitem


    【解决方案1】:

    如果我理解正确的话,每个QGraphicsItem 都有特别独特的QComboBox

    所以当你有很多项目时,你可以QSignalMapper 或尝试类似下一个(有很多方法可以做到这一点,我用小代码举例):

    QList<QPair<QCheckBox*,QGraphicsItem*> > pair;
    //fill your list with all QCheckBox and QGraphicsItem pointers which you need
    
    //do connection for each QCheckBox
    for(int i = 0; i <pair.size(); i++)
    {
        pair.at(i).first->setObjectName(QString::number(i));//it is a trick, objectName is our position in list
        connect(pair.at(i).first,SIGNAL(stateChanged(int)),SLOT(myslot()));
    }
    

    在我的插槽中:

    int index = sender()->objectName().toInt();
    if(pair.at(index).first->isChecked())
       pair.at(index).second->setFlags(QGraphicsItem::ItemIsMovable);//movable
    else
        pair.at(index).second->setFlags(pair.at(index).second->flags() & ~QGraphicsItem::ItemIsMovable );//not movable
    

    最重要的是,您不应该手动编写每个连接等,您可以在程序中执行此操作。你可以做一些稍微不同的事情,但我向你展示了如何用大约 10 行来做到这一点,这是可能的。

    【讨论】:

      猜你喜欢
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 2012-06-19
      • 2021-08-03
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多