PDA

View Full Version : Working with widgets created bt Qt Designer



dpetican
22nd August 2016, 03:21
I'm trying to break away from having all my widgets and my main window in one user interface file. So in the example below I've created a frame that I want to show up in the main window. Later on I will show and hide it along with other frames etc. It pops up but in its own window and only briefly. What am I doing wrong? How do I get the QFrame UI file I created in Qt Designer to behave like what I would expect? That is to say a frame and not frame inside a window? I've excluded the boilerplate code that pyuic4 generates.

myApp.py:


import sys
from PyQt4 import QtCore, QtGui
from main_window import Ui_MainWindow
from frame2 import Frame

class StartQt4(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
myFrame = Frame()
myFrame.show()

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mainWin = StartQt4()
mainWin.show()
sys.exit(app.exec_())

main_window.py:


class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(640, 480)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(155, 45, 146, 81))
self.label.setObjectName(_fromUtf8("label"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.label.setText(_translate("MainWindow", "Main Window without menu bar", None))

frame2.py:


class Ui_Frame(object):
def setupUi(self, Frame):
Frame.setObjectName(_fromUtf8("Frame"))
Frame.resize(320, 240)
Frame.setFrameShape(QtGui.QFrame.Box)
Frame.setFrameShadow(QtGui.QFrame.Plain)
Frame.setLineWidth(8)
self.label = QtGui.QLabel(Frame)
self.label.setGeometry(QtCore.QRect(100, 60, 141, 76))
self.label.setObjectName(_fromUtf8("label"))

self.retranslateUi(Frame)
QtCore.QMetaObject.connectSlotsByName(Frame)

def retranslateUi(self, Frame):
Frame.setWindowTitle(_translate("Frame", "Frame", None))
self.label.setText(_translate("Frame", "This is the frame", None))

class Frame(QtGui.QFrame, Ui_Frame):
def __init__(self, parent=None, f=QtCore.Qt.WindowFlags()):
QtGui.QFrame.__init__(self, parent, f)

self.setupUi(self)

anda_skoa
22nd August 2016, 09:50
It shows up in a separte window because a widget becomes a window if it doesn't have any parent widget.

You probably want your main window's central widget as the frame's parent and also add the frame to the central widget's layout.

Cheers,
_

dpetican
22nd August 2016, 14:01
Thanks. All I had to do was what you said:


myFrame = Frame(self)

self references the mainWin parent not itself (myFrame). Check autoFillBackground to get the stuff behind frame hidden. I just put that in for other newbies who might read this in the future.

Not sure about the central widget stuff though. In reality my main window will actually have its own child buttons designed in through Qt Designer and clicked events that will bring up the frame(s) at a specific place on the screen, then when another button is pressed (child object of the frame itself) the frame will close. In other words different buttons in the main window will show different frames, but each frame will have its own child button to hide itself. Am I programming doing this the correct way to achieve what I want?

anda_skoa
22nd August 2016, 15:13
A QMainWindow always works with a central widget, this is why the code you got generated from your designer file has one as well.

The layout of a main window is complex (menu bar, toolbar, etc), so the application develops put their UI into the central widget area.

That widget can be anything, often jsut empty and filled with custom widgets after the main window's setpUi() call.

Cheers,
_

dpetican
22nd August 2016, 17:18
Okay I grok the central widget thing. But I'm creating an application without menu bars, tool bars etc for a touchscreen system. The main window (self) will have a few buttons etc that when clicked show the other frames I've made. The other frames will have their own buttons that when clicked will close themselves. What advantage is there to add all my frames to the central widget of the main window in this case?



myFrame = Form(self)

and


myFrame = Form(self.centralwidget)

anda_skoa
22nd August 2016, 17:38
Okay I grok the central widget thing. But I'm creating an application without menu bars, tool bars etc for a touchscreen system.

Hmm, ok.
Which of the QMainWindow features are you using? I.e. what is your reason to use it instead of, say, a plain QWidget?



What advantage is there to add all my frames to the central widget of the main window in this case?

QMainWindow has a complex layout, you don't want to mess with that.

The central widget is the window's content area, under control of the application developer.

Cheers,
_