static void setAnchor(QGraphicsAnchorLayout *l, QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge, qreal spacing) { QGraphicsAnchor *anchor = l->addAnchor(firstItem, firstEdge, secondItem, secondEdge); anchor->setSpacing(spacing); }
static PyObject *meth_QGraphicsAnchor_spacing(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { QGraphicsAnchor *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QGraphicsAnchor, &sipCpp)) { qreal sipRes; Py_BEGIN_ALLOW_THREADS sipRes = sipCpp->spacing(); Py_END_ALLOW_THREADS return PyFloat_FromDouble(sipRes); } }
static PyObject *meth_QGraphicsAnchor_unsetSpacing(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { QGraphicsAnchor *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QGraphicsAnchor, &sipCpp)) { Py_BEGIN_ALLOW_THREADS sipCpp->unsetSpacing(); Py_END_ALLOW_THREADS Py_INCREF(Py_None); return Py_None; } } /* Raise an exception if the arguments couldn't be parsed. */ sipNoMethod(sipParseErr, sipName_QGraphicsAnchor, sipName_unsetSpacing, NULL); return NULL; }
void KWidget::setAnchor( const QString& a ) { Q_D(KWidget); //检查布局 KWidget* p = qobject_cast<KWidget*>(parent()); Q_ASSERT_X(p, __FUNCTION__, "parent is null"); Q_ASSERT_X(p->layoutType() == KWidget::Anchor, __FUNCTION__, "layout type should be anchor"); QGraphicsLayout *layout = p->layout(); QGraphicsAnchorLayout *anchorLayout = static_cast<QGraphicsAnchorLayout*>(layout); if(anchorLayout == NULL) { Q_ASSERT_X(false, __FUNCTION__, "layout is not group layout"); return ; } if(d->bAddToAnchor) { p->removeLayoutItem(this); } //参数转换 int index = KWidget::staticMetaObject.indexOfEnumerator("AnchorPoint"); QMetaEnum enu = KWidget::staticMetaObject.enumerator(index); QStringList list = a.split('|'); Q_ASSERT_X(list.count()==4, __FUNCTION__, QString("%1 is not valide anchor info").arg(a).toLatin1().constData()); Qt::AnchorPoint anchorPoint[4]={Qt::AnchorLeft}; int distance[4]={0}; for (int i = 0; i < list.count() && i < 4; ++i) { QStringList innerList = list[i].split(':'); Q_ASSERT_X(innerList.count()==2, __FUNCTION__, QString("%1 is not valide anchor info").arg(a).toLatin1().constData()); if (innerList.count() >= 2) { int value = enu.keyToValue(innerList[0].toLatin1().constData()); Q_ASSERT_X(value != -1, __FUNCTION__, (innerList[0] + "is not a valid enum value").toLatin1().constData()); if (value == -1) { value = 0; } anchorPoint[i] = (Qt::AnchorPoint)value; distance[i] = innerList[1].toInt(); } } //AnchorLeft: int idx = 0; QGraphicsAnchor *anchorLeft = anchorLayout->addAnchor(anchorLayout, anchorPoint[idx], this, Qt::AnchorLeft); if(anchorLeft) { anchorLeft->setSpacing(distance[idx]); } else if(anchorPoint[idx] >= Qt::AnchorTop) { Q_ASSERT_X(false, __FUNCTION__, QString("it's not the same orientation for AnchorLeft: bad string is[%1]").arg(list.at(idx)).toStdString().c_str()); } else { Q_ASSERT_X(false, __FUNCTION__, QString("bad parameter.bad string:%1").arg(list.at(idx)).toStdString().c_str()); } //AnchorTop: idx = 1; QGraphicsAnchor *anchorTop = anchorLayout->addAnchor(anchorLayout, anchorPoint[idx], this, Qt::AnchorTop); if(anchorTop) { anchorTop->setSpacing(distance[idx]); } else if(anchorPoint[idx] < Qt::AnchorTop) { Q_ASSERT_X(false, __FUNCTION__, QString("it's not the same orientation for AnchorTop: bad string is[%1]").arg(list.at(idx)).toStdString().c_str()); } else { Q_ASSERT_X(false, __FUNCTION__, QString("bad parameter.bad string:%1").arg(list.at(idx)).toStdString().c_str()); } //AnchorRight: idx = 2; QGraphicsAnchor *anchorRight = anchorLayout->addAnchor(this, Qt::AnchorRight, anchorLayout, anchorPoint[idx]); if(anchorRight) { anchorRight->setSpacing(distance[idx]); } else if(anchorPoint[idx] >= Qt::AnchorTop) { Q_ASSERT_X(false, __FUNCTION__, QString("it's not the same orientation for AnchorRight: bad string is[%1]").arg(list.at(idx)).toStdString().c_str()); } else { Q_ASSERT_X(false, __FUNCTION__, QString("bad parameter.bad string:%1").arg(list.at(idx)).toStdString().c_str()); } //AnchorBottom idx = 3; QGraphicsAnchor *anchorBottom = anchorLayout->addAnchor(this, Qt::AnchorBottom, anchorLayout, anchorPoint[idx]); if(anchorBottom) { anchorBottom->setSpacing(distance[idx]); } else if(anchorPoint[idx] < Qt::AnchorTop) { Q_ASSERT_X(false, __FUNCTION__, QString("it's not the same orientation for AnchorBottom: bad string is[%1]").arg(list.at(idx)).toStdString().c_str()); } else { Q_ASSERT_X(false, __FUNCTION__, QString("bad parameter.bad string:%1").arg(list.at(idx)).toStdString().c_str()); } d->bAddToAnchor = true; }