简体   繁体   中英

How to resize a QColorDialog

Is it possible to resize a QColorDialog ? I have been unable to get the window to resize appropriately. After the dialog is shown, it reverts to the default size.

An example:

import sys

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

class Window(QWidget):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()
        push_Button = QPushButton()
        layout.addWidget(push_Button)

        push_Button.clicked.connect(self.button)
        self.setLayout(layout)

    def button(self):
        color = QColorDialog(self)
        color.resize(100,100)
        print(color.size()) #Prints 100, 100
        color.show()
        print(color.size()) #Prints 551, 431

if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

The QColorDialog has a fixed size, because it contains several custom widgets which aren't designed to be resizable. It is possble to override these constraints and allow for manual resizing like this:

    color = QColorDialog(self)
    color.setSizeGripEnabled(True)
    color.layout().setSizeConstraint(QLayout.SetNoConstraint)
    color.show()

However, as you will see, the layout quickly becomes messed up with even a little bit of resizing. I also found that beyond a certain point, the dialog will actually crash due to floating point exceptions. So I think you will either have to accept it as it is, or perhaps write your own color dialog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM