0

I am trying to force the user to fill the QPlainTextEdit, and then click the button. Further, I wanna prevent the user to click outside the QMainWindow and avoid filling the text. In essence, the user is forced to enter the text and click the button.

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.window.setWindowFlag(Qt.WindowStaysOnTopHint)
        
        self.label = QLabel()
        self.label.setText("Write Something.")
        self.label.setAlignment(Qt.AlignCenter)
        self.text_area = QPlainTextEdit(self)
        self.button = QPushButton("Save",self)
        self.button.setDisabled(True)
        self.text_area.textChanged.connect(self.enable_button)

        self.button.clicked.connect(self.store_message)
        self.vbox = QVBoxLayout()
        self.vbox.addWidget(self.label)
        self.vbox.addStretch()
        self.vbox.addWidget(self.text_area)
        self.vbox.addWidget(self.button)
        # self.vbox.addStretch()
        self.window.setLayout(self.vbox)
        self.window.show()
    def enable_button(self):
        if self.text_area.toPlainText() != "":
            self.button.setEnabled(True)
        else:
            self.button.setDisabled(True)
    def store_message(self):
        ## do something
        print("Saving..")

Thanks in advance.

Lorenzo
  • 13
  • 3
  • By "not allowed to leave the application" do you mean that you want to prevent the user to close the window if no text has been entered? – musicamante Aug 13 '22 at 15:56
  • @musicamante Yes, exactly! – Lorenzo Aug 13 '22 at 16:03
  • Override the [`closeEvent()`](https://doc.qt.io/qt-5/qwidget.html#closeEvent) and ignore the event if the text field has no text (`if not self.text_area.toPlainText():` `event.ignore()`) – musicamante Aug 13 '22 at 16:16
  • Under that circumstance, the user can ignore the message, and he potentially can focus on another window. Essentially, I would like to force the user to answer. – Lorenzo Aug 13 '22 at 16:22
  • What message? Also, you cannot completely prevent changing focus on another window of a different program; if you're talking about another window in *your* program then that's a completely different thing than what you're asking, and it can be done by using a QDialog (while providing a proper parent window) either by calling `exec()` or `open()`. – musicamante Aug 13 '22 at 16:28
  • Hmm, you can see many programs, especially on MS Windows doing what I am asking. – Lorenzo Aug 13 '22 at 16:34
  • 1
    Asking what? You're asking two **very different** questions. Preventing the user to close a window or to change focus are very different things. Also, you never clarified if you want to prevent changing focus to a window of *your* program or another one (which is possible, but generally discouraged and not impossible to avoid), but, as said, this is not what you initially asked. So, please, take your time and [edit] your question to clarify exactly what you want to do in a detailed way. – musicamante Aug 13 '22 at 17:16

0 Answers0