My blog has been moved to ariya.ofilabs.com.

Wednesday, December 14, 2005

Arrow in Plastique QComboBox

If you try to find the dimension and position of the combobox arrow button, one of your chance is by using subControlRect of the style in use. Unfortunately, this can not work in Plastique style because this style does not return a correct QRect associated with the arrow's geometry, as evidence from the source code:

    case CC_ComboBox:
        switch (subControl) {
        case SC_ComboBoxArrow:
            rect = option->rect;
            break;

It simply gives back option->rect which is likely the widget's own rect(). Your program might believe that the arrow occupies the whole combobox.

Fortunately, thanks to the Trolls, this problem is gone with the upcoming Qt 4.1.

But just in case you still want to work with Qt 4.0 (and 4.0.1), then you have to implement hackish fix like this:

    QRect arrowRect = style()->subControlRect(QStyle::CC_ComboBox, 
        &opt, QStyle::SC_ComboBoxArrow, this);
    if( arrowRect == opt.rect )
    {
        // workaround for broken subControlRect
        int fw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
        arrowRect = QRect(width()-16-fw, height()-2*fw);
    }

2 comments:

Tim Whitbeck said...

What a godsend! I'm currently trying to make a style based on Plastique for Qt 4.0.1 and I was going nuts trynig to figure out this combo box. Thanks so much for the tip!

Ariya Hidayat said...

tim whitbeck: good to know that it is useful to you!