QTabWidget Style

I have a QTabWidget with a background gradient and two problems.

  • How dow do I remove the anonymous outline around the active tab (see image)? I tried "outline: no" as with buttons, but it doesn't seem to be affected.

  • How do I disable tabs? I tried: disabled and :! Enabled, but both do not work. // Edit: this works with: disabled, but not with all properties. Looks like I tried that which is not supported.

Anoying focus

The qt documentation did not help. Google too .: - (

+4
source share
3 answers

It seems that the focus rectangle is being handled by QStyle (not to be confused with style sheets), which is used. You can write a subclass of QStyle and apply it to your QTabWidget . The subclass should override the drawControl() method and do nothing if it is currently drawing a focus rectangle.

The subclass will look something like this:

NoFocusRectStyle.h

 #ifndef NOFOCUSRECTSTYLE_H #define NOFOCUSRECTSTYLE_H #include <QWindowsVistaStyle> // or the QStyle subclass of your choice class NoFocusRectStyle : public QWindowsVistaStyle { public: NoFocusRectStyle(); protected: void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const; }; #endif // NOFOCUSRECTSTYLE_H 

NoFocusRectStyle.cpp

 #include "NoFocusStyle.h" NoFocusRectStyle::NoFocusRectStyle() { } void NoFocusRectStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const { if(element == CE_FocusFrame) return; QWindowsVistaStyle::drawControl(element, option, painter, widget); } 

Somewhere in your intializer / constructor form, you would apply a custom style subclass to the tab widget:

 ui->tabWidget->setStyle(new NoFocusRectStyle()); 

This should allow your style sheets to continue to work.

It would be nice if there was an easier way to do this, but I could not find it :)

+2
source

This stream is old, but perhaps it will help people.

If you do not need to use focus, you can simply set it through the tab widget:

ui-> tabWidget-> setFocusPolicy (Qt :: NoFocus);

+2
source

The focus rectangle can be removed by adding the snippet below to your style:

 QWidget { outline: 0; } 

This does not apply directly to the QTabWidget style, but it works as you expect.

+1
source

Source: https://habr.com/ru/post/1444242/


All Articles