PySide获取QSpinBox中箭头按钮的宽度

时间:2019-01-20 22:06:54

标签: python pyside qspinbox

是否可以确定qspinbox中箭头按钮的宽度? 我正在尝试覆盖上下文菜单事件,并且只希望在用户右键单击箭头按钮时发生我的自定义事件,否则我希望显示普通的上下文菜单。

现在我只是硬编码20的值,这不理想。

XMVECTOR pt1;  // A
XMVECTOR pt2;  // B
XMVECTOR pt3;  // C

pt1 = XMLoadFloat3((const XMFLOAT3*) &lpPoints[0]);
pt2 = XMLoadFloat3((const XMFLOAT3*) &lpPoints[1]);
pt3 = XMLoadFloat3((const XMFLOAT3*) &lpPoints[2]);

TRACE("A = [  %g    %g    %g  ]  \n", XMVectorGetX(pt1), XMVectorGetY(pt1), XMVectorGetZ(pt1));
TRACE("B = [  %g    %g    %g  ]  \n", XMVectorGetX(pt2), XMVectorGetY(pt2), XMVectorGetZ(pt2));
TRACE("C = [  %g    %g    %g  ]  \n", XMVectorGetX(pt3), XMVectorGetY(pt3), XMVectorGetZ(pt3));

XMVECTOR AB = XMVectorSubtract(pt2, pt1);
TRACE("AB = [  %g    %g    %g  ]  \n", XMVectorGetX(AB), XMVectorGetY(AB), XMVectorGetZ(AB));

XMVECTOR AC = XMVectorSubtract(pt3, pt1);
TRACE("AC = [  %g    %g    %g  ]  \n", XMVectorGetX(AC), XMVectorGetY(AC), XMVectorGetZ(AC));

XMVECTOR N = XMVector3Cross(AB, AC);
XMVECTOR Nnormal = XMVector3Normalize(N);
XMVECTOR ABnormal = XMVector3Normalize(AB);
XMVECTOR V = XMVector3Cross(ABnormal, Nnormal);

TRACE("Nnormalized =  [  %g    %g    %g  ]  \n", XMVectorGetX(Nnormal), XMVectorGetY(Nnormal), XMVectorGetZ(Nnormal));
TRACE("ABnormalized = [  %g    %g    %g  ]  \n", XMVectorGetX(ABnormal), XMVectorGetY(ABnormal), XMVectorGetZ(ABnormal));
TRACE("V=AB cross N = [  %g    %g    %g  ]  \n\n", XMVectorGetX(V), XMVectorGetY(V), XMVectorGetZ(V));

XMMATRIX D;
D._11 = 0;
D._12 = 1.0F;
D._13 = 0;
D._14 = 0;
D._21 = 0;
D._22 = 0;
D._23 = 1.0F;
D._24 = 0;
D._31 = 0;
D._32 = 0;
D._33 = 0;
D._34 = 1.0F;
D._41 = 1.0F;
D._42 = 1.0F;
D._43 = 1.0F;
D._44 = 1.0F;

XMMATRIX S;
S._11 = XMVectorGetX(pt1);
S._12 = XMVectorGetX(pt1) + XMVectorGetX(ABnormal);
S._13 = XMVectorGetX(pt1) + XMVectorGetX(V);
S._14 = XMVectorGetX(pt1) + XMVectorGetX(Nnormal);
S._21 = XMVectorGetY(pt1);
S._22 = XMVectorGetY(pt1) + XMVectorGetY(ABnormal);
S._23 = XMVectorGetY(pt1) + XMVectorGetY(V);
S._24 = XMVectorGetY(pt1) + XMVectorGetY(Nnormal);
S._31 = XMVectorGetZ(pt1);
S._32 = XMVectorGetZ(pt1) + XMVectorGetZ(ABnormal);
S._33 = XMVectorGetZ(pt1) + XMVectorGetZ(V);
S._34 = XMVectorGetZ(pt1) + XMVectorGetZ(Nnormal);
S._41 = 1.0F;
S._42 = 1.0F;
S._43 = 1.0F;
S._44 = 1.0F;

TRACE("S           =  [  %g    %g    %g    %g  ]  \n", S._11, S._12, S._13, S._14);
TRACE("               [  %g    %g    %g    %g  ]  \n", S._21, S._22, S._23, S._24);
TRACE("               [  %g    %g    %g    %g  ]  \n", S._31, S._32, S._33, S._34);
TRACE("               [  %g    %g    %g    %g  ]  \n\n", S._41, S._42, S._43, S._44);

