2017-04-30 10 views
0

이 앱은 기본 Drag'n'Drop 프로그램이며 삭제 된 .html 파일을 처리해야합니다.이벤트는 Pyqt의 버튼 상자를 사용하여 두 번 발생합니다.

main.py

from PyQt4 import QtGui 
import sys 
import design, os 

class MyTextEdit(QtGui.QTextEdit): 

    def __init__(self, parent): 
     super(MyTextEdit, self).__init__(parent) 
     self.setAcceptDrops(True) 
     self.list_of_dropped_files = [] 

    def dragEnterEvent(self, event): 
     if event.mimeData().hasUrls(): 
      event.accept() 
     else: 
      event.ignore() 

    def dragMoveEvent(self, event): 
     event.accept() 

    def dropEvent(self, event): 

     self.clear() 

     self.list_of_dropped_files = event.mimeData().urls() 

     for single_file in self.list_of_dropped_files: 
      self.append(single_file.toLocalFile()) 


class DialogInit(QtGui.QDialog, design.Ui_Dialog): 
    def __init__(self): 
     super(self.__class__, self).__init__() 
     self.setupUi(self) 

     self.buttonBox.accepted.connect(self.accept) # handle the ok button click 
     self.textEditHandler = MyTextEdit(self.textEdit) 

    def accept(self): 
     print self.textEditHandler.list_of_dropped_files 


def main(): 
    app = QtGui.QApplication(sys.argv) 
    form = DialogInit()     
    form.show()       
    app.exec_()       


if __name__ == '__main__':    
    main()        

나는 또한 다음과 같이처럼 보이는 QtDesigner를 사용하여 design.py 코드를 생성 :이 코드를 작성했습니다 지금까지

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

# Form implementation generated from reading ui file 'untitled.ui' 
# 
# Created by: PyQt4 UI code generator 4.11.4 
# 
# WARNING! All changes made in this file will be lost! 

from PyQt4 import QtCore, QtGui 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    def _fromUtf8(s): 
     return s 

try: 
    _encoding = QtGui.QApplication.UnicodeUTF8 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig, _encoding) 
except AttributeError: 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig) 

class Ui_Dialog(object): 
    def setupUi(self, Dialog): 
     Dialog.setObjectName(_fromUtf8("Dialog")) 
     Dialog.resize(409, 80) 
     self.verticalLayoutWidget = QtGui.QWidget(Dialog) 
     self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 401, 71)) 
     self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget")) 
     self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget) 
     self.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) 
     self.horizontalLayout = QtGui.QHBoxLayout() 
     self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) 
     self.label = QtGui.QLabel(self.verticalLayoutWidget) 
     font = QtGui.QFont() 
     font.setPointSize(12) 
     self.label.setFont(font) 
     self.label.setObjectName(_fromUtf8("label")) 
     self.horizontalLayout.addWidget(self.label) 
     self.textEdit = QtGui.QTextEdit(self.verticalLayoutWidget) 
     self.textEdit.setAcceptDrops(True) 
     self.textEdit.setObjectName(_fromUtf8("textEdit")) 
     self.horizontalLayout.addWidget(self.textEdit) 
     self.verticalLayout.addLayout(self.horizontalLayout) 
     self.buttonBox = QtGui.QDialogButtonBox(self.verticalLayoutWidget) 
     self.buttonBox.setOrientation(QtCore.Qt.Horizontal) 
     self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) 
     self.buttonBox.setObjectName(_fromUtf8("buttonBox")) 
     self.verticalLayout.addWidget(self.buttonBox) 

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

    def retranslateUi(self, Dialog): 
     Dialog.setWindowTitle(_translate("Dialog", "Dialog", None)) 
     self.label.setText(_translate("Dialog", "Drop .html file(s):", None)) 

그러나 단추 상자에서 확인 단추를 누르면 이벤트와 함수 accept()가 두 번 실행됩니다.

QTextEdit에 여러 파일을 놓은 후에 위젯이 높이를 변경하지 않거나 적어도 스크롤 창을 자동으로 추가한다는 사실을 발견했습니다.

두 가지 문제를 해결하는 방법에 대한 아이디어가 있으십니까?

제 코드가 엉망이되어 버렸다고 생각합니다. 작동하지만 실제로 프로그래밍되지 않았습니다.

코드를 리팩토링하는 방법에 대해 조언을 제공해 줄 수 있습니까? 나는 무엇을 더 잘할 수 있었 을까?

감사합니다.

+0

상속 된 QDialog.accept와 충돌 할 때 DialogInit.accept의 이름을 변경하십시오. –

+0

감사! 그게 효과가 있었지만 이제 Ok 버튼을 누르면 대화 상자가 사라집니다. 왜 이런 일이 생길까요? –

답변

0

나는 ok 버튼을 클릭하면 QDialog 문제를 해결할 수있는 해결책을 찾았다. design.py에서 accept() 슬롯을 제거해야했습니다.

그러나 여러 파일이 삭제 된 경우 스크롤바가 추가되지 않습니다.