PDA

View Full Version : PyQt: How to manipulate QFileDialog



xaverxn
25th October 2010, 21:30
Hi,
I have the same problem as
http://www.qtcentre.org/threads/10504-QFileDialog
Too bad the solution doesn't seem to work for me.

I just want to add a widget to a QFileDialog. I tried many things like creating a QFileDialog instance and maniplulating it or its layout, to no effect. Now I even tried subclassing/inheriting and it doesn't give me an error, but changes nothing too:



class myofd(QtGui.QFileDialog):
def __init__(self,parent=None):
super(myofd, self).__init__(parent)
print "Columns: ",self.layout().columnCount()
print "Rows: ",self.layout().rowCount()
print "At 3-0: ",self.layout().itemAtPosition(3,0)
self.layout().removeItem(self.layout().itemAtPosit ion(3,0))
cmbo = QtGui.QComboBox()
cmbo.addItem("MyChoice1")
cmbo.addItem("MyChoice2")
self.layout().addWidget(cmbo,4,0)
self.layout().update()
self.layout().activate()
print "Columns: ",self.layout().columnCount()
print "Rows: ",self.layout().rowCount()
print "At 4-0: ",self.layout().itemAtPosition(4,0)

myloadinst = myofd()
myloadinst.getOpenFileName(self,"Open log file ...","","")


The row count goes up, the ".itemAtPosition" function works, no errors are thrown... only the dialog doesn't contain the new widget.

I've also tried the QLayout functions addChildWidget and addLayout and many more... also QFileDialog methods... sometimes I get a Runtime error:



self.layout().addChildWidget(cmbo)
RuntimeError: no access to protected functions or signals for objects not created from Python


which makes me think there may be some sip/c++/qt protection mechanisms which I don't know.

Am I doing sth wrong? Is there a better way to do this? I hope I don't have to make a file dialog from scratch because QFileDialog is somehow immutable...

Regards,
Xaver

xaverxn
28th October 2010, 20:58
Actually, might it be a problem that the QLayout object was created by C(++)? Wouldn't that mean any manipulation is impossible?
Because if I translate this: http://developer.qt.nokia.com/faq/answer/how_can_i_add_widgets_to_my_qfiledialog_instance line-by-line, it's not working either:


import sys
from PyQt4 import QtCore,QtGui

app = QtGui.QApplication(sys.argv)
box = QtGui.QFileDialog()
button = QtGui.QPushButton(box)
button.setText("New button")
layout = box.layout()
layout.addWidget(button, 0, 0)
box.show()
sys.exit(app.exec_())

(Actually, is line 8 correct? I don't speak C++)

Regards, Xaver