TRACE("D           =  [  %g    %g    %g    %g  ]  \n", D._11, D._12, D._13, D._14);
TRACE("               [  %g    %g    %g    %g  ]  \n", D._21, D._22, D._23, D._24);
TRACE("               [  %g    %g    %g    %g  ]  \n", D._31, D._32, D._33, D._34);
TRACE("               [  %g    %g    %g    %g  ]  \n\n", D._41, D._42, D._43, D._44);

XMMATRIX Sinv;
XMVECTOR det;
Sinv = XMMatrixInverse(&det, S);

TRACE("Sinv        =  [  %g    %g    %g    %g  ]  \n", Sinv._11, Sinv._12, Sinv._13, Sinv._14);
TRACE("               [  %g    %g    %g    %g  ]  \n", Sinv._21, Sinv._22, Sinv._23, Sinv._24);
TRACE("               [  %g    %g    %g    %g  ]  \n", Sinv._31, Sinv._32, Sinv._33, Sinv._34);
TRACE("               [  %g    %g    %g    %g  ]  \n\n", Sinv._41, Sinv._42, Sinv._43, Sinv._44);

XMMATRIX M;
M = XMMatrixMultiply(D, Sinv); 

TRACE("M           =  [  %g    %g    %g    %g  ]  \n", M._11, M._12, M._13, M._14);
TRACE("               [  %g    %g    %g    %g  ]  \n", M._21, M._22, M._23, M._24);
TRACE("               [  %g    %g    %g    %g  ]  \n", M._31, M._32, M._33, M._34);
TRACE("               [  %g    %g    %g    %g  ]  \n\n", M._41, M._42, M._43, M._44);

M = XMMatrixTranspose(M);  // DxMath, without this XMVector4Transform below won't work

XMMATRIX Minverse;
Minverse = XMMatrixInverse(&det, M);

list<MyPoly> testpolys;

MyPoly polystwod;
polystwod.Init(nPoints);
XMVECTOR twod;
for (int p = 0; p < nPoints; p++)
{
    XMVECTOR pt = XMLoadFloat3((const XMFLOAT3*)&lpPoints[p]);
    pt = XMVectorSetW(pt, 1.0F);
    twod = XMVector4Transform(pt, M);
    TRACE("Initial   %g   %g    %g    ", XMVectorGetX(pt), XMVectorGetY(pt), XMVectorGetZ(pt));
    TRACE("Initial * M =  %g   %g    %g \n", XMVectorGetX(twod), XMVectorGetY(twod), XMVectorGetZ(twod));
    // pass 2D data
    polystwod[p].x = XMVectorGetX(twod);
    polystwod[p].y = XMVectorGetY(twod);

}
testpolys.push_back(polystwod);

// pass to your 2d triangulation routine
// then back solve 2d points into the original 3d coordinate system

XMVECTOR threed;

list<MyPoly>::iterator iter;
for (iter = testpolys.begin(); iter != testpolys.end(); iter++)
{
    MyPoly w;
    w = *iter;
    ThreeFloats w3;
    int i, cntpoints;
    cntpoints = w.GetSize();
    for (i = 0; i < cntpoints; i++)
    {
        w3.X = w[i].x;  // initialize a 2d point 
        w3.Y = w[i].y;
        w3.Z = 0.0F;
        XMVECTOR pt = XMLoadFloat3((const XMFLOAT3*)&w3);  pt = XMVectorSetW(pt, 1.0F);
        threed = XMVector4Transform(pt, Minverse);
        TRACE("Back Solving Back    %g   %g    %g    ", w3.X, w3.Y, w3.Z);
        TRACE("Back * Minverse =    %g   %g    %g   \n", XMVectorGetX(threed), XMVectorGetY(threed), XMVectorGetZ(threed));
        // variable threed is now back in original 3d coordinate system 
    }
}


IMMEDIATE WINDOW OUTPUT



A = [  0.3    -0.45    0.5  ]  
B = [  0.1    -0.45    0.5  ]  
C = [  -0.1    0.15    0.5  ]  
AB = [  -0.2    0    0  ]  
AC = [  -0.4    0.6    0  ]  
Nnormalized =  [  0    0    -1  ]  
ABnormalized = [  -1    0    0  ]  
V=AB cross N = [  -0    -1    -0  ]  

S           =  [  0.3    -0.7    0.3    0.3  ]  
               [  -0.45    -0.45    -1.45    -0.45  ]  
               [  0.5    0.5    0.5    -0.5  ]  
               [  1    1    1    1  ]  

