qmessagebox.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtWidgets module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QMESSAGEBOX_H
  40. #define QMESSAGEBOX_H
  41. #include <QtWidgets/qdialog.h>
  42. QT_BEGIN_NAMESPACE
  43. #ifndef QT_NO_MESSAGEBOX
  44. class QLabel;
  45. class QMessageBoxPrivate;
  46. class QAbstractButton;
  47. class QCheckBox;
  48. class Q_WIDGETS_EXPORT QMessageBox : public QDialog
  49. {
  50. Q_OBJECT
  51. Q_FLAGS(StandardButtons)
  52. Q_PROPERTY(QString text READ text WRITE setText)
  53. Q_PROPERTY(Icon icon READ icon WRITE setIcon)
  54. Q_PROPERTY(QPixmap iconPixmap READ iconPixmap WRITE setIconPixmap)
  55. Q_PROPERTY(Qt::TextFormat textFormat READ textFormat WRITE setTextFormat)
  56. Q_PROPERTY(StandardButtons standardButtons READ standardButtons WRITE setStandardButtons)
  57. #ifndef QT_NO_TEXTEDIT
  58. Q_PROPERTY(QString detailedText READ detailedText WRITE setDetailedText)
  59. #endif
  60. Q_PROPERTY(QString informativeText READ informativeText WRITE setInformativeText)
  61. Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags)
  62. public:
  63. enum Icon {
  64. // keep this in sync with QMessageDialogOptions::Icon
  65. NoIcon = 0,
  66. Information = 1,
  67. Warning = 2,
  68. Critical = 3,
  69. Question = 4
  70. };
  71. Q_ENUM(Icon)
  72. enum ButtonRole {
  73. // keep this in sync with QDialogButtonBox::ButtonRole and QPlatformDialogHelper::ButtonRole
  74. InvalidRole = -1,
  75. AcceptRole,
  76. RejectRole,
  77. DestructiveRole,
  78. ActionRole,
  79. HelpRole,
  80. YesRole,
  81. NoRole,
  82. ResetRole,
  83. ApplyRole,
  84. NRoles
  85. };
  86. enum StandardButton {
  87. // keep this in sync with QDialogButtonBox::StandardButton and QPlatformDialogHelper::StandardButton
  88. NoButton = 0x00000000,
  89. Ok = 0x00000400,
  90. Save = 0x00000800,
  91. SaveAll = 0x00001000,
  92. Open = 0x00002000,
  93. Yes = 0x00004000,
  94. YesToAll = 0x00008000,
  95. No = 0x00010000,
  96. NoToAll = 0x00020000,
  97. Abort = 0x00040000,
  98. Retry = 0x00080000,
  99. Ignore = 0x00100000,
  100. Close = 0x00200000,
  101. Cancel = 0x00400000,
  102. Discard = 0x00800000,
  103. Help = 0x01000000,
  104. Apply = 0x02000000,
  105. Reset = 0x04000000,
  106. RestoreDefaults = 0x08000000,
  107. FirstButton = Ok, // internal
  108. LastButton = RestoreDefaults, // internal
  109. YesAll = YesToAll, // obsolete
  110. NoAll = NoToAll, // obsolete
  111. Default = 0x00000100, // obsolete
  112. Escape = 0x00000200, // obsolete
  113. FlagMask = 0x00000300, // obsolete
  114. ButtonMask = ~FlagMask // obsolete
  115. };
  116. typedef StandardButton Button; // obsolete
  117. Q_DECLARE_FLAGS(StandardButtons, StandardButton)
  118. explicit QMessageBox(QWidget *parent = Q_NULLPTR);
  119. QMessageBox(Icon icon, const QString &title, const QString &text,
  120. StandardButtons buttons = NoButton, QWidget *parent = Q_NULLPTR,
  121. Qt::WindowFlags flags = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
  122. ~QMessageBox();
  123. void addButton(QAbstractButton *button, ButtonRole role);
  124. QPushButton *addButton(const QString &text, ButtonRole role);
  125. QPushButton *addButton(StandardButton button);
  126. void removeButton(QAbstractButton *button);
  127. #ifdef Q_OS_WINCE
  128. void setVisible(bool visible);
  129. #endif
  130. using QDialog::open;
  131. void open(QObject *receiver, const char *member);
  132. QList<QAbstractButton *> buttons() const;
  133. ButtonRole buttonRole(QAbstractButton *button) const;
  134. void setStandardButtons(StandardButtons buttons);
  135. StandardButtons standardButtons() const;
  136. StandardButton standardButton(QAbstractButton *button) const;
  137. QAbstractButton *button(StandardButton which) const;
  138. QPushButton *defaultButton() const;
  139. void setDefaultButton(QPushButton *button);
  140. void setDefaultButton(StandardButton button);
  141. QAbstractButton *escapeButton() const;
  142. void setEscapeButton(QAbstractButton *button);
  143. void setEscapeButton(StandardButton button);
  144. QAbstractButton *clickedButton() const;
  145. QString text() const;
  146. void setText(const QString &text);
  147. Icon icon() const;
  148. void setIcon(Icon);
  149. QPixmap iconPixmap() const;
  150. void setIconPixmap(const QPixmap &pixmap);
  151. Qt::TextFormat textFormat() const;
  152. void setTextFormat(Qt::TextFormat format);
  153. void setTextInteractionFlags(Qt::TextInteractionFlags flags);
  154. Qt::TextInteractionFlags textInteractionFlags() const;
  155. void setCheckBox(QCheckBox *cb);
  156. QCheckBox* checkBox() const;
  157. static StandardButton information(QWidget *parent, const QString &title,
  158. const QString &text, StandardButtons buttons = Ok,
  159. StandardButton defaultButton = NoButton);
  160. static StandardButton question(QWidget *parent, const QString &title,
  161. const QString &text, StandardButtons buttons = StandardButtons(Yes | No),
  162. StandardButton defaultButton = NoButton);
  163. static StandardButton warning(QWidget *parent, const QString &title,
  164. const QString &text, StandardButtons buttons = Ok,
  165. StandardButton defaultButton = NoButton);
  166. static StandardButton critical(QWidget *parent, const QString &title,
  167. const QString &text, StandardButtons buttons = Ok,
  168. StandardButton defaultButton = NoButton);
  169. static void about(QWidget *parent, const QString &title, const QString &text);
  170. static void aboutQt(QWidget *parent, const QString &title = QString());
  171. // the following functions are obsolete:
  172. QMessageBox(const QString &title, const QString &text, Icon icon,
  173. int button0, int button1, int button2,
  174. QWidget *parent = Q_NULLPTR,
  175. Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
  176. static int information(QWidget *parent, const QString &title,
  177. const QString& text,
  178. int button0, int button1 = 0, int button2 = 0);
  179. static int information(QWidget *parent, const QString &title,
  180. const QString& text,
  181. const QString& button0Text,
  182. const QString& button1Text = QString(),
  183. const QString& button2Text = QString(),
  184. int defaultButtonNumber = 0,
  185. int escapeButtonNumber = -1);
  186. inline static StandardButton information(QWidget *parent, const QString &title,
  187. const QString& text,
  188. StandardButton button0, StandardButton button1 = NoButton)
  189. { return information(parent, title, text, StandardButtons(button0), button1); }
  190. static int question(QWidget *parent, const QString &title,
  191. const QString& text,
  192. int button0, int button1 = 0, int button2 = 0);
  193. static int question(QWidget *parent, const QString &title,
  194. const QString& text,
  195. const QString& button0Text,
  196. const QString& button1Text = QString(),
  197. const QString& button2Text = QString(),
  198. int defaultButtonNumber = 0,
  199. int escapeButtonNumber = -1);
  200. inline static int question(QWidget *parent, const QString &title,
  201. const QString& text,
  202. StandardButton button0, StandardButton button1)
  203. { return question(parent, title, text, StandardButtons(button0), button1); }
  204. static int warning(QWidget *parent, const QString &title,
  205. const QString& text,
  206. int button0, int button1, int button2 = 0);
  207. static int warning(QWidget *parent, const QString &title,
  208. const QString& text,
  209. const QString& button0Text,
  210. const QString& button1Text = QString(),
  211. const QString& button2Text = QString(),
  212. int defaultButtonNumber = 0,
  213. int escapeButtonNumber = -1);
  214. inline static int warning(QWidget *parent, const QString &title,
  215. const QString& text,
  216. StandardButton button0, StandardButton button1)
  217. { return warning(parent, title, text, StandardButtons(button0), button1); }
  218. static int critical(QWidget *parent, const QString &title,
  219. const QString& text,
  220. int button0, int button1, int button2 = 0);
  221. static int critical(QWidget *parent, const QString &title,
  222. const QString& text,
  223. const QString& button0Text,
  224. const QString& button1Text = QString(),
  225. const QString& button2Text = QString(),
  226. int defaultButtonNumber = 0,
  227. int escapeButtonNumber = -1);
  228. inline static int critical(QWidget *parent, const QString &title,
  229. const QString& text,
  230. StandardButton button0, StandardButton button1)
  231. { return critical(parent, title, text, StandardButtons(button0), button1); }
  232. QString buttonText(int button) const;
  233. void setButtonText(int button, const QString &text);
  234. QString informativeText() const;
  235. void setInformativeText(const QString &text);
  236. #ifndef QT_NO_TEXTEDIT
  237. QString detailedText() const;
  238. void setDetailedText(const QString &text);
  239. #endif
  240. void setWindowTitle(const QString &title);
  241. void setWindowModality(Qt::WindowModality windowModality);
  242. static QPixmap standardIcon(Icon icon);
  243. Q_SIGNALS:
  244. void buttonClicked(QAbstractButton *button);
  245. #ifdef Q_QDOC
  246. public Q_SLOTS:
  247. int exec();
  248. #endif
  249. protected:
  250. bool event(QEvent *e) Q_DECL_OVERRIDE;
  251. void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
  252. void showEvent(QShowEvent *event) Q_DECL_OVERRIDE;
  253. void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;
  254. void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
  255. void changeEvent(QEvent *event) Q_DECL_OVERRIDE;
  256. private:
  257. Q_PRIVATE_SLOT(d_func(), void _q_buttonClicked(QAbstractButton *))
  258. Q_PRIVATE_SLOT(d_func(), void _q_clicked(QPlatformDialogHelper::StandardButton, QPlatformDialogHelper::ButtonRole))
  259. Q_DISABLE_COPY(QMessageBox)
  260. Q_DECLARE_PRIVATE(QMessageBox)
  261. };
  262. Q_DECLARE_OPERATORS_FOR_FLAGS(QMessageBox::StandardButtons)
  263. #define QT_REQUIRE_VERSION(argc, argv, str) { QString s = QString::fromLatin1(str);\
  264. QString sq = QString::fromLatin1(qVersion()); \
  265. if ((sq.section(QChar::fromLatin1('.'),0,0).toInt()<<16)+\
  266. (sq.section(QChar::fromLatin1('.'),1,1).toInt()<<8)+\
  267. sq.section(QChar::fromLatin1('.'),2,2).toInt()<(s.section(QChar::fromLatin1('.'),0,0).toInt()<<16)+\
  268. (s.section(QChar::fromLatin1('.'),1,1).toInt()<<8)+\
  269. s.section(QChar::fromLatin1('.'),2,2).toInt()) { \
  270. if (!qApp){ \
  271. new QApplication(argc,argv); \
  272. } \
  273. QString s = QApplication::tr("Executable '%1' requires Qt "\
  274. "%2, found Qt %3.").arg(qAppName()).arg(QString::fromLatin1(\
  275. str)).arg(QString::fromLatin1(qVersion())); QMessageBox::critical(0, QApplication::tr(\
  276. "Incompatible Qt Library Error"), s, QMessageBox::Abort, 0); qFatal("%s", s.toLatin1().data()); }}
  277. #endif // QT_NO_MESSAGEBOX
  278. QT_END_NAMESPACE
  279. #endif // QMESSAGEBOX_H