Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] QComboBox and QtWebkit



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

    !http://i.stack.imgur.com/VOn9w.png(firefox combobox)!

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

    !http://i.stack.imgur.com/dnqQA.png(QT5 qcombobox)!

    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();
    }@
    

    Now this will only serve a custom QComboBox.

    I would like to know if you have to modify the QComboBox::showPopup used by QtWebkit.


  • Moderators

    if i understood you right you are looking for QComboBox::setSizeAdjustPolicy(QComboBox::AdjustToContents) ?



  • afaik to expand item view with qss you can use:

    @QComboBox QAbstractItemView
    {
    min-width:300px;
    }@

    though it would not fit the max length of one item's text; to do this you shoud handle it in code

    since the textElide is part of QStyleOption for painting with QStyle it cannot be changed by qss; so again you should set it in code ...



  • raven-worx it did not work.

    I suppose you've mistaken, QComboBox::setSizeAdjustPolicy sets the combobox and not the pop-up (listView).

    I need to adjust the pop-up.


    NicuPopescu I try this:

    @QListView {
    min-width:400px;
    }@

    But not quite what I want, I could help?


  • Moderators

    ok then you can do this:
    @
    void MyComboBox::showPopup()
    {
    int width = 100; //specify minimum width
    const int iconSize = this->iconSize().width() + 4;
    const QFontMetrics fontMetrics = this->fontMetrics();

    for( int i=0; i < this->count(); ++i )
    {
        const int textWidth = fontMetrics.width(this->itemText(i));
        if (this->itemIcon(i).isNull())
            width = (qMax(width, textWidth));
        else
            width = (qMax(width, textWidth + iconWidth));
    }
    
    QStyleOptionComboBox opt;
    this->initStyleOption(&opt);
    QSize size(width, 0);
    size = this->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, size, this);
    
    this->view()->setFixedWidth( qMax(this->width(),size.width()) );
    
    QComboBox::showPopup();
    

    }
    @



  • great solution.

    Tell me one thing in the case of webviews, it would be possible to implement something like QComboBox (the QWebView widgets uses QT itself)?

    Or will I have to modify and recompile "QT-sdk-open-source"?


  • Moderators

    AFAIK the "widgets" inside a QWebView are not drawn by Qt itself but by the webkit engine directly. For example like the scrollbars.
    So only JS and CSS can be used to change the appearance of web elements inside a QWebView.



  • raven-worx not all elements are of webkit, in the case of combobox (pop-up) uses the QT QComboBox.

    So much so that it is possible to style the combobox the "QWebView" used this:

    @QComboBox{
    ...
    }@

    See alternative solution: http://stackoverflow.com/a/17107731/1518921

    But this solution is not as good as what you did. I wanted to use your code to modify the "QComboBox" the "QWebView"

    The only solution I see is to download the source, modify the code and recompile QT-sdk.

    Could you tell me if there is another way? Thanks!



  • I update my question.




Log in to reply