Reply
Thread Tools
Posts: 47 | Thanked: 41 times | Joined on Jan 2010 @ Finland
#1
Hello, I have a question about PyQt and specially about QTabWidget. I have rewriten most of my application (pyKake) from GTK to PyQt and faced a problem I can't get solved (without restarting the app) by myself. The problem is that I want to hide some of the tabs or show them after the gui is loaded (Example, if I change the camera type from settings, from Canon to Olympus, I want to show a Viewer tab, and in the reverse situation I want to hide it).

My code looks like this:

Code:
class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        # ...
        
        # Creating tabs and using tab widget as a Central widget
        self.tabs = QtGui.QTabWidget()
        self.tabs.TabShape(QtGui.QTabWidget.Rounded)
        self.tabs.addTab(BasicMode(), "Basic") # index = 0
        self.tabs.addTab(BulbMode(), "Bulb") # index = 1
        self.tabs.addTab(TimedMode(), "Timed") # index = 2
        self.tabs.addTab(ViewerMode(), "Viewer") # index = 3
        self.tabs.addTab(SystemPage(), "System") # index = 4
        self.tabHandler("startup")
        
        self.setCentralWidget(self.tabs)

    def tabHandler(self, data):
        print "Start managing tabs"
        
        if data == "startup":
            print "Managing state: StartUp"
            if USEFULLSCREEN != True:
                print "State: No Fullscreen \n   Action: Removing SystemTab"
                self.tabs.removeTab(4)
            if CURRENT_REMOTETYPE != "Olympus_RM-1":
                print "State: No olympus \n   Action: Removing ViewerTab"
                self.tabs.removeTab(3)
                
        if data == "settings":
            print "Managing state: SettingsChange"
            if CURRENT_REMOTETYPE != "Olympus_RM-1":
                print "State: No Olympus\n   Action: removing ViewerTab"
                self.tabs.removeTab(3)
            if CURRENT_REMOTETYPE == "Olympus_RM-1" and USEFULLSCREEN == True:
                print "State: is Olympus, is Fullscreen \n   Action: removing SystemTab, adding ViewerTab, adding SystemTab"
                self.tabs.removeTab(4)
                self.tabs.addTab(ViewerMode(), "Viewer")
                self.tabs.addTab(SystemPage(), "System")
            elif CURRENT_REMOTETYPE == "Olympus_RM-1" and USEFULLSCREEN != True:
                print "State: is Olympus, no Fullscreen \n   Action: adding ViewerTab"
                self.tabs.addTab(ViewerMode(), "Viewer")
        
        self.tabs.update()
        print "Managing tabs stopped!"
# ...

class SettingsDialog(QtGui.QDialog):
    def setSettings(self):
        # ...
        MainWindow().tabHandler("settings")
        settings.sync()

        self.accept()
On the startup function tabHandler work as it should work, it prints out those control messages and removes pages I don't need (or do nothing if there is no need for it). But then, when I later change settings from settings dialog nothing happens. The code itself works as it should do in tabHandler (I get all the control messages I'm waiting for) but GUI won't change, it stays the same as it was loaded in the begining. (self.update() or self.tabs.update() won't work, neither does leaving it out). Hopefully you understand what I try to say.

I know the solution is probably something very basic I just can't get it by myself.
 
fpp's Avatar
Posts: 2,853 | Thanked: 968 times | Joined on Nov 2005
#2
Why don't you just show/hide tabs instead of adding/removing them ?...
__________________
maemo blog
 
Posts: 47 | Thanked: 41 times | Joined on Jan 2010 @ Finland
#3
Originally Posted by fpp View Post
Why don't you just show/hide tabs instead of adding/removing them ?...
That works in GTK yes, but not in Qt, and if I understand documentation for Qt 4.6 (maemo) correctly, that is not possible, or if it is, then I don't know how, because I can't find any hint about it from anywhere, so the only way is to remove and add them, of course I could use Enable/Disable Tabs as well (the tabs are shown, but cannot be used), but the result with them is exactly same as with add/remove, at startup it will work as it should be, but from settings dialog, not.
 
fpp's Avatar
Posts: 2,853 | Thanked: 968 times | Joined on Nov 2005
#4
Well, individual tabs are just QWidgets, and any QWidget has a setVisible() method, so I'd have thought you could hide/show them independently. But admittedly I haven't tried :-)
__________________
maemo blog
 
Posts: 47 | Thanked: 41 times | Joined on Jan 2010 @ Finland
#5
Originally Posted by fpp View Post
Well, individual tabs are just QWidgets, and any QWidget has a setVisible() method, so I'd have thought you could hide/show them independently. But admittedly I haven't tried :-)
Not working, only way to "hide" the tabs (at least in 4.6) is to use remove and add -functions. Which is stupid because if the tabs has indexes anyway, it shouldn't be so hard to write up the method for it, something like QTabWidget::hideTab(int index) and QTabWidget::showTab(int index), I wish I could do that by myself, but I don't know how (not enough skills).

Anyway the problem is kind of solved. (Should I put [SOLVED] in the topic of first message?)

I couldn't figure out how to add and remove tabs from different class, so I modified the code so that all parts of GUI (mainwin, tabs, widgets in tabs and dialogs) and UI handling codes are now on in same class (Class MainWindow) and now it is possible to manage the gui after it is shown.

Now the code I posted previously looks like this (just in case someone want to know):

Code:
class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        # ...
        # Creating tabs and using tab widget as a Central widget
        
        self.tabs = QtGui.QTabWidget()
        self.tabs.TabShape(QtGui.QTabWidget.Rounded)
        self.tabs.addTab(self.basicMode(), "Basic") # index = 0
        self.tabs.addTab(self.bulbMode(), "Bulb") # index = 1
        self.tabs.addTab(self.timedMode(), "Timed") # index = 2
        self.tabs.addTab(self.viewerMode(), "Viewer") # index = 3
        self.tabs.addTab(self.systemMode(), "System") # index = 4
        
        self.setCentralWidget(self.tabs)
        #...
        self.tabVisibility()
        #...
 
    def tabVisibility(self):
        count = self.tabs.count() # Number of tabs, used for failsafe!
        if USEFULLSCREEN != True and count == 5: # Removing systemMode if not fullscreen
            self.tabs.removeTab(count-1)
        
        if CURRENT_REMOTETYPE == "Olympus_RM-1" and USEFULLSCREEN == True and count < 5: # Includes Viewer tab, only if it is not there!
            self.tabs.removeTab((count-1))
            self.tabs.addTab(self.viewerMode(), "Viewer")
            self.tabs.addTab(self.systemMode(), "System")
        
        elif CURRENT_REMOTETYPE == "Olympus_RM-1" and USEFULLSCREEN != True and count == 3: # Includes Viewer tab, only if it is not there!
            self.tabs.addTab(self.viewerMode(), "Viewer")
        
        elif CURRENT_REMOTETYPE != "Olympus_RM-1" and count == 5:
            self.tabs.removeTab(3)
    
    def settingsDialog(self):
        #...
        
    def setSettings(self):
        #...
        self.tabVisibility() # Handling visibility of tabs
        settings.sync()
        self.dialog.accept()
 

The Following User Says Thank You to Yabba For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 04:47.