PDA

View Full Version : Switching windows



Drama
21st January 2016, 02:16
Im making an app that has 2 menus, the first one is a "Welcome" menu, and the second one is for searching something. The problem is that i cant make the searching window show itself. It sticks in the welcome menu. When i started programming the tools for the searching window i kept them in the "Welcome" menu and worked fine until i tried to put those tools in an other menu(ergo different function); So i guess the problem is not that im not calling the self.show() because they worked fine before...

Here's the details of the app and what im doing. Gonna delete the non important parts.
Its kinda complex:

First i have this:


class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()

self.buttons()
self.toolButtons()
self.searchMenu()

There there is the buttons() that creates all the interface of the "Welcome" menu, which was the first one.
toolButtons() explains itself. Im using one of the toolbuttons to switch between menues.
searchMenu() is the second Menu, which is supposed to show itself when you press one of the toolbuttons

A little bit later i have the important functions among others:


def buttons(self):
# Welcome menu. Images and labels.
self.show()

def toolButtons(self):
# Toolbuttons. Here is the button that is supposed to load the search menu, and it does it with these lines:
searchButton = QtGui.QAction(QtGui.QIcon('searchicon.png'), 'Search', self)
searchButton.triggered.connect(self.loadingSearchW indow) #Appears down
self.toolBar.addAction(searchButton)

def searchMenu(self):
# lot of comboboxes, images, etc. Loads everything of the search menu.
self.show()

____________________

Now here is the "loadingSearchWindow" that appears up in the searchButton:


def loadingSearchWindow(self):
search = self.searchMenu()
self.setCentralWidget(search)

So when you click the searchButton, it loads "loadingSearchWindow" which sets the searchMenu() as the CentralWidget.

And again, nothing happends when i click the toolButton that is supposed to load the search menu. And i dont know why, because im doing the same thing with the "buttons" function that loads the welcome menu without problems, but then it seems that if you want to add an other menu it doesn't work.

What am I doing wrong?

d_stranz
21st January 2016, 17:21
You seem to have a basic misunderstanding of the role of the "central widget" in a QMainWindow. All of your posts so far have asked various forms of the same questions, and you seem to persist in making the same mistake over and over.

In most cases, you do not want to keep flipping the central widget back and forth between different other widgets. The better way to accomplish this is to make the central widget a single instance of QStackedWidget. You make the child widgets that should be changed dynamically children of the stacked widget by instantiating them and then calling QStackedWidget::addWidget(). To make one child visible while hiding the others, you call the QStackedWidget::setCurrentIndex() method.

You do not need to create new widgets dynamically in response to button clicks or other actions. You make all of the widgets you need at startup (in __init()__) and add them to the stack. You use the button or action handling slots to make the appropriate child widget current (i.e show it and hide the others).