PDA

View Full Version : Alternative to QWidget's "Promote to" feature?



Leutzig
31st October 2015, 20:52
Hi Forum,

I'm having this weird problem with my color picker that I will eventually use to control RGB LEDs. I found a color wheel (see link) for my project that I wanted to connect to my palette so that when I change the color wheel's current color, the palette will display that color (updatePalette is called from the color wheel's MouseMoveEvent function). But this only works when the color wheel is given its own window (using show() in main). Whenever I try using the QWidget's "Promote to" feature, the program instantly crashes when I move the color wheel cursor.

You can check out the attached pictures for further understanding. But I'm mostly interested in hearing if there are alternative ways to add this color wheel to my UI without having it as a separate window or without using the QWidget's "Promote to" feature?

Color wheel with colorwheel.cpp and colorwheel.h:
https://forum.qt.io/topic/19261/colorwheel

//Leutzig

anda_skoa
1st November 2015, 09:05
Your code and the code for colorwheel don't match.
The wheel widget has a constructor with only one argument, you are calling it with two.

So maybe you made a modification and you are calling the working constructor while the promote widget functionality calls the other one.

An alternative to the promote widget approach is to just have an empty widget in the UI and use it as a container/parent for the widget you then create in code.

Cheers,
_

Leutzig
1st November 2015, 12:29
Thanks for the reply,

Yes, I made a modification in the constructor to call the updatePalette with the color wheel color.

So in order to use an empty widget as a parent for the colorwheel, I have created a pointer to the widget and passed it in as a parent when I create an instance of the color wheel.

QWidget * ColorSelect::getColorWheelWidgetPtr()
{
QWidget *colorWheelWidgetPtr = ui->colorWheelWidget;
return colorWheelWidgetPtr;
}

And in my main:
ColorWheel *colorWheel = new ColorWheel(colorSelect->getColorWheelWidgetPtr(), colorSelect);
colorWheel->show();

This kind of works. But now I cannot see the color wheel, only the two cursors very close to each other. But the palette's color changes when I move the cursors so that's good. I'm not sure if the color wheel fills the empty widget right.

11488

anda_skoa
1st November 2015, 13:23
Yes, I made a modification in the constructor to call the updatePalette with the color wheel color.

Promote widget calls the constructor that takes only QWidget*, so if you don't have that anymore or it doesn't work after your modification, that could explain the observed behavior.
Should be easy enough to test creating the widget manually using that constructor instead of the code you posted earlier.



So in order to use an empty widget as a parent for the colorwheel, I have created a pointer to the widget and passed it in as a parent when I create an instance of the color wheel.

You could also just create color wheel inside the constructor of ColorSelect, but ok.



This kind of works. But now I cannot see the color wheel, only the two cursors very close to each other. But the palette's color changes when I move the cursors so that's good. I'm not sure if the color wheel fills the empty widget right.

You probably forgot to create a layout for the container or to add the color wheel instance to the layout.

Cheers,
_

Leutzig
1st November 2015, 17:56
I didn't create any layout so that might be the issue. I'm not certain on how to create one, but in ColorSelect's constructor I added:

auto layout = new QVBoxLayout();
layout->addWidget(ui->colorWheelWidget);

How and where do I add my instance "colorWheel" to the layout?

Thanks

anda_skoa
1st November 2015, 21:17
No, I am sure colorWheelWidget is already in a layout, right?
You need to create a layout inside colorWheelWidget and add the color wheel to that layout.



ColorWheel *colorWheel = new ColorWheel(ui->colorWheelWidget);

QVBoxLayout *layout = new QVBoxLayout(ui->colorWheelWidget);
layout->setMargins(0);
layout->addWidget(colorWheel);


Cheers,
_

Leutzig
1st November 2015, 23:40
Thanks a lot,
The color wheel is now shown perfectly fine. This is sort of my first application so I haven't really used layouts before.
But I've constrained the size of the "main widget" so the stuff in it aren't gonna move around.

//Leutzig