Is it possible to add QPushButton to QFileDialog?

330 views
Skip to first unread message

Adam Baker

unread,
Nov 14, 2018, 10:58:03 PM11/14/18
to Python Programming for Autodesk Maya


Hey all,

I am trying to add a QPushButton or any other QWidget to a QFileDialog.

Here is where I currently am:

from PySide2 import QtWidgets, QtCore, QtGui
class fileOpen(QtWidgets.QFileDialog):
    def __init__(self):
        super(fileOpen, self).__init__()
        self.setWindowTitle("ATK Open File")
        self.setObjectName('ATKOpenFile')
        self.buildUI()
    def buildUI(self):
        btnLayout = QtWidgets.QHBoxLayout()
        self.setLayout(btnLayout)
        btn = QtWidgets.QPushButton('Click')
        btnLayout.addWidget(btn)

def showUI():
    ui = fileOpen()
    ui.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
    ui.show()
    return ui


The problem is that the button now takes up the while window and the QFileDialog gets pushed to the upper corner collapsing on it's self.

Thanks
Adam

Michael Boon

unread,
Nov 15, 2018, 12:31:41 AM11/15/18
to Python Programming for Autodesk Maya
I don't have it in front of me, but I think you need to use the existing layout instead of creating a new one. So in your buildUI, you need to call self.layout(), or self.centralWidget().layout() or something like that. Then examine that and figure out where you want to put your button in it.

Justin Israel

unread,
Nov 15, 2018, 12:35:19 AM11/15/18
to python_in...@googlegroups.com
QFileDialog is a composite widget, which already has its own layout and children. When you call setLayout, you are replacing the existing one with your own. This causes all of the existing widgets to lose their layout constraints. If you want to add more widgets, then you will have to work with the existing layout and child widgets. What kind of arrangement are you looking for with your new push button? There may be a better way to do it instead of messing with the layout, such as modifying the QDialogButtonBox:

buttonBox = self.findChild(QtWidgets.QDialogButtonBox)
# add button and wire up signal to something

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/1633c52d-e9ce-4b4c-b09b-d4298883baf4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Adam Baker

unread,
Nov 15, 2018, 9:23:07 AM11/15/18
to Python Programming for Autodesk Maya
I tried looking for the child and I keep getting None so I can’t add that layout to another layout or add a button to its layout. I am not just trying to adding a QPushButton but also add a QTextEdit to show the user more information about the file. My idea to add all the UI elements I want was to get the layout of the QfileDialog add that to a HBoxlayout. This will allow me to add UI elements off to the right of the QFileDialog’s layout.

Thanks,
- Adam

Tim Fowler

unread,
Nov 15, 2018, 11:12:43 AM11/15/18
to python_in...@googlegroups.com
I think that is exactly what Maya's file dialog does.  Derives from QFileDialog, grabs its main layout, then adds all the options UI to the end of the HBoxLayout.  I'm not 100% sure, but I think just calling QWidget::layout (from the derived parent class of the dialog) gives you the hbox that you're looking for to add something off to the right.  To edit other things you might have to do a "findChildren" with a specific name.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

aba...@spinvfx.com

unread,
Nov 15, 2018, 8:25:27 PM11/15/18
to Python Programming for Autodesk Maya
This is my latest method that seams to be working. The only problem I have found is that I am unable to remove the size control form the QFileDialog. I am going to continue to try and make it function more like the maya open file with the Sizeable window on the right side.

https://pastebin.com/DzemqXNc

Please let me know if you have any more ideas.

- Adam B.

Justin Israel

unread,
Nov 15, 2018, 9:19:49 PM11/15/18
to python_in...@googlegroups.com
My only suggestion for that code is that you probably don't need a QMainWindow, since you aren't even setting the setCentralWidget. You could just make it a QWidget or QDialog that nests the QFileDialog. If you made it a QDialog, then it would be easy to wire up the QFileDialog signals to it.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Adam Baker

unread,
Nov 16, 2018, 2:20:55 AM11/16/18
to Python Programming for Autodesk Maya
I was testing my code I posted in OSX and notices that its not behaving the same as on Linux. It will create two separate windows one for the FileDalog and another for the pushbutton. Would anyone have a clue why it would do this ?

