Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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:

    1. I have plenty of additional custom (not totally native) elements in my Windows application;
    2. and I want my app to have consistent design independently of version the of Windows (7, 10, 11).

    So questions are:

    1. how can I create the modified tab? Using QML?
    2. 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 How do you create the existing tab?



  • @artwaw my code for tabs is:

        QTabWidget *tabWidget = new QTabWidget;
        tabWidget->setTabsClosable(true);
    
        QWidget *testTab = new QWidget;
        QWidget *testTab2 = new QWidget;
        tabWidget->addTab(testTab, "Test tab");
        tabWidget->addTab(testTab2, "Test tab 2");
    


  • @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.html

    You will also find useful examples there.



  • @artwaw Can you please clarify how can I reach such result using layout? Is it possible to replace a standard Tab with custom? I need to include "refresh" button:
    1bff006a-2eaa-40cb-a068-26d1fa754f18-image.png



  • 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 Thank you so much for your recommendations! I will try.