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

Need help, or want to share a macro? Post here!
Taylor. Snow
Posts: 21
Joined: Wed Dec 11, 2019 6:43 am

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

Postby Taylor. Snow » Thu Mar 12, 2020 1:41 am

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 190 times
UR_
Posts: 1189
Joined: Tue Jan 03, 2017 8:42 pm

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

Postby UR_ » Thu Mar 12, 2020 7:29 am

Please have a look at this:

Screenshot 004.png
Screenshot 004.png (7.27 KiB) Viewed 169 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
Posts: 7999
Joined: Thu Jan 05, 2017 9:03 pm

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

Postby Kunda1 » Thu Mar 12, 2020 2:41 pm

#documentation
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
Taylor. Snow
Posts: 21
Joined: Wed Dec 11, 2019 6:43 am

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

Postby Taylor. Snow » Thu Mar 12, 2020 3:28 pm

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
Posts: 7799
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

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

Postby DeepSOIC » Thu Mar 12, 2020 4:00 pm

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.