- Adam 
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Nov 16, 2018, 6:00:50 AM11/16/18
to python_in...@googlegroups.com
Again, I would suggest not using a QMainWindow here, because it requires that you do setCentralWidget as opposed to setting a custom layout. 

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/dc53b860-3b14-48dd-a269-91f2465ec2b6%40googlegroups.com.

Adam Baker

unread,
Nov 16, 2018, 9:41:39 AM11/16/18
to Python Programming for Autodesk Maya
Hey Justin,

I am using a QWidget.QDialog here. I named the class “MainWindow” so I think your getting it mixed up. Sorry for that should have been more clear with my class names.

Any other ideas why I’d have this problem on OSX ? Haven’t tested with Windows yet.

-Adam

Justin Israel

unread,
Nov 16, 2018, 11:42:53 PM11/16/18
to python_in...@googlegroups.com
Oh sorry about that. I was reading it wrong multiple times :-)
For me, your example mostly works as expected on linux under PySide 1.x. But the reject signal is still mapped to the QFileDialog so when you hit ESC, the QFileDialog disappears and leaves behind your button and main widget. On PySide 2.x however, it completely doesn't work and pops up two individual widgets. It seems in Qt5 the QFileDialog is doing some late layout at the time its actually shown, so the child layouts and widgets aren't available yet. 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Adam Baker

unread,
Nov 17, 2018, 12:05:45 AM11/17/18
to Python Programming for Autodesk Maya
Ahah no worries. After all this info do you have any suggestions on how I can edit the QFileDialog and add the extra widgets in on the right hand side?

- Adam B. 

On Friday, 16 November 2018 23:42:53 UTC-5, Justin Israel wrote:
Oh sorry about that. I was reading it wrong multiple times :-)
For me, your example mostly works as expected on linux under PySide 1.x. But the reject signal is still mapped to the QFileDialog so when you hit ESC, the QFileDialog disappears and leaves behind your button and main widget. On PySide 2.x however, it completely doesn't work and pops up two individual widgets. It seems in Qt5 the QFileDialog is doing some late layout at the time its actually shown, so the child layouts and widgets aren't available yet. 

On Sat, Nov 17, 2018 at 3:41 AM Adam Baker <aj...@sympatico.ca> wrote:
Hey Justin,

I am using a QWidget.QDialog here. I named the class “MainWindow” so I think your getting it mixed up. Sorry for that should have been more clear with my class names.

Any other ideas why I’d have this problem on OSX ? Haven’t tested with Windows yet.

-Adam

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Nov 17, 2018, 12:18:41 AM11/17/18
to python_in...@googlegroups.com
On Sat, Nov 17, 2018 at 6:05 PM Adam Baker <aj...@sympatico.ca> wrote:
Ahah no worries. After all this info do you have any suggestions on how I can edit the QFileDialog and add the extra widgets in on the right hand side?

I don't really have any suggestions since I see it works differently in PySide 1.x vs 2.x. The QFileDialog relies on system native calls so it would be difficult I think to completely customise the existing one through Qt.
 

- Adam B. 

On Friday, 16 November 2018 23:42:53 UTC-5, Justin Israel wrote:
Oh sorry about that. I was reading it wrong multiple times :-)
For me, your example mostly works as expected on linux under PySide 1.x. But the reject signal is still mapped to the QFileDialog so when you hit ESC, the QFileDialog disappears and leaves behind your button and main widget. On PySide 2.x however, it completely doesn't work and pops up two individual widgets. It seems in Qt5 the QFileDialog is doing some late layout at the time its actually shown, so the child layouts and widgets aren't available yet. 

On Sat, Nov 17, 2018 at 3:41 AM Adam Baker <aj...@sympatico.ca> wrote:
Hey Justin,

I am using a QWidget.QDialog here. I named the class “MainWindow” so I think your getting it mixed up. Sorry for that should have been more clear with my class names.

Any other ideas why I’d have this problem on OSX ? Haven’t tested with Windows yet.

-Adam

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a56eda2e-f966-423e-a50c-3a1136a737a1%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages