如何识别 QTabWidget 中当前选项卡何时发生更改?
我正在使用 QTabWidget,我需要一种方法来在当前选项卡的更改实际发生之前处理它,并且如果满足某些条件,可能会取消它。当前选项卡更改后会收到 QTabWidget::currentChanged
信号,但是是否有 QTabWidget::currentChanging
信号或其他方式来实现我需要的行为?
I'm using a QTabWidget
and I need a way to handle the change of the current tab before it actually happens, and possibly cancel it if certain conditions are met. The QTabWidget::currentChanged
signal is received after the current tab has changed, but is there a QTabWidget::currentChanging
signal or another way to achieve the behavior I need?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就我而言,我像这样连接 SIGNAL 和 SLOT:
在
tabSelected()
函数中,我检查当前选项卡索引:In my case, I connect SIGNAL and SLOT like this:
and in
tabSelected()
function, I check current Tab Index:这就是我解决这个问题的方法
单击按钮,我将 lockTabs 设置为 true 并将当前选项卡索引保存到lockedTab (int)。无论您单击哪个选项卡,它都会让您返回到锁定的选项卡。
我确实同意第一个评论,即禁用选项卡是更好的方法。这是我禁用选项卡的解决方案:
This is how I solved it
On click of a button, I set lockTabs to true and save the current tab index to lockedTab (int). No matter what tab you click it will just throw you back to the locked tab.
I do agree with the first comment that disabling tabs is the better way tho. This is my solution for disabling tabs:
有一个带有事件过滤器的简单解决方案,不需要子类化 QTabWidget。就我而言,我需要禁用切换到特定选项卡
:
在鼠标单击阶段,可以检索当前选定选项卡的索引并准备将其切换(或取消切换,如我的示例中所做的那样)。
There is a simple solution with event filters that doesn't require to subclass QTabWidget. In my case I needed to disable switching to a particular tab
then:
At the stage of mouse click it is possible to retrieve index of a currently selected tab and prepare it for being switched(or cancel switching as done in my example).
在标头中,声明:
创建一个例程
tabChanged
,其中包含currentChanged()
信号的槽。然后:In your header, declare:
Create a routine
tabChanged
have the slot for thecurrentChanged()
signal. Then:如果禁止更改,则使用常规的 QTabWidget 并在发出 currentChanged 后翻转回上一个选项卡对于用户来说并不合适,因为新选项卡在上一个选项卡之前可见重新选择,这是由于
QTabWidget
通知选项卡“已更改”,而不是“即将更改”。一种选择是创建您自己的
QTabWidget
。感谢QTabBar
,这非常明显。您还需要创建类似
QTabWidget
的函数来“像”QTabWidget
一样使用它,但您需要的函数并不多。下面是一个
QTabWidget
类的示例,其中发出aboutToChangeTab
信号,通知选项卡即将更改,可以将allowed
设置为 < code>false 禁止制表符更改。并且:
可以将
aboutToChangeTab
连接到插槽 (allowTabChange
) 并执行以下操作:Using a regular
QTabWidget
and flipping back to previous tab aftercurrentChanged
was emitted if change was forbidden does not look right for user as the new tab is made visible before the previous one is re-selected, this is due toQTabWidget
informing the tab "changed", not "is about to change".One option is to create your own
QTabWidget
. Thanks toQTabBar
, this is pretty obvious.You'll also need to create
QTabWidget
like function to use it "like" aQTabWidget
, but there's not so many function you'll need.Here is an example of
QTabWidget
like class with aaboutToChangeTab
signal being emitted informing that tab is about to be changed, one may setallowed
tofalse
to forbid tab change.And:
One can connect
aboutToChangeTab
to a slot (allowTabChange
) and do something like: