PDA

View Full Version : Using a QFrame as a color selection indicator



KShots
19th March 2007, 20:37
Hello all,

I'm having a problem with using a QFrame as a color selection indicator. What I'm trying to do is to place a QFrame into my dialog via designer, set its palette's "Window" role to a color (it starts black), then a QPushButton will invoke a QColorDialog::getColor() to change the base color of the QFrame - the idea is to show the picked color on the parent dialog.

What I'm seeing when I run my application is that the QFrame is not visible (at all), and no matter what I change the color to (the base color is being stored in the invisible frame), I cannot see it.

Any ideas? I'm using Qt 4.2.2

jpn
19th March 2007, 21:28
Check src/gui/dialogs/qcolordialog.cpp. There are many interesting widgets like QColorShowLabel, QColorPicker and QColSpinBox. ;)

wysota
19th March 2007, 23:19
I have some different widgets for picking colours ready as well :) For example the two attached.

KShots
19th March 2007, 23:44
Hmm... so instead of simply using a simple QFrame so show what color was selected, the "proper" solution is to make a QWidget from scratch? I can do that, but I would have thought things would be a bit simpler than that.

EDIT: Actually, from what I can tell, those examples subclass QFrame to do what I'm trying to do. I've pretty much done the same thing, but my frames still do not appear at all. I'm running out of ideas. What could make a QFrame appear in designer, but not appear at all when running as an application?

Here are all the changed variables from my QFrame, and their values:


geometry = 82, 6, 16, 16 (x, y, width, and height, respectively)
size policy = fixed, fixed, 0, 0 (hSizeType, vSizeType, horizontalStretch and verticalStretch, respectively)
autoFillBackground = true
frameShape = QFrame::Panel
frameShadow = QFrame::Sunken

EDIT: Now, I have also sub-classed QFrame and promoted my existing frames to custom widgets. The sub-class has a paintEvent() function that fills a rectangle with the given color, and whenever a new color is set the repaint() function gets triggered. Still no dice.

wysota
20th March 2007, 01:49
There is no "proper" solution here. Your problem is probably that the frame is transparent (meaning that it doesn't listen to the background colour). Try calling "setAutoFillBackground(true)" on your frame. It might just make it work. Also remember about setting a minimum size for the frame or it'll get shrunk to nothing.

KShots
20th March 2007, 02:57
There is no "proper" solution here. Your problem is probably that the frame is transparent (meaning that it doesn't listen to the background colour). Try calling "setAutoFillBackground(true)" on your frame. It might just make it work. Also remember about setting a minimum size for the frame or it'll get shrunk to nothing.
As I showed above, I had the autoFillBackground set to true via designer. I also had the size fixed at 16 pixels for both height and width, but it being shrunk down to nothing does make sense (I can't even see the border of the frame at the moment). I tried setting the size from a 16x16 fixed size to a 16x16 minimum size to no effect as well. Any other ideas?

EDIT: Toying around with the dialog, I found that if I took out my horizontal spacers, the button now takes up the whole horizontal section of the dialog, going right over the spot that the frame should be. The frame is being shrunk down to nothing.

EDIT: On the other hand, telling the button to be a fixed size does not make the frame appear, either. It's like designer sees the frame, but the application does not bother building it.

wysota
20th March 2007, 11:06
Does this work?


#include <QApplication>
#include <QFrame>
#include <QWidget>
#include <QLayout>

int main(int argc, char **argv){
QApplication app(argc, argv);
QWidget wgt;
QHBoxLayout *l = new QHBoxLayout(&wgt);
QFrame *f = new QFrame;
QPalette p = f->palette();
p.setColor(QPalette::Window, Qt::red);
p.setColor(QPalette::Base, Qt::blue);
p.setColor(QPalette::Button, Qt::green);
f->setPalette(p);
f->setAutoFillBackground(true);
l->addWidget(f);
wgt.show();
wgt.resize(300, 200);
return app.exec();
}

Which style do you use for your applications? I checked the above code with Plastique and Cleanlooks and they both worked fine (red frame background).

KShots
1st May 2007, 19:25
I think what's going on is that if you have an expanding object in your layout, your empty QFrame disappears to nothingness.

I've tried setting minimum, base, and fixed sizes for the QFrame to no avail. Maybe if I put a simple label inside or something as a test case

yodasoda
15th June 2011, 00:55
One thing I can tell you is happening is that the properties you have seen and applied in qt designer dont appear at run time. In my case, I had manual code to set the stylesheet of my qframe subclass to give it a background color. This made it visible by showing the background. On the other hand the frame is not visible even if I try to set it programatically which brought me to this forum. Also, the subclassed qframes I had instantiated without setting the background color using stylesheet appear invisible to me as well.