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,
{
public:
TabWidget
(QWidget *parent
= nullptr
);
};
class TabWidget : public QTabWidget
{
public:
TabWidget(QWidget *parent = nullptr);
};
To copy to clipboard, switch view to plain text mode
In my CPP file,
constexpr int MAX_COUNT = 10;
class TabWidget* parentTab_;
parentTab_ = new (std::nothrow) TabWidget(this);
parentTab_->setStyleSheet("QTabBar::tab {color: rgb(43, 50, 143);font-weight: bold; font-size: 12}");
parentTab_->setTabsClosable(false);
for(int docketIndex = 0; docketIndex < MAX_COUNT ; docketIndex++)
{
docWidgets_
[docketIndex
] = new (std
::nothrow) QDockWidget(parentTab_
);
// Creating DockWidgets }
constexpr int MAX_COUNT = 10;
class TabWidget* parentTab_;
class QDockWidget* docWidgets_[MAX_COUNT];
parentTab_ = new (std::nothrow) TabWidget(this);
parentTab_->setStyleSheet("QTabBar::tab {color: rgb(43, 50, 143);font-weight: bold; font-size: 12}");
parentTab_->setTabsClosable(false);
for(int docketIndex = 0; docketIndex < MAX_COUNT ; docketIndex++)
{
docWidgets_[docketIndex] = new (std::nothrow) QDockWidget(parentTab_); // Creating DockWidgets
}
To copy to clipboard, switch view to plain text mode
METHOD-1
// Setting Object Name for the docWidgets_ (Setting object name by widget level)
docWidgets_
[docketIndex
]->setObjectName
("Tab_" + QString::number(docketIndex
+1) + "_Control");
// The objectName string will be like "Tab_1_Control", "Tab_2_Control" ..... "Tab_10_Control"
// 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.
QTabBar* tabBar_
= parentTab_
->tabBar
();
for(int tabIndex = 0; tabIndex < MAX_COUNT; tabIndex++)
{
parentTab_
->addTab
(docWidgets_
[tabIndex
],
"#" + QString::number(tabIndex
+ 1));
// Adding tab and adding the DockWidgets to the tab. parentTab_
->setIconSize
(QSize(60,
45));
parentTab_
->setTabIcon
(tabIndex,
QIcon(NO_ICON
));
tabBar_
->tabButton
(tabIndex,
QTabBar::RightSide)->deleteLater
();
tabBar_
->setTabButton
(tabIndex,
QTabBar::RightSide,
0);
// Setting Object Name for the docWidgets_ (Setting object name by widget level)
docWidgets_[docketIndex]->setObjectName("Tab_" + QString::number(docketIndex+1) + "_Control");
// The objectName string will be like "Tab_1_Control", "Tab_2_Control" ..... "Tab_10_Control"
// 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.
QTabBar* tabBar_ = parentTab_->tabBar();
for(int tabIndex = 0; tabIndex < MAX_COUNT; tabIndex++)
{
parentTab_->addTab(docWidgets_[tabIndex], "#" + QString::number(tabIndex + 1)); // Adding tab and adding the DockWidgets to the tab.
parentTab_->setIconSize(QSize(60,45));
parentTab_->setTabIcon(tabIndex, QIcon(NO_ICON));
tabBar_->tabButton(tabIndex, QTabBar::RightSide)->deleteLater();
tabBar_->setTabButton(tabIndex, QTabBar::RightSide, 0);
To copy to clipboard, switch view to plain text mode
METHOD-2
//Trying to set objectName for each tabs of QTabBar using index (Setting object name by tab level)
tabBar_
[tabIndex
].
setObjectName("Tab_" + QString::number(docketIndex
+1) + "_Control");
// This code compiles fine, but gives SEGMENTATION FAULT error.
//Trying to set objectName for each tabs of QTabBar using index (Setting object name by tab level)
tabBar_[tabIndex].setObjectName("Tab_" + QString::number(docketIndex+1) + "_Control");
// This code compiles fine, but gives SEGMENTATION FAULT error.
To copy to clipboard, switch view to plain text mode
METHOD-3
//Trying to set objectName for each tabs of QTabBar without using index (Setting object name by tab level)
tabBar_
->setObjectName
("Tab_" + QString::number(docketIndex
+1) + "_Control");
// 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.
// 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"
//Trying to set objectName for each tabs of QTabBar without using index (Setting object name by tab level)
tabBar_->setObjectName("Tab_" + QString::number(docketIndex+1) + "_Control");
// 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.
// 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
//Trying to set objectName for each tabs through the parentTab_ (QTabWidget) using index (Setting object name by tab level)
parentTab_
[tabIndex
].
setObjectName("Tab_" + QString::number(docketIndex
+1) + "_Control");
// This code compiles fine, but gives SEGMENTATION FAULT error.
//Trying to set objectName for each tabs through the parentTab_ (QTabWidget) using index (Setting object name by tab level)
parentTab_[tabIndex].setObjectName("Tab_" + QString::number(docketIndex+1) + "_Control");
// This code compiles fine, but gives SEGMENTATION FAULT error.
To copy to clipboard, switch view to plain text mode
METHOD-5
//Trying to set objectName for each tabs through the parentTab_ (QTabWidget) using the widget() API (Setting object name by tab level)
parentTab_
->widget
(tabIndex
)->setObjectName
("Tab_" + QString::number(tabIndex
+1) + "_Control");
// This code compiles fine, but not setting the objectName.
cout<<"tabBar_ current index : " << tabBar_->currentIndex()); // This always prints 0.
cout<<"parentTab_ current index : " << parentTab_->currentIndex()); // This always prints 0.
cout<<"Tabs count in tabBar_ : " << tabBar_->count()); // This prints the tab count correctly as 10
cout<<"Tabs count in parentTab_ : " << parentTab_->currentIndex()); // This prints the tab count correctly as 10
}
//Trying to set objectName for each tabs through the parentTab_ (QTabWidget) using the widget() API (Setting object name by tab level)
parentTab_->widget(tabIndex)->setObjectName("Tab_" + QString::number(tabIndex+1) + "_Control");
// This code compiles fine, but not setting the objectName.
cout<<"tabBar_ current index : " << tabBar_->currentIndex()); // This always prints 0.
cout<<"parentTab_ current index : " << parentTab_->currentIndex()); // This always prints 0.
cout<<"Tabs count in tabBar_ : " << tabBar_->count()); // This prints the tab count correctly as 10
cout<<"Tabs count in parentTab_ : " << parentTab_->currentIndex()); // This prints the tab count correctly as 10
}
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.
Bookmarks