Results 1 to 5 of 5

Thread: QTabWidget - prevent change of tab without disabling tabs

  1. #1
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question QTabWidget - prevent change of tab without disabling tabs

    Hi,

    This question has actually been asked, but I am not sure what was the context of it.
    Also, the solution in that thread is not clear, so I will ask the question again with my context.

    In a QTabWidget, I want the tab buttons to be enabled, but the tab switching needs to be prevented for some tabs.
    The use of this is in the below scenario.

    There are two tabs - "User" and "Admin".
    When "Admin" is clicked, a dialog/popup to ask for user name and password needs to be displayed, and only on successful login, tab should change to Admin.
    But "User" tab can be clicked and User screen should be displayed.
    If I disable the "Admin" Tab, I cannot click it to show the popup.
    And if I don't block the opening of Admin tab page, unauthorized viewing will be there.

    Can anyone please suggest me how to proceed.

    Is there any event that leads to TabChange (i.e. before the Tab is changed)

    Regards,
    Ash

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,064
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QTabWidget - prevent change of tab without disabling tabs

    I do not know when the QTabWidget::tabBarClicked() signal is emitted - before or after the tab is changed. If it is emitted before the tab is changed, then that is probably what you want. However, I do not think there is any way of preventing the change if the user doesn't enter the right credentials for Admin.

    Here is an alternative: Create the QWidget you use for the content of the Admin page, but set its visible state to false when you create it. This way, when the tab appears, the contents will be invisible. Implement a handler for the QTabWidget::currentChanged() signal. When you receive it for your Admin page, post the dialog to log the user in as Admin. If the login succeeds, set the widget to visible. When the user leaves the page, set the widget back to invisible.

  3. #3
    Join Date
    Aug 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTabWidget - prevent change of tab without disabling tabs

    Quote Originally Posted by d_stranz View Post
    I do not know when the QTabWidget::tabBarClicked() signal is emitted - before or after the tab is changed. If it is emitted before the tab is changed, then that is probably what you want. However, I do not think there is any way of preventing the change if the user doesn't enter the right credentials for Admin.

    Here is an alternative: Create the QWidget you use for the content of the Admin page, but set its visible state to false when you create it. This way, when the tab appears, the contents will be invisible. Implement a handler for the QTabWidget::currentChanged() signal. When you receive it for your Admin page, post the dialog to log the user in as Admin. If the login succeeds, set the widget to visible. When the user leaves the page, set the widget back to invisible.
    Hi d_stranz. Thanks for the suggested solution. QTabWidget::tabBarClicked() didn't help me.
    Your other solution was good, i.e. keeping the main widget as invisible. But it had one design issue that the Admin tab was shown as selected, even if the widget is invisible.

    I actually achieved the required behavior by overriding mouseEvents.
    I just think that these are few controls that should be provided natively in QT.

    In any case, if anybody needs, I can post the working sample code for this above requirement/behavior.

  4. #4
    Join Date
    Sep 2016
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTabWidget - prevent change of tab without disabling tabs

    Hi ashtray 4241....can you please share the working sample code.

  5. #5
    Join Date
    Sep 2016
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTabWidget - prevent change of tab without disabling tabs

    I was able to implement this using an eventfilter in PyQt.

    Sample code:

    self.tabWidget.tabBar().installEventFilter(self)

    def eventFilter(self, object, event):
    try:
    if object == self.tabWidget.tabBar() and event.type() in [QtCore.QEvent.MouseButtonPress, QtCore.QEvent.MouseButtonRelease] and event.button() == QtCore.Qt.LeftButton
    # tabIndex = object.tabAt(event.pos())
    if event.type() == QtCore.QEvent.MouseButtonPress:
    if self.tab_1.search_inprogress is True:
    dlg = custom_dialog(width=300, height=100)
    dlg.pushButton_no.setCheckable(True)
    dlg.pushButton_yes.setCheckable(True)
    dlg.pushButton_no.setText("No")
    dlg.pushButton_yes.setText("Yes")
    dlg.pushButton_yes.clicked.connect(self.tab_1.stop _progress)
    dlg.label_text.setText("Search operation is underway. Would you like to stop the operation and proceed?")
    if dlg.exec_() == QtGui.QDialog.Accepted:
    dlg.close()
    if dlg.pushButton_no.isChecked():
    return True
    elif dlg.pushButton_yes.isChecked():
    return False
    else:
    return False
    except Exception as e:
    print "Exception raised in eventfilter", e

Similar Threads

  1. Replies: 1
    Last Post: 12th June 2014, 07:27
  2. QTabWidget with same tabs
    By Djony in forum Qt Programming
    Replies: 20
    Last Post: 24th December 2011, 12:20
  3. Change QTabWidget tabs while resizing
    By michael. in forum Qt Programming
    Replies: 6
    Last Post: 17th May 2011, 11:21
  4. Replies: 4
    Last Post: 28th July 2010, 09:57
  5. QTabWidget - Add Tabs dynamically
    By fruzzo in forum Qt Programming
    Replies: 11
    Last Post: 27th February 2008, 08:26

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.