Reputation: 137
Hello PyQt/PySide experts,
I am a newbie to both PyQt/PySide and desktop application development (I'm a web developer).
I have a question about how you should navigate among different views in a PyQt/PySide application.
Suppose my app has views (i.e. like pages in a web app) "TEST1"(default) and "TEST2", and I want to switch between them by clicking on the corresponding toolbar item.
I thought I could use QMainWindow.setCentralWidget() to set the requested view each time the toolbar button is clicked, but is this a normal way to navigate among different views in PyQt/PySide?
For your reference, I will post an example code to illustrate the above:
#!/usr/bin/env python
import sys
from PySide import QtCore
from PySide.QtCore import *
from PySide.QtGui import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
# toolbar action 1
self.test1_action = QAction(QIcon('icons/test1.png'), 'Test 1', self)
self.test1_action.triggered.connect(self.show_test1_view)
# toolbar action 2
self.test2_action = QAction(QIcon('icons/test2.png'), 'Test 2', self)
self.test2_action.triggered.connect(self.show_test2_view)
# create toolbar
self.toolbar = self.addToolBar('Actions')
self.toolbar.addAction(self.test1_action)
self.toolbar.addAction(self.test2_action)
# default view is test1, so call the method to set the central widget to "test1" view.
self.show_test1_view()
# switch to "test1" view - just a simple label here.
def show_test1_view(self):
self.test1_view = QLabel('TEST1 VIEW')
self.setCentralWidget(self.test1_view)
# switch to "test2" view - just a simple label here.
def show_test2_view(self):
self.test2_view = QLabel('TEST2 VIEW')
self.setCentralWidget(self.test2_view)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
sys.exit()
Thank you in advance...!
Upvotes: 4
Views: 3009