Results 1 to 2 of 2

Thread: How to access individual tab in a QTabWidget/QTabBar using index?

  1. #1
    Join Date
    Jun 2018
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default How to access individual tab in a QTabWidget/QTabBar using index?

    Hello Friends,

    I'm new to Qt development. I'm trying to access an individual tab from a QTabBar of the parent QTabWidget and I want to set a unique Object name to each tab for automation purpose using Squish tool.

    I'm able add 'N' number of tabs to the QTabWidget using addTab(), but after adding I'm not able to access individual tab from the list of added tabs. I'm trying accessing the tabs using index like below. I tried with 5 different methods, but none of them giving expected output.

    I have attached the snapshot of my QtabWidget prototype with this thread.

    In my header file,
    Qt Code:
    1. class TabWidget : public QTabWidget
    2. {
    3. public:
    4. TabWidget(QWidget *parent = nullptr);
    5. };
    To copy to clipboard, switch view to plain text mode 


    In my CPP file,

    Qt Code:
    1. constexpr int MAX_COUNT = 10;
    2.  
    3. class TabWidget* parentTab_;
    4. class QDockWidget* docWidgets_[MAX_COUNT];
    5.  
    6. parentTab_ = new (std::nothrow) TabWidget(this);
    7. parentTab_->setStyleSheet("QTabBar::tab {color: rgb(43, 50, 143);font-weight: bold; font-size: 12}");
    8. parentTab_->setTabsClosable(false);
    9.  
    10. for(int docketIndex = 0; docketIndex < MAX_COUNT ; docketIndex++)
    11. {
    12. docWidgets_[docketIndex] = new (std::nothrow) QDockWidget(parentTab_); // Creating DockWidgets
    13. }
    To copy to clipboard, switch view to plain text mode 

    METHOD-1
    Qt Code:
    1. // Setting Object Name for the docWidgets_ (Setting object name by widget level)
    2. docWidgets_[docketIndex]->setObjectName("Tab_" + QString::number(docketIndex+1) + "_Control");
    3. // The objectName string will be like "Tab_1_Control", "Tab_2_Control" ..... "Tab_10_Control"
    4. // This code compiles fine but not setting any objectName, means Squish tool is not able to detect the objects. The objects are detected as "unNamed" in Squish.
    5.  
    6.  
    7. QTabBar* tabBar_ = parentTab_->tabBar();
    8. for(int tabIndex = 0; tabIndex < MAX_COUNT; tabIndex++)
    9. {
    10. parentTab_->addTab(docWidgets_[tabIndex], "#" + QString::number(tabIndex + 1)); // Adding tab and adding the DockWidgets to the tab.
    11. parentTab_->setIconSize(QSize(60,45));
    12. parentTab_->setTabIcon(tabIndex, QIcon(NO_ICON));
    13. tabBar_->tabButton(tabIndex, QTabBar::RightSide)->deleteLater();
    14. tabBar_->setTabButton(tabIndex, QTabBar::RightSide, 0);
    To copy to clipboard, switch view to plain text mode 

    METHOD-2
    Qt Code:
    1. //Trying to set objectName for each tabs of QTabBar using index (Setting object name by tab level)
    2. tabBar_[tabIndex].setObjectName("Tab_" + QString::number(docketIndex+1) + "_Control");
    3. // This code compiles fine, but gives SEGMENTATION FAULT error.
    To copy to clipboard, switch view to plain text mode 

    METHOD-3
    Qt Code:
    1. //Trying to set objectName for each tabs of QTabBar without using index (Setting object name by tab level)
    2. tabBar_->setObjectName("Tab_" + QString::number(docketIndex+1) + "_Control");
    3. // This code is sets the object name, but sets the same object name "Tab_10_Control" for all tabs. Because the last updated value of tabIndex is 9, so tabIndex+1 = 10.
    4. // So it sets the string as "Tab_10_Control". But I'm expecting the objectName string should be like "Tab_1_Control", "Tab_2_Control" ..... "Tab_10_Control"
    To copy to clipboard, switch view to plain text mode 


    METHOD-4
    Qt Code:
    1. //Trying to set objectName for each tabs through the parentTab_ (QTabWidget) using index (Setting object name by tab level)
    2. parentTab_[tabIndex].setObjectName("Tab_" + QString::number(docketIndex+1) + "_Control");
    3. // This code compiles fine, but gives SEGMENTATION FAULT error.
    To copy to clipboard, switch view to plain text mode 

    METHOD-5
    Qt Code:
    1. //Trying to set objectName for each tabs through the parentTab_ (QTabWidget) using the widget() API (Setting object name by tab level)
    2. parentTab_->widget(tabIndex)->setObjectName("Tab_" + QString::number(tabIndex+1) + "_Control");
    3. // This code compiles fine, but not setting the objectName.
    4.  
    5. cout<<"tabBar_ current index : " << tabBar_->currentIndex()); // This always prints 0.
    6. cout<<"parentTab_ current index : " << parentTab_->currentIndex()); // This always prints 0.
    7. cout<<"Tabs count in tabBar_ : " << tabBar_->count()); // This prints the tab count correctly as 10
    8. cout<<"Tabs count in parentTab_ : " << parentTab_->currentIndex()); // This prints the tab count correctly as 10
    9. }
    To copy to clipboard, switch view to plain text mode 
    METHOD-1 and METHOD-5 compiles fine but not setting any objectName. Squish tool is not detecting the objects.
    METHOD-2 and METHOD-4 compiling fine but gives SEGMENTATION FAULT error
    METHOD-3 sets the object name for all tabs, but not unique.

    Looks like, it tabs are added correctly, but couldn't iterate on each tabs using the index positions.
    METHOD-3 works partially fine, but not setting unique object name based on tab index.
    Can someone help me on this? My requirement is, I need to set unique object name for individual tabs in the tabBar.


    -----
    Moderator note: Approved and moved out of Moderator queue. Edited to replace HTML tags with CODE tags.
    Attached Images Attached Images
    Last edited by d_stranz; 31st May 2022 at 18:30. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access individual tab in a QTabWidget/QTabBar using index?

    Qt Code:
    1. tabBar_[tabIndex].setObjectName("Tab_" + QString::number(docketIndex+1) + "_Control");
    2. // This code compiles fine, but gives SEGMENTATION FAULT error.
    To copy to clipboard, switch view to plain text mode 

    I don't see how this code compiles. QTabBar does not have an operator[](int) method, and even if it did, it would return a pointer to some QWidget class which would be dereferenced using the "->" operator, not ".".

    In any case, the individual tabs on a QTabBar are not widgets, so you cannot set an object name for them. If you need to set some unique identifier for them (other than the name that is displayed on the tab), then you could try using the QTabBar::setTabData() method, using the tab index and a QString as the QVariant data to be set.

    Note that the QTabBar::tabButton() method does not return a QWidget representing the tab; it is a QWidget that the program places there using a call to QTabBar::setTabButton() and is a functional decoration in addition to the tab label and icon. The QTabBar simply manages and draws this widget.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Get QTabBar index from QMdiSubwindow
    By QMAsker in forum Qt Programming
    Replies: 0
    Last Post: 21st May 2019, 14:48
  2. Need access to individual video frames
    By themagician in forum Newbie
    Replies: 2
    Last Post: 26th November 2012, 14:40
  3. changing individual tab content properites of qtabwidget
    By qtnewbi3 in forum Qt Programming
    Replies: 0
    Last Post: 17th February 2012, 19:09
  4. QTabBar & QTabWidget
    By salmanmanekia in forum Newbie
    Replies: 1
    Last Post: 3rd June 2010, 12:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.