Customizing tab created with QTabWidget.
-
I need to modify the tab created with QTabWidget. I want to add an additional button (besides the close button:
setTabsClosable()
).
In Qt designer, as I understand it, this cannot be done. I tried to make it with QWidget (in cpp file with code) but I didn't succeed.
Also:- I have plenty of additional custom (not totally native) elements in my Windows application;
- and I want my app to have consistent design independently of version the of Windows (7, 10, 11).
So questions are:
- how can I create the modified tab? Using QML?
- Should I create a whole application using QML or I can create separate UI elements with QML and implement them in QWidget application? What approach should I choose?
-
-
@Asimo Please take a look into QWidget documentation, section "Top-level and child widgets".
All you need to do is to add layout to the widget and the button.
Further reading regarding the layouts: https://doc.qt.io/qt-5/layout.htmlYou will also find useful examples there.
-
A quick look at the QTabWidget source code shows that it is hard-coded to use a QTabBar and QStackedWidget together. QTabBar is responsible for drawing the tab imagery. There is no way to replace this tab bar object with your own, customised version.
Your widget-based option is to construct your own QTabWidget look-alike and use a QTabBar sub-class with customised paintEvent() along with a way to enable/disable the refresh button, enable disable animation of the icon, and a signal to indicate it has been clicked. You'll probably have to play with sizing logic as well. Quite a lot of fiddly work I expect.
Alternatives:
- Place a single refresh button as the cornerWidget of the standard QTabWidget and have it refresh the current tab
- Place a single refresh button to the left/right of the QTabBar in your own QTabWidget look-alike.
-
@ChrisW67 What if I want to resize the tabs but index-based?
Similar to a web browser, I want to enable a button/tab-like interface. The first tab should, if possible, display only the icon and the last being a new tab option will display a varied interface.