Reputation: 15
One of the functions of my program is to create a file. When the given button is pressed, the user is prompted to enter a name for the file. If the file already exists, they are asked to enter a new name. If not, a QDialog pops up, where the user can input some data to be written in the file. However, I am having trouble with the QDialog accepted signal --> when the user presses "Ok" after inputting the data, nothing happens. Only after the process is repeated a second time, does the QDialog.accepted (in document.py) invoke the function to which it is connected. Please refer to my code below:
main.py
from PyQt5 import QtWidgets, QtCore
from gui import UiMainWindow
from document import Doc
import sys
class Logic(QtWidgets.QMainWindow, UiMainWindow, Doc):
def __init__(self):
super().__init__()
self.setupUi(self)
self.treeView = QtWidgets.QTreeView(self.centralwidget)
self.treeView.setGeometry(QtCore.QRect(270, 90, 801, 571))
self.button1.clicked.connect(self.make_document)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
logic = Logic()
logic.show()
sys.exit(app.exec_())
gui.py
from PyQt5 import QtWidgets, QtCore
class UiMainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("File Manager")
MainWindow.resize(1120, 750)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.button1 = QtWidgets.QPushButton(self.centralwidget)
self.button1.setGeometry(QtCore.QRect(10, 80, 121, 41))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("File Manager", "File Manager"))
self.button1.setText(_translate("MainWindow", "+ New File"))
document.py
from PyQt5.QtWidgets import *
from form import Form
import os
class Doc(QInputDialog):
"""Create an doc and populate it based on user input."""
def __init__(self, parent=None):
super().__init__(parent)
self.work_dir = os.path.join("C:\\", "Document Manager", "Work environment")
self.dialog = Form()
def make_document(self):
user_input = QInputDialog()
active = True
while active:
text, ok = user_input.getText(self, "Document name",
"Please enter a file name")
if ok and text != '':
self.name = text
output_path = f"{self.work_dir}" + "\\" + f"{self.name}" + ".pdf"
if not os.path.exists(output_path):
self.open_dialog()
self.dialog.accepted.connect(self.do_something)
active = False
else:
self.show_popup()
else:
active = False
def open_dialog(self):
self.dialog.exec_()
def do_something(self):
print("Doing something")
def show_popup(self):
"""Show a pop-up message if invoice name already exists"""
msg = QMessageBox()
msg.setWindowTitle("File/folder already exists.")
msg.setText("The document you are trying to create already exists.")
x = msg.exec_()
form.py
from PyQt5.QtWidgets import *
class Form(QDialog):
"""Show a pop-up form for the user to input file data."""
NumGridRows = 3
NumButtons = 4
def __init__(self, parent=None):
super().__init__(parent)
self.create_form()
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok |
QDialogButtonBox.Cancel, self)
self.button_box.accepted.connect(self.accept)
self.button_box.rejected.connect(self.reject)
main_layout = QVBoxLayout()
main_layout.addWidget(self.formbox)
main_layout.addWidget(self.button_box)
self.setLayout(main_layout)
self.setWindowTitle("Document data input")
def create_form(self):
self.formbox = QGroupBox("Please provide data below: ")
layout = QFormLayout()
layout.addRow(QLabel("Business name: "), QLineEdit())
layout.addRow(QLabel("Customer name: "), QLineEdit())
layout.addRow(QLabel("Customer email: "), QLineEdit())
self.formbox.setLayout(layout)
I have researched and tried various solutions, but to no avail. Any help would be truly appreciated!
Upvotes: 0
Views: 297