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

How to uniformize a QFrame derived widget style with the QLineEdit style?



  • I incorporated a widget that I got the source code from https://stackoverflow.com/a/11358560/3174651

    It is a derived class from QFrame.

    It works but it is not 100% correct, imho but the point is it has a slightly different appearance that the 2 other QLineEdit widgets in the dialog.

    For one, the corners are square vs rounded corners for QLineEdit.

    and secondly, the focus border color is different:
    IPCtrl focus appearance:
    IPCtrl focus appearance

    QLineEdit focus appearance:
    QLineEdit focus appearance

    How do I change IPCtrl so that its frame is identical to QLineEdit widgets?

    As a sidenote, the IPCtrl focus frame is something that I added with:

        else if (event->type() == QEvent::FocusIn) {
            m_pFocusFrame->setWidget(this);
        }
        else if (event->type() == QEvent::FocusOut) {
            m_pFocusFrame->setWidget(nullptr);
        }
    

    in its eventFilter(QObject *obj, QEvent *event) function.


  • Lifetime Qt Champion

    @lano1106 said in How to uniformize a QFrame derived widget style with the QLineEdit style?:

    How do I change IPCtrl so that its frame is identical to QLineEdit widgets?

    You should check its implementation: does it use style-sheets?



  • If you want the frame looks exatly like a QLineEdit, then don't use something like focus frame, just paint with the same PrimitiveElement as QLineEdit.

    class IPCtrl : public QFrame
    {
        Q_OBJECT
    ......
    protected:
        void paintEvent(QPaintEvent *);
    ......
    };
    
    void IPCtrl::paintEvent(QPaintEvent *)
    {
        QPainter p(this);
        QStyleOptionFrame option;
        initStyleOption(&option);
        //if (isReadOnly())
        //    option->state |= QStyle::State_ReadOnly;
        for ( int i = 0; i < QTUTL_IP_SIZE; ++i )
        {
            QLineEdit* pEdit = m_pLineEdit[i];
    #ifdef QT_KEYPAD_NAVIGATION
            if(pEdit->hasEditFocus()) {
                option.state |= QStyle::State_HasEditFocus;
            }
    #endif
            if(pEdit->hasFocus()) {
                option.state |= QStyle::State_HasFocus;
            }
        }
        style()->drawPrimitive(QStyle::PE_PanelLineEdit, &option, &p, this);
    }
    

    Other changes that need to be made from the code you posted:

    IPCtrl::IPCtrl(QWidget *parent) : QFrame(parent)
    {
    ......
        pLayout->setContentsMargins( 1, 1, 1, 1 );//if margin = 0 the frame would be blocked a bit
    ......
    }
    
    bool IPCtrl::eventFilter(QObject *obj, QEvent *event)
    {
    ......
        if ( event->type() == QEvent::KeyPress )
        {
    ......
        } else if(event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) {
                update(); //update the frame when the lineEdits have FocusIn/Out events
        }
    
        return bRes;
    }
    

Log in to reply