1

i need to popup a dialog, but everytime when the dialog popup, there is certain button is always checked, no matter how i set it's attribute before start up that always is checked, can anyone tell me reason?

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class Demo(QDialog):
    def __init__(self):
        super().__init__()
        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        self.export_bn = QPushButton('export')
        self.search_bn = QPushButton('search')
        layout.addStretch(1)
        layout.addWidget(self.export_bn)
        layout.addWidget(self.search_bn)

        main_layout = QVBoxLayout()
        main_layout.addLayout(layout)
        main_layout.addWidget(QTextEdit('Test'))
        self.setLayout(main_layout)

        #unchecked this button not working when start a window
        self.export_bn.setChecked(False)


app = QApplication([])
demo = Demo()
demo.show()
app.exec()

The export button is checked, i want to no button checked when dialog popup.

"sample"

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
jett chen
  • 1,067
  • 16
  • 33

1 Answers1

2

This looks like auto-default button behavior.

From the documentation for the autoDefault property:

In some GUI styles a default button is drawn with an extra frame around it, up to 3 pixels or more.

and

This property's default is true for buttons that have a QDialog parent; otherwise it defaults to false.

The QDialog automatically becomes the buttons' parent when you call setLayout, so the autoDefault property will be True for both buttons.

So, instead of using self.export_bn.setChecked(False), you can do this:

self.export_bn.setAutoDefault(False)
self.search_bn.setAutoDefault(False)
djvg
  • 11,722
  • 5
  • 72
  • 103