在 Pyqt 中选择选项卡时未发出信号
我创建了一个选项卡小部件,其中包含 3 个选项卡。当我更改选项卡时,我需要发出信号,查看 pyQt 文档 currentChanged() 是在更改选项卡时发出的信号,但它对我不起作用。因此需要您的帮助来理解相同的内容,
我的代码如下:
main_tab_widget = QTabWidget()
#Creating Tabs
self.run_tab = QWidget()
self.cc_tab = QWidget()
self.result_tab = QWidget()
#Adding Tabs to Tab Widget
main_tab_widget.addTab(self.run_tab, "RUN")
main_tab_widget.addTab(self.cc_tab, "Config Creator")
main_tab_widget.addTab(self.result_tab, "Result")
#Layout Creation
main_layout = QVBoxLayout()
main_layout.addWidget(main_tab_widget)
self.connect(self.cc_tab, SIGNAL('currentChanged(int)'), self.pseudofunction)
self.RunTab()
self.setLayout(main_layout)
#self.configcreatortab()
#self.resulttab()
def pseudofunction(self):
print 'Inside Pseudo Function'
I have a tab widget created with 3 tabs in it. I need to emit signals when I change the tab, Looking at the pyQt documentation currentChanged() is the signal that is emitted on Changing the tab but it does not work for me. So need your help on understanding the same,
MY code is as follows:
main_tab_widget = QTabWidget()
#Creating Tabs
self.run_tab = QWidget()
self.cc_tab = QWidget()
self.result_tab = QWidget()
#Adding Tabs to Tab Widget
main_tab_widget.addTab(self.run_tab, "RUN")
main_tab_widget.addTab(self.cc_tab, "Config Creator")
main_tab_widget.addTab(self.result_tab, "Result")
#Layout Creation
main_layout = QVBoxLayout()
main_layout.addWidget(main_tab_widget)
self.connect(self.cc_tab, SIGNAL('currentChanged(int)'), self.pseudofunction)
self.RunTab()
self.setLayout(main_layout)
#self.configcreatortab()
#self.resulttab()
def pseudofunction(self):
print 'Inside Pseudo Function'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试连接来自错误对象的
currentChanged
信号。main_tab_object
是发出此信号的 QTabWidget,但您正在尝试从self.cc_tab
(选项卡之一中的 QWidget)连接此信号。将该行替换
为
EDIT:无法使用信号/槽机制将不同的函数连接到 QTabWidget 的每个选项卡。如果您想在选择不同选项卡时调用不同的函数,则必须编写另一个函数来根据选择的选项卡调用相关函数。例如:
然后将 QTabWidget 的
currentChanged
信号连接到此函数。您是正确的,QTabBar 也有
currentChanged
信号,但您在第二条评论中使用了它,将QWidget()
替换为QTabBar()
> 在你上面的代码中,不会达到你想要的。QTabBar 仅充当包含选项卡集合的栏。 (事实上,QTabWidget 在内部使用 QTabBar 作为其选项卡栏。)因此,将 QTabBar 放入 QTabWidget 中将为您提供选项卡中的选项卡。我不相信这就是你想要的。此外,我怀疑您没有向 QTabBars 添加任何选项卡。 QTabBars 确实有
currentChanged
信号,并且您的代码将正确连接这些信号,但这些信号永远不会触发,因为 QTabBars 不包含要更改的选项卡。这些 QTabBar 本身可能位于 QTabWidget 内部的事实是无关紧要的。他们只能看到对其自己的选项卡组的更改,因此只能针对对其自己的选项卡组的更改发出信号。
You're attempting to wire up the
currentChanged
signal from the wrong object.main_tab_object
is the QTabWidget that emits this signal, but you're attempting to wire up this signal fromself.cc_tab
, a QWidget in one of the tabs.Replace the line
with
EDIT: There is no way to use the signals/slots mechanism to connect a different function to each tab of a QTabWidget. If you want to call different functions when different tabs are selected, you'll have to write another function that calls the relevant function depending on which tab was selected. For example:
You then connect the
currentChanged
signal of the QTabWidget to this function.You are correct that QTabBar also has the
currentChanged
signal, but your usage of it in your second comment, by replacingQWidget()
withQTabBar()
in your code above, will not achieve what you want.A QTabBar acts only as the bar that contains a collection of tabs. (In fact, the QTabWidget uses a QTabBar internally for its tab bar.) So, putting a QTabBar within a QTabWidget gives you tabs within tabs. I don't believe this is what you want. Furthermore I suspect you're not adding any tabs to the QTabBars. The QTabBars do have the
currentChanged
signal, and your code will correctly connect these signals, but these signals will never fire because the QTabBars contain no tabs to change.The fact that these QTabBars may themselves be inside a QTabWidget is irrelevant. They only see changes to their own set of tabs and hence can only fire signals for changes to their own set of tabs.