Forum > QT

Qt colors

<< < (2/2)

dbannon:

--- Quote from: zeljko on March 12, 2023, 09:58:20 am ---You can read theme colors from QApplication_palette(), read docs about QPalette.
Also, you can create your own QPalette, set your colors and set it as application palette.

--- End quote ---

Yep, that was good advice and I should have taken it way back then.

zeljko, I have something working fine with QPalette but don't understand whats happening with memory management. Simplified version of my code -


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function GetWindowQTColor() : TColor;var  PaletteH : QPaletteH;  ABrush : QBrushH;  APQColor : PQColor;begin  PaletteH := QPalette_Create;          // https://doc.qt.io/qt-5/qpalette.html  QGuiApplication_palette(PaletteH);    // https://doc.qt.io/qt-5/qguiapplication.html  ABrush := QPalette_Window(PaletteH);  APQColor := QBrush_color(ABrush);  Result := RGBtoColor(APQColor^.r div 256, APQColor^.g div 256, APQColor^.b div 256);//  PaletteH.free;                      // NO, don't do this.  QPalette_destroy(PaletteH);  // zeljko says (below) do this and it works !end;           

I found  QGuiApplication_palette(PaletteH) must have PaletteH created (or I get a segV).  OK, said I, if I call create(), I must call .free(), nope, SegV again. So, it works as expected. But no memory leaks, I call QPalette_Create;  and do not free it. And if I call it multiple times, the memory consumption does not increase.

Is Qt doing some memory management below my line of sight ?                     

The above works perfectly, no memory leaks and returns the stuff I want. But I feel I am breaking an important rule !

Thoughts ?

Davo

EDIT: added scale to r, g and b values.  Added zeljko's suggestion on how to free PaletteH, all good now !

zeljko:
No, it does not. You must free it. Check with valgrind and you'll see.
Edit: Forgot to write : You must use QPalette_destroy(PaletteH);

dbannon:
Ah, thank you zeljko, yep, that works perfectly !  Don't think I would have guessed that !

Now my code works as expected. Beautiful. Works fine in Qt6 too. I will edit the source above in case someone else is interested.

Davo

Navigation

[0] Message Index

[*] Previous page

Go to full version