3

I looked here in stackoverflow and google for some days for something like my case, but all the examples I found did not work.

What I want is to have my parent window with the menu, and then call other child windows from that menu and execute/show them inside the parent window.

I tried put a widget in the parent window and call the child window inside of it, use MDIArea, but nothing worked.

Obs.: My screen files are generated from Qt designer, and I'm making separated classes to manipulate the widgets, pushbuttons, etc to keep everything more organized.

I created MdiArea in my main window using QtDesigner and them in a class triggered by clicking a menu I call the subwindow (a widget created with QtDesigner too) inside the MdiArea.

from resources.SubWindowQtDes import Ui_SubWindow
from resources.MainWindowQTDes import Ui_MainWindow
class cadastraAluno(Ui_SubWindow,Ui_MainWindow):
    def __init__(self, parent=None):
        super(cadastraAluno, self).__init__(parent = None)
        dialog = Ui_SubWindow()
        window = Ui_MainWindow()
        mdi = window.mdiArea
        mdi.addSubWindow(dialog, flags = 0)
        dialog.show()
Pythonian
  • 41
  • 1
  • 2
  • 7
  • If you actually want the child windows to be _inside_ the parent window then MdiArea is probably the way to go. These won't really be children windows though, they'll just be subwindows of your MdiArea widget. How did it fail when you tried this? – 101 May 26 '15 at 23:35
  • I put a piece of code up there in the question(still learning how to use markdown in stackoverflow he he) containing the class I call the MdiArea. The problem is that when I click the menu to call the subwindow inside de Mdi, Eclipse shows an erros telling that my Ui_MainWindow has no atribute mdiArea, although I used the auto completion just to be sure. – Pythonian May 28 '15 at 02:58
  • mdiarea is the name I give to the QmdiArea in my QtDesigner generated File. – Pythonian May 28 '15 at 03:04
  • Did you forget to instantiate `Ui_MainWindow` (i.e. like `window = Ui_MainWindow()` with the `()` at the end)? – 101 May 28 '15 at 03:06
  • Hmm I managed to skip the error... I changed code using : `class cadastraAluno(QMainWindow,Ui_MainWindow)` and `self.setupUi(self)`. After that Eclipse show no more errors, but nohting happens, the subwindow is not called. – Pythonian May 28 '15 at 03:15
  • yes.. I just realized the `( )` thing... fixed that. Now no erros but the mdiArea keeps being empty. Also took out the `flags = 0`. There was an error about that too. – Pythonian May 28 '15 at 03:18
  • Acctualy i found a way of putting the mdiArea to work. Thanks. – Pythonian May 29 '15 at 11:18

2 Answers2

3

Here what I usually do for child windows :

class subwindow(QtGui.QWidget):
    def createWindow(self,WindowWidth,WindowHeight):
       parent=None
       super(subwindow,self).__init__(parent)
       selt.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
       self.resize(WindowWidth,WindowHeight)

class mainwindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
       [...]

    def createsASubwindow(self):
       self.mySubwindow=subwindow()
       self.mySubwindow.createWindow(500,400)
       #make pyqt items here for your subwindow
       #for example self.mySubwindow.button=QtGui.QPushButton(self.mySubwindow)

       self.mySubwindow.show()

This way you have a subwindow that always stays on top of main window and which can only be instantiated once.

I hope it helped

Olivier Giniaux
  • 870
  • 2
  • 8
  • 22
  • It calls the subwindow and so on. But I'm not able to keep the subwindow "embedded" in the parent window... – Pythonian May 28 '15 at 22:41
  • I even created a MdiWindow in the class I call when I click the menu...... No erros on Eclipse, but still nothing happens... it seems I can't dynamically manipulate my widgets in the Parent Window, making them appear and desapear as needed... – Pythonian May 28 '15 at 22:46
  • Maybe you can take a look at the example _Python27\Lib\site-packages\PyQt4\examples\demos\embeddeddialogs\embeddeddialogs.py_, it might use the feature you are looking for. – Olivier Giniaux May 29 '15 at 07:52
  • I found a way of using the QmdiArea. It works now. :) – Pythonian May 29 '15 at 11:10
1

I found a way. I used self.show() instead of dialog.show() and self.mdiAreainstead of window.mdiArea.

Now I close the window and show it again with the widgets I want. I want to find a way to just "refresh" the window. But this is subject for another topic.
Thanks a lot guys.

Pythonian
  • 41
  • 1
  • 2
  • 7