pyqt4 | qt 4.8 | linux (open suse) | python 2.7
I have a simple question about a strange behaviour concerning pyqt and python memory.
I did a little test, a widget, the creation of 1000 buttons on this widget and set the parent to the widget to None.
I think that the good behaviour is, on the widget (with 1000 buttons inside) if we do widget.setParent(None), qt will destroy it (and all of his children).
but if we add a widget.setStyleSheet('background-color:green'), it seems to be that qt doesn't destroy button, OR python doesn't free the memory of these buttons.
a simple test code. (good behaviour)
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
widgetCentral = QtGui.QWidget(self)
for dummy in range(1000):
beginBtn = QtGui.QPushButton(widgetCentral)
beginBtn.setText('btn')
# without this line, the memory is 22mo, if we had this line, the memory fall to 15mo
#widgetCentral.setParent(None)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
and now, the same example with the style sheet
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
widgetCentral = QtGui.QWidget(self)
widgetCentral.setStyleSheet('background-color:green;')
for dummy in range(1000):
beginBtn = QtGui.QPushButton(widgetCentral)
beginBtn.setText('btn')
# with or without this line, the memory is 22mo/23mo
#widgetCentral.setParent(None)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
so it seems to be a bad behaviour with style sheet and python memory (garbage ?)
someone has an idea about this behaviour ?
it's a bug or just a bad implementation ?
Related
My code calls one window with a button. When the button is clicked, call another window. But the second window closes immediately
"basic" and "windows_two" are .py libraries genereted by pyuic5 from .ui files
import basic, windows_two
from PyQt5 import QtCore, QtGui, QtWidgets
if __name__ == "__main__":
#Declarations
import sys
app = QtWidgets.QApplication(sys.argv)
def Call_Second_Window():
#Second Screen
Form = QtWidgets.QWidget()
ui = windows_two.Ui_Form()
ui.setupUi(Form)
Form.show()
def Call_Main_Window():
#MainWindow
MainWindow = QtWidgets.QMainWindow()
ui = basic.Ui_MainWindow()
ui.setupUi(MainWindow)
ui.printButton.clicked.connect(Call_Second_Window) #click event to second window
MainWindow.show()
sys.exit(app.exec_())
Call_Main_Window()
Whats wrong?
Thanks
Whenever a variable is local it gets "garbage collected" as soon as the function returns; this means that everything the variable might reference to will also be (possibly) deleted too.
What is happening in your case is that while the windows is correctly created, it will be immediately deleted (due to the garbage collection) when the Call_Second_Window returns (just after Form.show()).
To avoid that there is only one solution: make the reference to the object persistent. There are various approaches to achieve that, depending on the situation.
Unfortunately your code is a bit unorthodox (especially from a PyQt perspective), so I'm "refactoring" it in order to make it more standardized, better object oriented and, also importantly, easily readable.
import basic, windows_two
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = basic.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.printButton.clicked.connect(self.call_Second_Window)
self.secondWindow = None
def call_Second_Window(self):
if not self.secondWindow:
self.secondWindow = SecondWindow()
self.secondWindow.show()
class SecondWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.ui = windows_two.Ui_Form()
self.ui.setupUi(self)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
Note: As you can see, I changed the name of call_Second_Window with a lower "c", and that's because capitalized names should only be used for classes and constants, while function names should always start with a lower case. This is again for readability, which is very important in programming and one of the core principles of python. Read more about this and other important topics on the official Style Guide for Python Code.
In PyQt4, is there a way to suspend resize of a window until a function is completed?
My problem is that I have created a window with a text edit that might contain large amounts of text. Since I switched to working with a grid layout, the text edit gets resized as well, and when there is a lot of text, the application hangs. I tried overriding resizeEvent to clear text edit text at resize but the application still hangs, since it is clearing the text only AFTER resizing.
Other solutions are welcomed as well.
The python code (and a link to the .ui file):
import sys
from PyQt4 import QtGui, uic, QtCore
from PyQt4.QtGui import QDesktopWidget
qtCreatorMainWindowFile = "mainwindow.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorMainWindowFile)
class MainWindow(QtBaseClass, Ui_MainWindow):
def __init__(self):
QtBaseClass.__init__(self)
self.setupUi(self)
# Set window size to 4/5 of the screen dimensions
sg = QDesktopWidget().screenGeometry()
self.resize(sg.width()*4/5, sg.height()*4/5)
self.clearTextBrowserButton.clicked.connect(self.ClearTextBrowsers)
#staticmethod
def WriteToTextBrowser(string, text_browser):
cursor = text_browser.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(string)
text_browser.setTextCursor(cursor)
text_browser.ensureCursorVisible()
def ClearTextBrowsers(self):
self.textBrowser.clear()
# def resizeEvent(self,event):
# print "resize"
# self.ClearTextBrowsers()
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
for i in range(1,100000):
window.WriteToTextBrowser("TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST\r\n",window.textBrowser)
sys.exit(app.exec_())
The ui. file:
https://www.dropbox.com/s/y3hxp6mjhfpv2hy/mainwindow.ui?dl=0
I found a workaround that seems to work so far. I added an event filter that catches "Move" or "WindowStateChange" QEvents. These seem to happen before the actual resize (the prior works for clicking and stretching and the latter for maximizing/minimizing). The downside is that simply moving the window clears the text edit, but it is a price I'm willing to pay.
The added code (inside MainWindow):
self.installEventFilter(self)
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.Move or event.type() == QtCore.QEvent.WindowStateChange):
self.file.write(self.textBrowser.toPlainText())
self.ClearTextBrowsers()
return QtBaseClass.eventFilter(self, source, event)
Disclaimer: New to both python and qt designer
QT Designer 4.8.7
Python 3.4
PyCharm 5.0.3
Question - How do I add controls to the main form or a scroll area widget on the main form (created in QT Designer) programmatically?
I have created a MainWindow in qt designer and added my widgets. The following is the entire test program in PyCharm:
import sys
from PyQt4 import QtGui, QtCore, uic
from PyQt4.QtGui import *
from PyQt4.QtCore import *
qtCreatorFile = "programLauncher.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
# Cannot resize or maximize
self.setFixedSize(1045, 770)
# Add button test
self.dateLabel = QtGui.QLabel("Test")
self.pushButton = QtGui.QPushButton('Test button')
# self.scrollArea_programs.addWidget()
grid = QtGui.QGridLayout()
# self.scrollArea_programs.addWidget(self.pushButton)
grid.addWidget(self.dateLabel,0,0)
grid.addWidget(self.pushButton,0,1)
self.setLayout(grid)
self.pushButton_exit.clicked.connect(self.closeEvent)
def closeEvent(self):
QtGui.QApplication.quit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
As you can see I tried to add controls to a grid but nothing shows up when the program runs - I have also tried to add a control to the scroll area. Can someone help me to just add 1 control to the scroll area at run time - so then I can know the proper way to do it or "a" proper way to do this.
Thanks in advance
Without having access to your programLauncher.ui and making minimal changes to your posted code, you can add your UI elements to the window like so:
from PyQt4 import QtGui
import sys
class MyApp(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
# Cannot resize or maximize
self.setFixedSize(1045, 770)
widget = QtGui.QWidget(self)
self.setCentralWidget(widget)
# Add button test
self.dateLabel = QtGui.QLabel("Test")
self.pushButton = QtGui.QPushButton('Test button')
grid = QtGui.QGridLayout()
grid.addWidget(self.dateLabel, 0, 0)
grid.addWidget(self.pushButton, 0, 1)
widget.setLayout(grid)
self.pushButton.clicked.connect(self.closeEvent)
def closeEvent(self, event):
QtGui.QApplication.quit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
This will get the controls on the screen, although the layout leaves a lot to be desired. You may have to make modifications to this based on what's in your .ui file. One thing that you'll want to note in this example is that the QMainWindow needs a central widget (widget in the example above). You then set the layout on that widget.
You can use the designer to create your .ui file
The you can load it in your .py using something like:
from PyQt4 import QtCore, QtGui, uic
class my_win(QtGui.QMainWindow):
def __init__(self):
self.ui = uic.loadUi('my_ui.ui',self)
then you can access all your widgets with something like
self.ui.actionQuit.triggered.connect(QtGui.qApp.quit)
or
self.ui.my_button.triggered.connect(self.do_someting)
Thanks to JCVanHamme (the programLauncher.ui hint) and also outside help I now learned most of what I need to know to access MainWindow at run time. So for anyone interested in this beginner tip:
Take a blank form in QT Designer
Add a control
Run pyuic4 batch file
Take a look at the generated .py file to learn EVERYTHING about how to add controls.
Don't let the power go to your head - cheers
This is a Mac OS QT issue,
I have created a QMainWindow and added a toolbar to it.
after setting "setUnifiedTitleAndToolBarOnMac" flag to TRUE my toolbar got unified to my mainwindow.
But when i customize the window buttons such as only close button option,
my window Toolbar got detached from title bar as if it looks like a winodow OS toolbar
not like a Mac native one.
Attached my test code below which is in pyqt.
Hope someone know how to achieve it.
Thanks
from PyQt4 import QtGui
from PyQt4 import QtCore
class Ui_windo(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setObjectName("windo")
self.resize(400, 300)
self.setWindowTitle(QtGui.QApplication.translate("window", "window", None, QtGui.QApplication.UnicodeUTF8))
self.b1 = QtGui.QToolButton()
self.b1.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly)
self.b1.setText('Test')
self.b1.setObjectName("b1")
self.b1.setCheckable(True)
_toolBar = self.addToolBar('test')
_toolBar.setMovable(False)
_toolBar.addWidget(self.b1)
self.setUnifiedTitleAndToolBarOnMac(True);
_windowButtons = QtCore.Qt.Window| QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.CustomizeWindowHint
self.setWindowFlags(_windowButtons)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = Ui_windo()
ui.show()
sys.exit(app.exec_())
Seems that the Qt.CustomizeWindowHint flag removes the styling from the toolbar. Not sure if this is expected behavior or a bug. There have been a number of style-related bug reports pertaining to setUnifiedTitleAndToolBarOnMac. You should post it to confirm whether its expected or not.
https://bugreports.qt-project.org/secure/IssueNavigator.jspa
If you set your window flag to use Qt.Tool, that will at least get you close by removing the minimize button.
I'm having problems with a "New Window" function in PyQt4/PySide with Python 2.7. I connected a initNewWindow() function, to create a new window, to an action and put it in a menu bar. Once a common function in desktop software. Instead of giving me a new persistent window alongside the other one the new window pops up and closes. The code I'm working on is proprietary so I created an example that does the same thing with the same error below. Is there any way to get this to work? Runs in PySide with Python 2.7. It was written in and tested in Windows.
from PySide.QtCore import QSize
from PySide.QtGui import QAction
from PySide.QtGui import QApplication
from PySide.QtGui import QLabel
from PySide.QtGui import QMainWindow
from PySide.QtGui import QMenuBar
from PySide.QtGui import QMenu
from sys import argv
def main():
application = QApplication(argv)
window = QMainWindow()
window.setWindowTitle('New Window Test')
menu = QMenuBar(window)
view = QMenu('View')
new_window = QAction('New Window', view)
new_window.triggered.connect(initNewWindow)
view.addAction(new_window)
menu.addMenu(view)
label = QLabel()
label.setMinimumSize(QSize(300,300))
window.setMenuBar(menu)
window.setCentralWidget(label)
window.show()
application.exec_()
def initNewWindow():
window = QMainWindow()
window.setWindowTitle('New Window')
window.show()
if __name__ == '__main__':
main()
If a function creates a PyQt object that the application needs to continue using, you will have to ensure that a reference to it is kept somehow. Otherwise, it could be deleted by the Python garbage collector immediately after the function returns.
So either give the object a parent, or keep it as an attribute of some other object. (In principle, the object could also be made a global variable, but that is usually considered bad practice).
Here's a revised version of your example script that demonstrates how to fix your problem:
from PySide import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
menu = self.menuBar().addMenu(self.tr('View'))
action = menu.addAction(self.tr('New Window'))
action.triggered.connect(self.handleNewWindow)
def handleNewWindow(self):
window = QtGui.QMainWindow(self)
window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
window.setWindowTitle(self.tr('New Window'))
window.show()
# or, alternatively
# self.window = QtGui.QMainWindow()
# self.window.setWindowTitle(self.tr('New Window'))
# self.window.show()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(300, 300)
window.show()
sys.exit(app.exec_())
When initNewWindow() returns, the window variable is deleted and the window's reference count drops to zero, causing the newly created C++ object to be deleted. This is why your window closes immediately.
If you want to keep it open, make sure to keep a reference around. The easiest way to do this is to make your new window a child of the calling window, and set its WA_DeleteOnClose widget attribute (see Qt::WidgetAttribute).