Results 1 to 2 of 2

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 17:30. Reason: missing [code] tags

Similar Threads

  1. Get QTabBar index from QMdiSubwindow
    By QMAsker in forum Qt Programming
    Replies: 0
    Last Post: 21st May 2019, 13:48
  2. Need access to individual video frames
    By themagician in forum Newbie
    Replies: 2
    Last Post: 26th November 2012, 13:40
  3. changing individual tab content properites of qtabwidget
    By qtnewbi3 in forum Qt Programming
    Replies: 0
    Last Post: 17th February 2012, 18:09
  4. QTabBar & QTabWidget
    By salmanmanekia in forum Newbie
    Replies: 1
    Last Post: 3rd June 2010, 11: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.