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

QTabBar setTabText and setTabToolTip not working



  • When I tabify QDockWidgets I want to edit the text in the QTabBar for the tabbed docks. I have set the text for one of my QDockWidgets to "F" for texting purposes. The following code extract is inside my MainWindow:

    QObjectList oList = children();
    QObject *object;
    QTabBar *tabBar = nullptr;
    foreach (object, oList) {
        if (object->metaObject()->className() == "QTabBar") {
            tabBar = qobject_cast<QTabBar *>(object);
            break;
        }
    }
    for (int i = 0; i < tabBar->count(); ++i) {
        if (tabBar->tabText(i) == "F") {
            tabBar->setTabText(i, "Folders");                           // fails
            tabBar->setTabToolTip(i, "System Folders");                 // fails
            tabBar->setTabTextColor(i, Qt::red);                        // fails
            tabBar->setTabIcon(i, QIcon(":/images/icon16/test.png"));   // works
            tabBar->setTabVisible(i, false);                            // works
        }
    }
    

    Any ideas why this if failing?


  • Lifetime Qt Champion

    Hi,

    What exactly it failing ?
    Are you sure you are working on the correct QTabBar ?



  • The lines commented // fails are not working. Since the lines:

    tabBar->setTabIcon(i, QIcon(":/images/icon16/test.png"));   // works
    tabBar->setTabVisible(i, false);                            // works
    

    are working, I'm pretty confident I have the correct QTabBar. In addition, a QDebug reported the correct text, which I removed for brevity. Finally, there is only 1 QTabBar in oList.


  • Lifetime Qt Champion

    They are working because they are left untouched by the QMainWindow internal layout.

    See here for more details.



  • @SGaist Thanks for the link. I'm not seeing how this will help me. Can you give me a hint on how to rename the tab text and set the tooltip in this situation? Much appreciated.



  • @Rory_1 said in QTabBar setTabText and setTabToolTip not working:

    tabBar->setTabVisible(i, false); // works

    visibility is set to false/hidden. Where do you verify it's text changed or not?



  • @nagesh I have tried all changes separately. I've just included setTabVisible to show what has worked and what has not.


  • Lifetime Qt Champion

    @Rory_1 said in QTabBar setTabText and setTabToolTip not working:

    @SGaist Thanks for the link. I'm not seeing how this will help me. Can you give me a hint on how to rename the tab text and set the tooltip in this situation? Much appreciated.

    That's what the code shows, you can't as the content of the tab bar is updated based on the QDockWidget, so either you modify the dock widget on docking/undocking or you can try KDDockWidgets. Last option is to build your own qtbase with the modification required to implement what you want.



  • @SGaist Thanks very much. Have a great weekend.


Log in to reply