how to change default "ok" and "cancel" button.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Taylor. Snow
Posts: 22
Joined: Wed Dec 11, 2019 6:43 am

how to change default "ok" and "cancel" button.

Post by Taylor. Snow »

I create a widget and contorl by FreeCAD. I want to leave the close button, not the ok button.
for example:

Code: Select all

from PySide import QtCore, QtGui
box = QtGui.QMessageBox(QtGui.QMessageBox.Warning,"123","love")
FreeCADGui.Control.showDialog(box)
widget.png
widget.png (12.66 KiB) Viewed 962 times
UR_
Veteran
Posts: 1355
Joined: Tue Jan 03, 2017 8:42 pm

Re: how to change default "ok" and "cancel" button.

Post by UR_ »

Please have a look at this:

Screenshot 004.png
Screenshot 004.png (7.27 KiB) Viewed 941 times

Code: Select all

from PySide import QtCore, QtGui

def Macro_Main ():

    msgBox = QtGui.QMessageBox()
    msgBox.setIcon(QtGui.QMessageBox.Question)
    msgBox.setText(u"Some information")
    ProcessButton = msgBox.addButton(u"Process this!", QtGui.QMessageBox.AcceptRole)
    SkipButton = msgBox.addButton(u"Skip it!", QtGui.QMessageBox.AcceptRole)
    CloseButton = msgBox.addButton(QtGui.QMessageBox.Close)
    msgBox.exec_()
    
    if msgBox.clickedButton() == CloseButton:
      print ("action closed")
      return
      #
      #
      #
    elif msgBox.clickedButton() == ProcessButton:
      print ("some processing is done now")
      #
      #
      #
    elif msgBox.clickedButton() == SkipButton:
      print ("we are skipping now")
      #
      #
      #
    else:
      # should never be executed
      print ("internal error") 
      return

Macro_Main ()
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: how to change default "ok" and "cancel" button.

Post by Kunda1 »

#documentation
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Taylor. Snow
Posts: 22
Joined: Wed Dec 11, 2019 6:43 am

Re: how to change default "ok" and "cancel" button.

Post by Taylor. Snow »

UR_ wrote: Thu Mar 12, 2020 7:29 am Please have a look at this:


Screenshot 004.png

Code: Select all

from PySide import QtCore, QtGui

def Macro_Main ():

    msgBox = QtGui.QMessageBox()
    msgBox.setIcon(QtGui.QMessageBox.Question)
    msgBox.setText(u"Some information")
    ProcessButton = msgBox.addButton(u"Process this!", QtGui.QMessageBox.AcceptRole)
    SkipButton = msgBox.addButton(u"Skip it!", QtGui.QMessageBox.AcceptRole)
    CloseButton = msgBox.addButton(QtGui.QMessageBox.Close)
    msgBox.exec_()
    
    if msgBox.clickedButton() == CloseButton:
      print ("action closed")
      return
      #
      #
      #
    elif msgBox.clickedButton() == ProcessButton:
      print ("some processing is done now")
      #
      #
      #
    elif msgBox.clickedButton() == SkipButton:
      print ("we are skipping now")
      #
      #
      #
    else:
      # should never be executed
      print ("internal error") 
      return

Macro_Main ()
first , thank you for you offer this method, i found it not show in "Combo View". so i want to keep in "Combo View", by using FreeCADGui.Control.showDialog(),but i can't change it default "ok"button
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: how to change default "ok" and "cancel" button.

Post by DeepSOIC »

add something like this to your task dialog class:

Code: Select all

class MyTaskDialog(object):
    ...
    def getStandardButtons(self):
        return int(QtGui.QDialogButtonBox.Ok) | int(QtGui.QDialogButtonBox.Cancel)| int(QtGui.QDialogButtonBox.Apply)
I am not aware of any method to add a custom button other than adding them explicitly to your task widget, and making getStandardButtons return 0. There may very well be.
Post Reply