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. [SOLVED] Qt stylesheets: border on a QGLWidget
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Qt stylesheets: border on a QGLWidget

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.3k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    tilsitt
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • frederikF Offline
      frederikF Offline
      frederik
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tilsitt
        wrote on last edited by
        #3

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

        }
        @

        1 Reply Last reply
        0

        • Login

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