Macro MessageBox: Difference between revisions

From FreeCAD Documentation
(languages it)
(Use {{MacroCode}})
 
(20 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<languages/>
{{Macro|Name=MessageBox|Description=Show how to give information to the user in macros|Author=Gaël Ecorchard}}
<translate>
#! /usr/bin/env python
<!--T:1-->
# -*- coding: utf-8 -*-
{{Macro
|Name=MessageBox
|Icon=Macro MessageBox.png
|Description=Show how to give information to the user in macros.
|Author=Gaël Ecorchard
|Version=1.0
|Date=2011-09-19
|FCVersion=All
|Download=[https://www.freecadweb.org/wiki/images/7/7f/Macro_MessageBox.png ToolBar Icon]
}}

==Description== <!--T:2-->
Show how to give information to the user in macros

<!--T:3-->
[[File:Macro MessageBox 00.png|480px]]
{{Caption|MessageBox}}

==Script== <!--T:6-->
</translate>

ToolBar Icon [[Image:Macro_MessageBox.png]]

'''Macro_MessageBox.FCMacro'''

{{MacroCode|code=
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""Show how to give information to the user in macros
"""Show how to give information to the user in macros
"""
"""
from PyQt4 import QtCore, QtGui
from PySide import QtCore, QtGui
def errorDialog(msg):
def errorDialog(msg):
# Create a simple dialog QMessageBox
# Create a simple dialog QMessageBox
# The first argument indicates the icon used: one of QtGui.QMessageBox.{NoIcon, Information, Warning, Critical, Question}
# The first argument indicates the icon used: one of QtGui.QMessageBox.{NoIcon, Information, Warning, Critical, Question}
diag = QtGui.QMessageBox(QtGui.QMessageBox.Warning, 'Error in macro MessageBox', msg)
diag = QtGui.QMessageBox(QtGui.QMessageBox.Warning, 'Error in macro MessageBox', msg)
diag.setWindowModality(QtCore.Qt.ApplicationModal)
diag.setWindowModality(QtCore.Qt.ApplicationModal)
diag.exec_()
diag.exec_()
msg = 'Example of warning message'
msg = 'Example of warning message'
errorDialog(msg)
errorDialog(msg)
raise(Exception(msg))
raise(Exception(msg))
}}

<translate>

<!--T:4-->
In order to use the accented characters in the text field from '''Qt''', using the tag ''' #-*-coding: utf-8-*- ''' must be added a '''u''' before the message to display<br />
Example :
</translate>

{{MacroCode|code=
diag = QtGui.QMessageBox(QtGui.QMessageBox.Warning, u'Trop d'éléments désignés', msg)
...
...
msg = u'Élément sélectionnés affichés'
}}

<translate>

<!--T:5-->
To display multiple lines in a dialog box '''Qt''', must be added '''"\n"''' (quotation, valid also between apostrophes) between each line.<br />
Valid also ''' "\r\n"''' which correspond to '''CR''' carriage return, and '''LF''' end of line, valid also '''" \t"''' is a tab, characters should be between quotation marks (and apostrophes) as a character string, the tags can be found next to the text to display '''" \nRayon\t: "''', the tag '''" \ "''' (reversed slash) defines the command.<br />
Example :
</translate>

{{MacroCode|code=
diag = QtGui.QMessageBox(QtGui.QMessageBox.Information,u"Coordonnées",u"Coordonnée X : "+str(x)+"\r\n"+u"Coordonnée Y : "+str(y)+"\n"+u"Coordonnée Z :<br>
"+str(z)+"\nRayon\t : "+str(r))
}}


{{clear}}
{{languages | {{es|Macro_MessageBox/es}} {{it|Macro_MessageBox/it}} }}

Latest revision as of 23:29, 7 May 2020

MessageBox

Description
Show how to give information to the user in macros.

Macro version: 1.0
Last modified: 2011-09-19
FreeCAD version: All
Download: ToolBar Icon
Author: Gaël Ecorchard
Author
Gaël Ecorchard
Download
ToolBar Icon
Links
Macro Version
1.0
Date last modified
2011-09-19
FreeCAD Version(s)
All
Default shortcut
None
See also
None

Description

Show how to give information to the user in macros

MessageBox

Script

ToolBar Icon

Macro_MessageBox.FCMacro

#! /usr/bin/env python
# -*- coding: utf-8 -*-
 
"""Show how to give information to the user in macros
"""
from PySide import QtCore, QtGui
 
def errorDialog(msg):
    # Create a simple dialog QMessageBox
    # The first argument indicates the icon used: one of QtGui.QMessageBox.{NoIcon, Information, Warning, Critical, Question} 
    diag = QtGui.QMessageBox(QtGui.QMessageBox.Warning, 'Error in macro MessageBox', msg)
    diag.setWindowModality(QtCore.Qt.ApplicationModal)
    diag.exec_()
 
msg = 'Example of warning message'
errorDialog(msg)
raise(Exception(msg))


In order to use the accented characters in the text field from Qt, using the tag #-*-coding: utf-8-*- must be added a u before the message to display
Example :

diag = QtGui.QMessageBox(QtGui.QMessageBox.Warning, u'Trop d'éléments désignés', msg)
    ...
    ...
msg = u'Élément sélectionnés affichés'


To display multiple lines in a dialog box Qt, must be added "\n" (quotation, valid also between apostrophes) between each line.
Valid also "\r\n" which correspond to CR carriage return, and LF end of line, valid also " \t" is a tab, characters should be between quotation marks (and apostrophes) as a character string, the tags can be found next to the text to display " \nRayon\t: ", the tag " \ " (reversed slash) defines the command.
Example :

diag = QtGui.QMessageBox(QtGui.QMessageBox.Information,u"Coordonnées",u"Coordonnée X : "+str(x)+"\r\n"+u"Coordonnée Y : "+str(y)+"\n"+u"Coordonnée Z :<br>
 "+str(z)+"\nRayon\t     : "+str(r))