カスタムダイアログの作成

2016/08/15

Python2.7.6, PySide1.2.4

# -*- coding: utf-8 -*-

from PySide import QtCore,QtGui

class Window(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.btn = QtGui.QPushButton(self,text='Open')
        self.btn.clicked.connect(self.openDialog)

    def openDialog(self):
        dialog = MyDialog(self)
        result = dialog.exec_()
        if result == True:
            self.statusBar().showMessage(u'右が押されました')
        else:
            self.statusBar().showMessage(u'左が押されました')

class MyDialog(QtGui.QDialog):
    def __init__(self,parent=None):
        super(MyDialog, self).__init__(parent)
        self.setWindowTitle(u'ダイアログ上でクリックして下さい')

    def mousePressEvent(self,event):
        if event.button() == QtCore.Qt.RightButton:
            self.accept()

        if event.button() == QtCore.Qt.LeftButton:
            self.reject()

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

dialog.exec_()に対してdialog.accept() dialog.reject() で返す。

UI適用サンプル

# -*- coding: utf-8 -*-

from PySide import QtCore,QtGui

import window_ui
import dialog_ui

class Window(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.ui = window_ui.Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.pushButton.clicked.connect(self.openDialog)

    def openDialog(self):
        dialog = MyDialog(self)
        result = dialog.exec_()
        if result == True:
            self.ui.lineEdit.setText(dialog.ui.lineEdit.text())
            self.statusBar().showMessage(u'OKが押されました')
        else:
            self.statusBar().showMessage(u'Cancelが押されました')

class MyDialog(QtGui.QDialog):
    def __init__(self,parent=None):
        super(MyDialog, self).__init__(parent)

        self.ui = dialog_ui.Ui_Dialog()
        self.ui.setupUi(self)



if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()

    window.show()
    sys.exit(app.exec_())


window_ui.py

from PySide import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(247, 64)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(10, 10, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(130, 10, 91, 23))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "ダイアログを開く", None, QtGui.QApplication.UnicodeUTF8))


dialog_ui.py

from PySide import QtCore, QtGui

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(237, 102)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(30, 50, 161, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(20, 10, 181, 20))
        self.lineEdit.setObjectName("lineEdit")

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))