D           =  [  0    1    0    0  ]  
               [  0    0    1    0  ]  
               [  0    0    0    1  ]  
               [  1    1    1    1  ]  

Sinv        =  [  1    1    1    0.65  ]  
               [  -1    0    0    0.3  ]  
               [  0    -1    0    -0.45  ]  
               [  0    0    -1    0.5  ]  

M           =  [  -1    0    0    0.3  ]  
               [  0    -1    0    -0.45  ]  
               [  0    0    -1    0.5  ]  
               [  0    0    0    1  ]  

Initial   0.3   -0.45    0.5    Initial * M =  0   0    0 
Initial   0.1   -0.45    0.5    Initial * M =  0.2   0    0 
Initial   -0.1   0.15    0.5    Initial * M =  0.4   -0.6    0 
Initial   0.1   0.35    0.5    Initial * M =  0.2   -0.8    0 
Initial   0.3   0.35    0.5    Initial * M =  0   -0.8    0 
Back Solving Back    0   0    0    Back * Minverse =    0.3   -0.45    0.5   
Back Solving Back    0.2   0    0    Back * Minverse =    0.1   -0.45    0.5   
Back Solving Back    0.4   -0.6    0    Back * Minverse =    -0.1   0.15    0.5   
Back Solving Back    0.2   -0.8    0    Back * Minverse =    0.1   0.35    0.5   
Back Solving Back    0   -0.8    0    Back * Minverse =    0.3   0.35    0.5   



SWAPPING Z AND X  



A = [  0.5    -0.45    0.3  ]  
B = [  0.5    -0.45    0.1  ]  
C = [  0.5    0.15    -0.1  ]  
AB = [  0    0    -0.2  ]  
AC = [  0    0.6    -0.4  ]  
Nnormalized =  [  1    0    0  ]  
ABnormalized = [  0    0    -1  ]  
V=AB cross N = [  0    -1    0  ]  

S           =  [  0.5    0.5    0.5    1.5  ]  
               [  -0.45    -0.45    -1.45    -0.45  ]  
               [  0.3    -0.7    0.3    0.3  ]  
               [  1    1    1    1  ]  

D           =  [  0    1    0    0  ]  
               [  0    0    1    0  ]  
               [  0    0    0    1  ]  
               [  1    1    1    1  ]  

Sinv        =  [  -1    1    1    1.65  ]  
               [  0    0    -1    0.3  ]  
               [  0    -1    0    -0.45  ]  
               [  1    0    -0    -0.5  ]  

M           =  [  0    0    -1    0.3  ]  
               [  0    -1    0    -0.45  ]  
               [  1    0    0    -0.5  ]  
               [  0    0    0    1  ]  

Initial   0.5   -0.45    0.3    Initial * M =  0   2.98023e-008    0 
Initial   0.5   -0.45    0.1    Initial * M =  0.2   2.98023e-008    0 
Initial   0.5   0.15    -0.1    Initial * M =  0.4   -0.6    0 
Initial   0.5   0.35    0.1    Initial * M =  0.2   -0.8    0 
Initial   0.5   0.35    0.3    Initial * M =  0   -0.8    0 
Back Solving Back    0   2.98023e-008    0    Back * Minverse =    0.5   -0.45    0.3   
Back Solving Back    0.2   2.98023e-008    0    Back * Minverse =    0.5   -0.45    0.1   
Back Solving Back    0.4   -0.6    0    Back * Minverse =    0.5   0.15    -0.1   
Back Solving Back    0.2   -0.8    0    Back * Minverse =    0.5   0.35    0.1   
Back Solving Back    0   -0.8    0    Back * Minverse =    0.5   0.35    0.3   



ANOTHER TEST CONSTANT Y



A = [  -0.9    0.25    0.3  ]  
B = [  -0.9    0.25    0.1  ]  
C = [  0.3    0.25    -0.1  ]  
AB = [  0    0    -0.2  ]  
AC = [  1.2    0    -0.4  ]  
Nnormalized =  [  0    -1    0  ]  
ABnormalized = [  0    0    -1  ]  
V=AB cross N = [  -1    -0    -0  ]  

S           =  [  -0.9    -0.9    -1.9    -0.9  ]  
               [  0.25    0.25    0.25    -0.75  ]  
               [  0.3    -0.7    0.3    0.3  ]  
               [  1    1    1    1  ]  

D           =  [  0    1    0    0  ]  
               [  0    0    1    0  ]  
               [  0    0    0    1  ]  
               [  1    1    1    1  ]  

