[SOLVED] Qt stylesheets: border on a QGLWidget



  • Hi,

    I would like to add a border to a QGLWidget derived class when it has the focus. I am using the following code:

    @
    void myGLWidget::focusInEvent(QFocusEvent* _event)
    {
    QGLWidget::focusInEvent(event);
    setStyleSheet("border: solid 5px red");
    }

    void myGLWidget::focusOutEvent(QFocusEvent* _event)
    {
    QGLWidget::focusOutEvent(event);
    setStyleSheet("border: none;");
    }
    @

    As a result, there is no border drawn when the widget takes focus (I added console outputs inside the methods to check if I pass through them). Does anybody know what I am doing wrong?



  • Since the QGLWidget does not use QStyle the style sheet does not work.
    I would suggest putting your QGWidget into a normal QWidget and set the style sheet on that one.
    You can use event filters to get notified about the focus events.



  • Well, thanks for the answer. I solved the problem using another way. I used a QPainter to draw the border inside glDraw():

    @
    myGLWidget::myGLWidget()
    {
    // prevent the widget from being cleared when beginning the QPainter
    setAutoFillBackground(false);

    setAutoBufferSwap(false);
    

    }

    void myGLWidget::glDraw()
    {
    QGLWidget::glDraw();

    if(hasFocus())
    {
        QPainter painter(this);
    
        QPen pen(Qt::yellow, 4);
        painter.setPen(pen);
    
        painter.drawRect(0, 0, width(), height());
    }
    
    swapBuffers();
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.