“QComboBox Pop-up” expanding and QtWebkit

2020-06-13 21:41发布

问题:

In Firefox/Chrome/InternetExplorer/Safari/Opera pop-ups from the combobox expand as the content, see Firefox picture:

QComboBox pop-up does not expand the content. Pop-ups are limited by the size of QComboBox, see QWebView picture:

So I implemented the QComboBox::showPopup:

void newQComboBox::showPopup() {
    int width = this->width();
    this->view()->setTextElideMode( Qt::ElideNone );

    const int iconSize = this->iconSize().width();
    const QFontMetrics fontMetrics = this->fontMetrics();
    const int j = this->count();

    for( int i=0; i < j; ++i ) {
        const int textWidth = fontMetrics.width( this->itemText(i) + "WWW" );
        if (this->itemIcon(i).isNull()) {
            width = qMax(width, textWidth);
        } else {
            width = qMax(width, textWidth + iconSize);
        }
    }

    QStyleOptionComboBox opt;
    this->initStyleOption(&opt);
    QSize size(width, 0);
    size = this->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, size, this);

    this->view()->setFixedWidth( width );

    QComboBox::showPopup();
}

Is there any way to modify (reimplement) the QComboBox::showPopup of QWebViews (QtWebkit)?

Qt-BUG (suggestion): https://bugreports.qt.io/browse/QTBUG-35771

回答1:

I solved using QProxyStyle class, example:

#include <QProxyStyle>
#include <QAbstractItemView>
#include <QComboBox>

class myProxyStyle : public QProxyStyle
{

public:
    int styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
    {
        if(hint == QStyle::SH_ComboBox_Popup) {
            const QComboBox *combo = (QComboBox *) widget;//cast to QComboBox
            const QObjectList a = combo->children();
            const int j = a.count();
            QAbstractItemView *view = 0;
            QString className = "";
            bool hasView = false;

            /*
            at this point I have not used combo->view() because he "crash" the application without explanation
            so I had to make a loop to find the "view"
            */
            for (int i = 0; i < j; ++i) {
                const QObjectList b = a.at(i)->children();
                const int y = b.count();

                for (int x = 0; x < y; ++x) {
                    className = b.at(x)->metaObject()->className();

                    if (className == "QComboBoxListView") {
                        view = (QAbstractItemView *) b.at(x);
                        hasView = true;
                        break;
                    }
                }

                if (hasView) {
                    break;
                }
            }

            if (hasView) {
                const int iconSize = combo->iconSize().width();
                const QFontMetrics fontMetrics1 = view->fontMetrics();
                const QFontMetrics fontMetrics2 = combo->fontMetrics();
                const int j = combo->count();
                int width = combo->width(); //default width

                for (int i = 0; i < j; ++i) {
                    const int textWidth = qMax(
                        fontMetrics1.width(combo->itemText(i) + "WW"),
                        fontMetrics2.width(combo->itemText(i) + "WW")
                    );

                    if(combo->itemIcon(i).isNull()) {
                        width = qMax(width, textWidth);
                    } else {
                        width = qMax(width, textWidth + iconSize);
                    }
                }

                view->setFixedWidth(width);
            }
        }

        return QProxyStyle::styleHint(hint, option, widget, returnData);
    }
};

how to use:

You must apply in QApplication (usually file main.cpp):

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setStyle(new myProxyStyle);//set ProxyStyle

    return a.exec();
}

@peppe thanks to your comment I got the solution



回答2:

Please try to make use of the setMinimumWidth function and pass the max width you have calculated. it should work ui.comboBox->view()->setMinimumWidth(width); for more detail please go through https://qt-project.org/forums/viewthread/26388 link