Sinv        =  [  1    1    1    1.35  ]  
               [  0    2.98023e-008    -1    0.3  ]  
               [  -1    0    0    -0.9  ]  
               [  0    -1    0    0.25  ]  

M           =  [  0    2.98023e-008    -1    0.3  ]  
               [  -1    0    0    -0.9  ]  
               [  0    -1    0    0.25  ]  
               [  0    0    0    1  ]  

Initial   -0.9   0.25    0.3    Initial * M =  -2.23517e-008   0    0 
Initial   -0.9   0.25    0.1    Initial * M =  0.2   0    0 
Initial   0.3   0.25    -0.1    Initial * M =  0.4   -1.2    0 
Initial   0.7   0.25    0.1    Initial * M =  0.2   -1.6    0 
Initial   0.7   0.25    0.3    Initial * M =  -2.23517e-008   -1.6    0 
Back Solving Back    -2.23517e-008   0    0    Back * Minverse =    -0.9   0.25    0.3   
Back Solving Back    0.2   0    0    Back * Minverse =    -0.9   0.25    0.1   
Back Solving Back    0.4   -1.2    0    Back * Minverse =    0.3   0.25    -0.1   
Back Solving Back    0.2   -1.6    0    Back * Minverse =    0.7   0.25    0.1   
Back Solving Back    -2.23517e-008   -1.6    0    Back * Minverse =    0.7   0.25    0.3   



ONE FINAL TEST



A = [  1    1    1  ]  
B = [  2    1    1  ]  
C = [  1    1    2  ]  
AB = [  1    0    0  ]  
AC = [  0    0    1  ]  
Nnormalized =  [  0    -1    0  ]  
ABnormalized = [  1    0    0  ]  
V=AB cross N = [  0    0    -1  ]  

S           =  [  1    2    1    1  ]  
               [  1    1    1    0  ]  
               [  1    1    0    1  ]  
               [  1    1    1    1  ]  

D           =  [  0    1    0    0  ]  
               [  0    0    1    0  ]  
               [  0    0    0    1  ]  
               [  1    1    1    1  ]  

Sinv        =  [  -1    1    1    0  ]  
               [  1    0    0    -1  ]  
               [  0    0    -1    1  ]  
               [  0    -1    0    1  ]  

M           =  [  1    0    0    -1  ]  
               [  0    0    -1    1  ]  
               [  0    -1    0    1  ]  
               [  0    0    0    1  ]  

Initial   1   1    1    Initial * M =  0   0    0 
Initial   2   1    1    Initial * M =  1   0    0 
Initial   1   1    2    Initial * M =  0   -1    0 
Initial   -2   1    7    Initial * M =  -3   -6    0 
Back Solving Back    0   0    0    Back * Minverse =    1   1    1   
Back Solving Back    1   0    0    Back * Minverse =    2   1    1   
Back Solving Back    0   -1    0    Back * Minverse =    1   1    2   
Back Solving Back    -3   -6    0    Back * Minverse =    -2   1    7   

1 个答案:

答案 0 :(得分:1)

代替获取宽度,仅需要获取SubControl即可知道是否已按下箭头按钮之一:

def contextMenuEvent(self, event):
    opt = QtGui.QStyleOptionSpinBox()
    self.initStyleOption(opt)
    opt.subControls = QtGui.QStyle.SC_All
    hoverControl = self.style().hitTestComplexControl(QtGui.QStyle.CC_SpinBox, opt, event.pos(), self)
    if hoverControl == QtGui.QStyle.SC_SpinBoxUp:
        print("up")
    elif hoverControl == QtGui.QStyle.SC_SpinBoxDown:
        print("down")
    else:
        super(self.__class__, self).contextMenuEvent(event)

如果要获取每个子控件的QRect,则应使用

# up
rect_up = self.style().subControlRect(QtGui.QStyle.CC_SpinBox, opt, QtGui.QStyle.SC_SpinBoxUp, self)
# down
rect_down = self.style().subControlRect(QtGui.QStyle.CC_SpinBox, opt, QtGui.QStyle.SC_SpinBoxDown, self)

另一个选择:

def contextMenuEvent(self, event):
    opt = QtGui.QStyleOptionSpinBox()
    self.initStyleOption(opt)
    r = QtCore.QRect()
    for sc in (QtGui.QStyle.SC_SpinBoxUp, QtGui.QStyle.SC_SpinBoxDown):
        r= r.united(self.style().subControlRect(QtGui.QStyle.CC_SpinBox, opt, sc, self))
    if r.contains(event.pos()):
        print("arrow")
    else:
        super(self.__class__, self).contextMenuEvent(event)