-1

I have a grid layout as(vtkOpenGLWidget) and inside I distribute the layout in four section. So, I want to highlight the view port when i will clicking on the it. This is my grid layout image view :

enter image description here

I will use this code for highlighting the view port code:

void QvtkOpenGLWidgetdrag::paintEvent(QPaintEvent *event)
{
    QOpenGLWidget::paintEvent(event);

    QPainter painter(this);

    painter.setPen(QPen(QBrush(Qt::blue), 2));

    if (hasFocus())
        painter.drawRect(QRect(1, 1, width() - 2, height() - 2));
}

Its work fine, when i will clicked on the viewport. But i am getting issue when i will select any other module inside the Ui-viewer then it wil by default shows unselected. But, I have selected everytime and once i will select to the other viewport then only it unselect and select to the next view port and vice versa.

Any help is considerable for me. Thank you in advance

Amar Kumar
  • 45
  • 10
  • **Do not** call `paintEvent` manually, e.g. by connecting it to the clicked signal. – scopchanov Sep 12 '18 at 11:37
  • Please, take this code *after* `EDIT`, and post another question, as it introduces a completely different issue. Also put a link the my answer and the exception from your comment. – scopchanov Sep 12 '18 at 11:52

1 Answers1

2

Note: Your question is not very clear, so I will try to guess the right solution for you.

Cause

The focus policy of QOpenGLWidget is set to Qt::NoFocus by default. Furthermore, this widget does not respect the stylesheet.

Solution

Subclass QOpenGLWidget, change the focus policy to setFocusPolicy(Qt::StrongFocus); and reimplement the paintEvent like this:

void OpenGLWidget::paintEvent(QPaintEvent *event)
{
    QOpenGLWidget::paintEvent(event);

    QPainter painter(this);

    painter.setPen(QPen(QBrush(Qt::magenta), 2));

    if (hasFocus())
        painter.drawRect(QRect(1, 1, width() - 2, height() - 2));
}

Example

In order to demonstrate how to implement that in an application I have prepared a working example for you. The code is available on GitHub.

Result

This will give you a nice purple border at the edge of the clicked view:

enter image description here

scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • All the OpenGLWidget available inside the gridLayout. Still, I will be highlight like this way. – Amar Kumar Aug 28 '18 at 05:33
  • @AmarKumar, what do you mean by _All the OpenGLWidget available inside the gridLayout_? – scopchanov Aug 28 '18 at 06:40
  • it means grid layout by default take the whole size of OpenGLWidget. when I call (1*1) layout. So, border is created inside the layout or outside the layout – Amar Kumar Aug 28 '18 at 07:30
  • @AmarKumar, I understand now. So, this approach puts the frame inside the widget. – scopchanov Aug 28 '18 at 07:38
  • I am getting this one error when i used this function inside the mainwindow.cpp, Unhandled exception at 0x00007FF645A9D53F in RadSpaEngine.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000C5A3C03FB0). – Amar Kumar Sep 12 '18 at 06:45
  • @AmarKumar, the proper thing to do to get help with your problem is not to unaccept the answer, blaming the provided code for causing the given exception, as it was tested and works as expected, but to post another question, in which you show **how you use this code in your program**. Then and only then would someone be able to tell you what is the reason for the crash and how to solve it. – scopchanov Sep 12 '18 at 11:13
  • Everything i am using fine, I will declare void paintEvent(QPaintEvent *event) inside the QOpenGLWidget class. and try to run this code but lastly i am getting Exception. If you check it once then it will be very usefull for me. – Amar Kumar Sep 12 '18 at 11:23
  • @AmarKumar, I have added the example. Check it out. – scopchanov Sep 12 '18 at 11:34
  • Can i added your given function inside the mainwindow.cpp – Amar Kumar Sep 12 '18 at 13:03
  • @AmarKumar, if I understand you correctly, you have to add the class, not the function. – scopchanov Sep 12 '18 at 13:06
  • yes bcz i try to add this function inside the QVTKOpenGLWidget.cxx then it will not work.Also I am not able to create another file with the same name. – Amar Kumar Sep 12 '18 at 13:18
  • @AmarKumar, do not add this function inside the QVTKOpenGLWidget.cxx. Instead subclass QVTKOpenGLWidget and add it there. Of course, instead of `QOpenGLWidget::paintEvent(event);` you have to call `QVTKOpenGLWidget::paintEvent(event);`. – scopchanov Sep 12 '18 at 13:24
  • Inside this one class( class QOpenGLWidget : public QWidget) i will put the function but still this one not working. – Amar Kumar Sep 12 '18 at 13:54
  • @AmarKumar, you have to subclass it, not to change the provided classes. – scopchanov Sep 12 '18 at 13:56
  • please give me example related to mainwindow.cpp – Amar Kumar Sep 12 '18 at 13:58
  • @AmarKumar, what do you mean by _related to mainwindow.cpp_? – scopchanov Sep 12 '18 at 13:59
  • I need to add this code inside my mainwindow.cpp. I try to add ur mention code inside the subclass of openGl Widget.cpp but it will not response properly. – Amar Kumar Sep 24 '18 at 13:27
  • @AmarKumar, it is not clear: _inside my mainwindow.cpp_ or _inside the subclass of openGl Widget.cpp_? – scopchanov Sep 24 '18 at 13:29
  • I need to write the code inside the mainwindow.cpp file – Amar Kumar Sep 24 '18 at 13:45
  • @AmarKumar, I am willing to help you. So, in order to be able to do that, I suggest you to post another question, add a link to this one and describe how do you try to use the code I have written and where you have problems. Then it would be much clearer what should be done. – scopchanov Sep 24 '18 at 13:50