How do I make a button in PyQt5 to pick a color like here (Color Picker)?
QColorDialog? Or are you specifically looking for the triangle?
Yes. Dialog will let me choose color. Tough. I want the button to reflect what currently is selected
I found this:
Serves well my purpose.
I do this for the lazytexttool:
def openColorDialog(self, color):
colorDlg = QtWidgets.QColorDialog(self)
colorDlg.setOption(QtWidgets.QColorDialog.ShowAlphaChannel)
if self.currentColor:
colorDlg.setCurrentColor(QtGui.QColor(self.currentColor))
if colorDlg.exec_():
self.setColor(colorDlg.currentColor().name(),colorDlg.currentColor().alpha())
self.colorButton.setStyleSheet('background-color: %s; color: %s' % (
self.currentColor.name(),
'#fff' if self.currentColor.value() < 90 else '#000'
)
)
self.colorButton.setText(str(self.currentColor.alpha()))
It sets the color of the button via css, and also includes the alpha level
I make a simple empty widget(square) and then I place a module inside it with a paintEvent
and then use painter to make all the colors while handling the detection of the inputs alongside it.
https://doc.qt.io/qt-5/qpainter.html
For some reason setting background-color property for stylesheet of a button results in a color being lightened (desatureated a bit).
For instance here blue RGB(0, 0, 255) and green RGB(0, 255, 0).
Does anyone know what styling trick should be applied so that the color would be true?
That is probably because using a Q push button. A empty widget does not have that shading. And you can add the click into it also by hand.
Also style sheets are very reliable but can become a bit laggy if you make too much on it.
Try something like
def resizeEvent(self, event):
self.w = self.layout.widget.width()
self.h = self.layout.widget.height()
And then on the painter
painter.drawRec(0,0,self.w,self.h)
Thanks. I will give it a shot.
Try keeping the size in memory because asking any widget something causes lag, it is easier to have it handy with python after there is any change to it.
Try a QToolButton and/or make it flat.
Also, for UI design, I highly suggest QT 5 Designer as you can see the results without rebooting Krita.
Just putting things on the painter makes it more laggy. Do note painter is executed multiple times per second. So really the only things you want to put on painter is collection of events (cause events happen more often). For 1 time changes, it is better to do it outside of painter unless really necessary.
well yes that is why
if it is inside a module the paintEvent will not be constantly triggered as when it is outside, but will trigger only when there are changes to the widget or information sent in as there are less events happening. This means the paintEvent will stand still until further notice.