Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to uniformize a QFrame derived widget style with the QLineEdit style?

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

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 261 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lano1106
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • L lano1106

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #3

        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;
        }
        
        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved