I am trying to figure out a way to save the current state and all values in the gui such as the text in QLineEdit and QEditText widgets.
I found this code which I have been trying to use and it seems that I can get it to save everything okay when I exit the GUI but when I open it, all it seems to restore is the window dimensions if I had moved them previously.
I can see in the ini file that everything gets saved including any text in the 2 widgets but I cant get the text to restore when I open the GUI. Does anyone know how I can get the text values to restore as well?
Here is what I am currently working with.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def restore(settings):
finfo = QFileInfo(settings.fileName())
if finfo.exists() and finfo.isFile():
for w in qApp.allWidgets():
mo = w.metaObject()
if w.objectName() != "":
for i in range(mo.propertyCount()):
name = mo.property(i).name()
val = settings.value("{}/{}".format(w.objectName(), name), w.property(name))
w.setProperty(name, val)
def save(settings):
for w in qApp.allWidgets():
mo = w.metaObject()
if w.objectName() != "":
for i in range(mo.propertyCount()):
name = mo.property(i).name()
settings.setValue("{}/{}".format(w.objectName(), name), w.property(name))
class MainWindow(QWidget):
settings = QSettings("gui.ini", QSettings.IniFormat)
def __init__(self):
super(MainWindow, self).__init__()
self.setObjectName("MainWindow")
restore(self.settings)
self.layout = QGridLayout()
self.text_Box = QTextEdit(self)
self.text_Box.setObjectName("text_Box")
self.layout.addWidget(self.text_Box, 2, 0, 1, 1)
self.quit_Button = QPushButton(self)
self.quit_Button.setMaximumSize(30, 30)
self.quit_Button.setObjectName("quit_Button")
self.layout.addWidget(self.quit_Button, 3, 0, 1, 1)
self.line_Edit = QLineEdit(self)
self.line_Edit.setObjectName("line_Edit")
self.layout.addWidget(self.line_Edit, 1, 0, 1, 1)
self.quit_Button.clicked.connect(self.exitGUI)
self.setLayout(self.layout)
def closeEvent(self, event):
save(self.settings)
QWidget.closeEvent(self, event)
def exitGUI(self):
self.close()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
Related
I'm trying to incorporate a QSplitter. The code works perfectly from a functionality standpoint, but the QSplitter itself doesn't appear correctly under the default PyQt style... possibly because it is itself embedded within a vertical splitter. This is confusing for the user.
If you uncomment out the line (and thus change the default PyQt style), the QSplitter visualizes correctly only when hovered over... however, I also don't want this other style.
Can anyone provide any guidance on this matter?
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
L_layout = QGridLayout()
R_layout = QGridLayout()
L_widgets = QWidget()
L_widgets.setLayout(L_layout)
R_widgets = QWidget()
R_widgets.setLayout(R_layout)
topleft = QFrame()
topleft.setFrameShape(QFrame.StyledPanel)
btn1 = QPushButton('btn1')
bottom = QFrame()
bottom.setFrameShape(QFrame.StyledPanel)
textedit = QTextEdit()
L_layout.addWidget(topleft, 0, 0, 1, 1)
L_layout.addWidget(btn1, 1, 0, 1, 1)
R_layout.addWidget(textedit)
splitter1 = QSplitter(Qt.Horizontal,frameShape=QFrame.StyledPanel,frameShadow=QFrame.Plain)
splitter1.addWidget(L_widgets)
splitter1.addWidget(R_widgets)
splitter1.setStretchFactor(1,1)
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)
hbox.addWidget(splitter2)
self.setLayout(hbox)
#QApplication.setStyle(QStyleFactory.create('Cleanlooks'))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QSplitter demo')
self.show()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
EDIT: This is apparently a known macOS bug. When viewed on Linux, the bar of splitter1 has the same look as splitter2. I'll leave this topic open in case anyone else knows of a suitable workaround for mac.
Because the QPushButton has default minimum size, when you want to move splitter to left,
the button has reached its minimum size. So you can not move left anymore, otherwise the left will will collapse.
So if you want the left showing as you want, you can set the minimum size off button widget.
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
L_layout = QGridLayout()
R_layout = QGridLayout()
L_widgets = QWidget()
L_widgets.setLayout(L_layout)
R_widgets = QWidget()
R_widgets.setLayout(R_layout)
topleft = QFrame()
topleft.setFrameShape(QFrame.StyledPanel)
btn1 = QPushButton('btn1')
btn1.setMinimumWidth(1) # For example : set the minimum width to 1, then you can move left until the btn1 width is 1
bottom = QFrame()
bottom.setFrameShape(QFrame.StyledPanel)
textedit = QTextEdit()
L_layout.addWidget(topleft, 0, 0, 1, 1)
L_layout.addWidget(btn1, 1, 0, 1, 1)
R_layout.addWidget(textedit)
splitter1 = QSplitter(Qt.Horizontal,frameShape=QFrame.StyledPanel,frameShadow=QFrame.Plain)
splitter1.addWidget(L_widgets)
splitter1.addWidget(R_widgets)
splitter1.setStretchFactor(1,1)
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)
hbox.addWidget(splitter2)
self.setLayout(hbox)
#QApplication.setStyle(QStyleFactory.create('Cleanlooks'))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QSplitter demo')
self.show()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I am trying to make a list box where a user can enter more items to the list. I have a list box set up with an add button. The add button opens up a user input box where it will direct the user to enter a value. However, I am have issue with passing on the value of user input to the add onto the list. Any suggestions would help. Below is my code:
List box
from input_box import *
class list_form(QtGui.QWidget):
def __init__(self,list_of_items,open_text,parent= None):
super(list_form, self).__init__()
global output_path
output_path = output_path_i
grid = QtGui.QGridLayout()
grid.setSpacing(10)
self.widget = QtGui.QWidget()
self.layout = QtGui.QGridLayout(self.widget)
open_message = QtGui.QLabel(open_text)
grid.addWidget(open_message,0,0,2,4)
self.lst = QtGui.QListWidget()
grid.addWidget(self.lst,3, 0,1,4)
for i in list_of_items:
self.lst.addItem(str(i))
self.setLayout(grid)
add = QtGui.QPushButton('Add')
grid.addWidget(add,50,0)
add.clicked.connect(self.add_button)
def add_button(self):
self.input_box = input_box()
self.input_box.setWindowTitle("Window 2")
self.input_box.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = list_form(list(xrange(100)),"List of Values")
window.setWindowTitle('Window 1')
window.show()
sip.setdestroyonexit(False)
sys.exit(app.exec_())
Input box
import sys
from PyQt4 import QtCore, QtGui
import sip
class input_box(QtGui.QWidget):
def __init__(self,parent= None):
super(input_box, self).__init__()
grid = QtGui.QGridLayout()
grid.setSpacing(10)
self.widget = QtGui.QWidget()
self.layout = QtGui.QGridLayout(self.widget)
open_message = QtGui.QLabel("Enter Value:")
grid.addWidget(open_message,0,0,2,3)
self.txt = QtGui.QLineEdit()
grid.addWidget(self.txt,2, 0,1,2)
self.setLayout(grid)
save = QtGui.QPushButton('Save')
grid.addWidget(save,50,0)
a = save.clicked.connect(self.save)
save.clicked.connect(self.close)
cancel = QtGui.QPushButton('Cancel')
grid.addWidget(cancel,50,1)
cancel.clicked.connect(self.close)
def save(self):
value = self.txt.text()
return value
If you want to get data from a window you should use a QDialog instead of a QWidget, connect the clicked signal of save and cancel to the accept and reject slot, respectively:
input_box.py
from PyQt4 import QtCore, QtGui
class Input_Box(QtGui.QDialog):
def __init__(self,parent= None):
super(Input_Box, self).__init__(parent)
open_message = QtGui.QLabel("Enter Value:")
self.txt = QtGui.QLineEdit()
save = QtGui.QPushButton('Save', clicked=self.accept)
cancel = QtGui.QPushButton('Cancel', clicked=self.reject)
grid = QtGui.QGridLayout(self)
grid.setSpacing(10)
grid.addWidget(open_message, 0, 0)
grid.addWidget(self.txt, 1, 0, 1, 2)
grid.addWidget(save, 2, 0)
grid.addWidget(cancel, 2, 1)
self.setFixedSize(self.sizeHint())
def save(self):
value = self.txt.text()
return value
Then you use the exec_() method that returns a code if it is called accept or reject, and according to that the data must be obtained and added. On the other hand, do not use global variables because they are a headache when debugging.
from PyQt4 import QtCore, QtGui
from input_box import Input_Box
class list_form(QtGui.QWidget):
def __init__(self,list_of_items,open_text,parent= None):
super(list_form, self).__init__()
open_message = QtGui.QLabel(open_text)
self.lst = QtGui.QListWidget()
self.lst.addItems([str(i) for i in list_of_items])
add = QtGui.QPushButton('Add', clicked=self.add_button)
grid = QtGui.QGridLayout(self)
grid.setSpacing(10)
grid.addWidget(open_message)
grid.addWidget(self.lst)
grid.addWidget(add)
#QtCore.pyqtSlot()
def add_button(self):
input_box = Input_Box()
input_box.setWindowTitle("Window 2")
if input_box.exec_() == QtGui.QDialog.Accepted:
val = input_box.save()
it = QtGui.QListWidgetItem(val)
self.lst.addItem(it)
self.lst.scrollToItem(it)
if __name__ == "__main__":
import sys
import sip
app = QtGui.QApplication(sys.argv)
window = list_form(list(range(100)),"List of Values")
window.setWindowTitle('Window 1')
window.show()
sip.setdestroyonexit(False)
sys.exit(app.exec_())
The advantage of using QDialog is the decoupling between the classes, for example there is no need to pass a QListWidget as the other answer suggests making it possible that you can use the same dialog for other purposes.
You need to arrange for the action taken when the save button is pressed to pass information back to the list widget. There's more than one way to do it, but just returning the data won't get it done.
Here's an example of the sort of thing that will work - though other approaches are possible.
Change the input_box constructor so it keeps a reference to the list widget which it expects:
class input_box(QtGui.QWidget):
def __init__(self, list_widget, parent= None):
super(input_box, self).__init__()
self.list_widget = list_widget
...
Change the call to the constructor to provide that information:
def add_button(self):
self.input_box = input_box(self.lst)
self.input_box.setWindowTitle("Window 2")
self.input_box.show()
And then use that information in the save method to add to the list widget:
def save(self):
value = self.txt.text()
self.list_widget.addItem(value)
Bingo!
An alternative approach could be to arrange for the input_box to emit a signal with the new value in it, and connect that to a slot on the list_form, or on the list_widget. Or in the input_box you could navigate via its parent to the list_widget. But I think mine is simple and straightforward.
I am developing an app using PyQt4. And I would like to have an option to print the main widget to a pdf document. I have a custom qlayout for the main widget and I want to create a pdf document with that qlayout. I read a lot about pyqt qprinter, but I'm not sure that's what I want.
Could anyone suggest how I could create a pdf with a qlayout full of qwidgets?
Use QPixmap.grabWidget to render the widget to a pixmap, then paint that on a QPrinter which can then convert it to a pdf:
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.text = QtGui.QTextEdit(self)
self.text.setText(open(__file__).read())
self.edit = QtGui.QLineEdit(self)
self.edit.setText('/tmp/test.pdf')
self.buttonSave = QtGui.QPushButton('Save', self)
self.buttonSave.clicked.connect(self.handleSave)
layout = QtGui.QGridLayout(self)
layout.addWidget(self.text, 0, 0, 1, 2)
layout.addWidget(self.edit, 1, 0, 1, 1)
layout.addWidget(self.buttonSave, 1, 1, 1, 1)
def handleSave(self):
printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
printer.setPageSize(QtGui.QPrinter.A6)
printer.setColorMode(QtGui.QPrinter.Color)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
printer.setOutputFileName(self.edit.text())
pixmap = QtGui.QPixmap.grabWidget(self).scaled(
printer.pageRect(QtGui.QPrinter.DevicePixel).size().toSize(),
QtCore.Qt.KeepAspectRatio)
painter = QtGui.QPainter(printer)
painter.drawPixmap(0, 0, pixmap)
painter.end()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 640, 640)
window.show()
sys.exit(app.exec_())
EDIT:
If the QPainter part won't work for some reason on your setup, you could try the alternative save method below:
def handleSave(self):
printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
printer.setPageSize(QtGui.QPrinter.A9)
printer.setColorMode(QtGui.QPrinter.Color)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
printer.setOutputFileName(self.edit.text())
self.render(printer)
Or another alternative would be to use a QTextDocument:
def handleSave(self):
printer = QtGui.QPrinter()
printer.setPageSize(QtGui.QPrinter.A5)
printer.setResolution(200)
printer.setColorMode(QtGui.QPrinter.Color)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
printer.setOutputFileName(self.edit.text())
size = printer.pageRect(QtGui.QPrinter.DevicePixel).size()
pixmap = QtGui.QPixmap.grabWidget(self).scaled(
size.toSize(), QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation)
data = QtCore.QByteArray()
buffer = QtCore.QBuffer(data)
pixmap.save(buffer, 'PNG')
document = QtGui.QTextDocument()
document.setPageSize(size)
document.setHtml('<img src="data:image/png;base64,%s"/>' %
bytes(data.toBase64()).decode('ascii'))
document.print_(printer)
I am trying to make a simple interface using Pyside that will accept and write text to a csv file.
The code below doesn't produce an error message but it will only write things like "PySide.QtGui.QLineEdit object at 0x03A534B8" to the csv file. I have been trying to work out how to set these as strings but am stuck (I have minimal experience with python and pyside). What am I doing wrong?
import sys
from PySide import QtGui, QtCore
import csv
class Form(QtGui.QWidget):
def __init__(self):
super(Form, self).__init__()
self.initUI()
def initUI(self):
global itemText
global descText
item = QtGui.QLabel('Item')
itemEdit = QtGui.QLineEdit()
itemText = str(itemEdit)
desc = QtGui.QLabel('Description (optional)')
descEdit = QtGui.QTextEdit()
descText = str(descEdit)
add = QtGui.QPushButton("Add item")
grid = QtGui.QGridLayout()
grid.setSpacing(10)
grid.addWidget(item, 1, 0)
grid.addWidget(itemEdit, 1, 1)
grid.addWidget(desc, 2, 0)
grid.addWidget(descEdit, 2, 1, 3, 1)
grid.addWidget(add, 6, 1)
add.clicked.connect(self.writeFile)
self.setLayout(grid)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle("Add to list")
self.show()
def writeFile(self):
csvfile = open('list.csv', 'ab')
csvwriter = csv.writer(csvfile)
csvwriter.writerow([itemText, descText])
print itemText
def main():
app = QtGui.QApplication(sys.argv)
ex = Form()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Try changing this line
descText = str(descEdit)
for this:
descText = str(descEdit.text())
I am new to pyqt. I am doing a program that allows you clicks on the picture and remember the coordinates of points you clicks and draw a stickfigure on a widget of the GUI. My code right now can prompt out a new window to show a polygon with 4 points. However, I hope it can be displayed on the ui file I alreay made by pyqt. The object name for the widget is called widget.I hope someone can help me to modify the code to display the polygon on the gui widget not prompting out a new window.
Thank you so much!!!
import sys
from PyQt4.QtCore import *
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from Main_window import *
global imgloc
imgloc = "1.jpg"
array = []
clicks = 0
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.local_image = QImage(imgloc)
self.imageLocation = imgloc
self.local_scene = QGraphicsScene()
self.pixMapItem = QGraphicsPixmapItem(QPixmap(self.local_image), None, self.local_scene)
self.ui.graphicsView_5.setScene( self.local_scene )
self.pixMapItem.mousePressEvent = self.pixelSelect
def pixelSelect(self,event):
global imgloc
a = event.pos().x()
b = event.pos().y()
global clicks
global array
if clicks != 4:
clicks += 1
point = QPoint(a,b)
array.append(point)
else:
clicks = 0
dialog = DialogBody()
dialog.show()
dialog.exec_()
array = []
class DialogBody(QDialog):
def __init__(self,parent=None):
super(QDialog,self).__init__(parent)
self.setGeometry(100, 100, QImage(imgloc).height(), QImage(imgloc).width())
def paintEvent(self,e):
qp = QtGui.QPainter()
qp.begin(self)
self.drawBody(qp)
qp.end()
def drawBody(self, qp):
qp.setPen(QtCore.Qt.red)
qp.drawPolygon(array[0],array[1],array[2],array[3])
qp.drawEllipse(array[0],2,2)
qp.drawEllipse(array[1],2,2)
qp.drawEllipse(array[2],2,2)
qp.drawEllipse(array[3],2,2)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp= MyForm()
myapp.show()
sys.exit(app.exec_())
Looks like you want to draw items on QGraphicsScene? In this case you could add items to the scene:
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui
class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.scene = QtGui.QGraphicsScene()
self.view = QtGui.QGraphicsView(self.scene)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.view)
self.setLayout(layout)
self.pixmap_item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap('image.png'), None, self.scene)
self.pixmap_item.mousePressEvent = self.pixelSelect
self.click_positions = []
def pixelSelect(self, event):
self.click_positions.append(event.pos())
if len(self.click_positions) < 4:
return
pen = QtGui.QPen(QtCore.Qt.red)
self.scene.addPolygon(QtGui.QPolygonF(self.click_positions), pen)
for point in self.click_positions:
self.scene.addEllipse(point.x(), point.y(), 2, 2, pen)
self.click_positions = []
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
widget = MainWidget()
widget.resize(640, 480)
widget.show()
sys.exit(app.exec_())
QGraphicsScene has many features.
Read Graphics View Framework overview in Qt docs.