How can I prevent QDialog class from closing

后端 未结 3 824
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 16:09

How can I prevent QDialog class from closing after \"Ok\" button was pressed? I need to close the window only if the some actions were performed correctly on this dialog, in

3条回答
  •  佛祖请我去吃肉
    2021-01-18 16:57

    The same as from @kuba-ober, but using custom property. Change one function:

    void QDialogValidator::checkValidity(QLineEdit * l) {
      if (!l) return;
      QString text = l->text();
      int pos;
      bool isValid = l->validator()->validate(text, pos) == QValidator::Acceptable;
      setSelect(m_validWidgets, isValid, (QWidget*)l);
      if (!l->property("invalid").toBool() != isValid) {
        l->setProperty("invalid", !isValid);
        l->style()->unpolish(l);
        l->style()->polish(l);
      }
      emit newValidity(m_validWidgets.count() == m_needsValid);
    }
    

    Now styleSheet... functions are not necessary but we need to add constant style sheet for the dialog or application:

    d.setStyleSheet("*[invalid=\"true\"] { background: yellow }");
    

    Or if use Qt designer, add root widget property:

    
     *[invalid="true"] { border-style: solid; border-width: 1px; border-radius: 2px; border-color: yellow; }
    
    

提交回复
热议问题