PDA

View Full Version : Pass Clicks through a label



tescrin
22nd June 2012, 17:17
Currently I've made a subclass of labels called "clickLabels" that has clicks pass through it.
-This is because I have labels "covering" my buttons
-Which is because the button seems to push an icon image out of it's way if you also have text for the button

Now that I'm done with my first iteration of this design (or close enough); I'm reflecting on what I may have missed as I get up to speed on all things Qt.


My question is "is this functionality redundant?" I could see it being either way. I went this direction because labels seemed to "absorb" clicks no matter what properties I applied to them; so to make them "transparent" (so the button is clicked) I did this:

void clickLabel::mousePressEvent(QMouseEvent *e)
{
//stop tracking the mouse so it's free to grab. Might be unnecessary.
releaseMouse();
//tell others we've been pressed and hand off the MouseEvent
emit pressed(e);
}
and implemented:

void aniButton::stealMousePress(QMouseEvent *e)
{
//track the mouse cursor
grabMouse();
//deliver the mouse press to ourselves as if it were ours to begin with
mousePressEvent(e);
}


(I grab the mouse for other reasons.) The idea should be obvious by now; and now i"m just curious if it was done in error because the functionality already exists. I.E. did I go reinvent the wheel?

wysota
22nd June 2012, 22:20
Isn't it just simpler to subclass QPushButton and draw the text where you want it instead of putting an artifficial label over the button?

tescrin
22nd June 2012, 22:47
Well, maybe? I will note that while I've used C++ and various languages/libraries for years, I've only done a few projects in Qt. So with that in mind, your simple solution might be quite applicable (and exactly why I was asking.)

In this case, I have the constraint that the label needs to be available to place as you like in Qt Designer, so when I did the initial design in Qt Designer I placed new labels over the top because the default label was clashing (read, pushing) the images I set to the buttons off to the side (rather than being overlaid with eachother, the icon would be on the left side, and the text on the right.)


If you're able to do this in designer I'd be happy to find out as that's yet another improvement on the design. The more I can simplify this the better!



P.S.
Frankly, there are a lot of klugey pieces in the code that show me working around Qt's in-place functionality by subclassing because it's hard to tell sometimes whether or not the functionality is an option in the original Qt code and whether or not it's easier to just reinvent the wheel instead of spending potentially wasted hours verifying if existing functionality exists yet.

For example; I subclassed QPushButton in order to get it to animate in a specific way. To do this without messing with lots of existing functionality it holds *even more* icons. It's quite possible that using the "trackMouse" option (instead of grabbing and releasing the mouse) and that the "animateButton" option applies to a subset of the existing icons that I simply haven't figured out through testing yet because:
-initial tests failed
-there's a desire for progress on this project (replicating an existing UI as a proof of concept)
-and because there are an unbounded number of potential tests to do to make it work as desired.


Right now, I'm at a bit of a stopping point where I can reflect on what I've learned and figure out how to improve the program to stop fighting with Qt's existing functionality where I don't need to. If the proof of concept is enough to show Qt's worth on this project it'll become a much larger scale project where I'll be redoing much of my existing code; meaning a great oppurtunity to improve on anything and everything wrong with it.

With my life story there, just telling me whether or not the labels that come with buttons can be manipulated well in the Designer would be enough :)

wysota
22nd June 2012, 22:49
Maybe we should start from the beginning. What do you want to use those buttons and labels for?

tescrin
22nd June 2012, 22:57
The buttons are used as normal buttons, they communicated everything through SLOTs activated by "clicked()" signals. The initial placement in designer had the text pushing the icons I needed to use out of the way; being restricted to using designer (and still learning the library) I just placed labels *over* them.

Realizing those absorbed clicks no matter which options or combination of options I selected I subclassed and passed clicks through so the buttons would work *despite* the labels. The labels are *just* text labels that I wish to do nothing with other than display text. They're overlayed over the button in order to display the button's text, but *over* the image.

wysota
22nd June 2012, 23:03
The initial placement in designer had the text pushing the icons I needed to use out of the way
That still doesn't tell why you want an arbitrary position of a label on the button.

being restricted to using designer
Why are you restricted to using Designer?


I just placed labels *over* them.
Bad idea.


They're overlayed over the button in order to display the button's text, but *over* the image.
So subclass the button, reimplement its paintEvent and paint the text over the image.

tescrin
22nd June 2012, 23:13
I'm not *soley* restricted to designer; but the idea is two fold:
-all the GUI elements should be able to be placed/sized/messed with in designer because non-programming types may be the ones finalizing the GUI *wayyyy* down the line.
-It makes sense to this in general as it's much easier to size/place/mess with GUI elements in the GUI instead of trial and error with pixel numbers in code. :S

To your thoughts:
I may be misunderstanding something; the text displayed on the button in designer *IS* the label yes? If it is, then the fact it pushes my icon out of the way (I'd hazard to guess using a QLayout of some kind?) makes it unusable in it's standard form.

If I subclass and use a new label I *may* be able to use the Plugin approach to adding widgets to the designer in order to allow them to place the label in an arbitrary position in the designer. Getting the label where I want won't be the problem; getting the label to be movable in designer while retaining functionality is.

The approach I used is abstracted away from the designer so they could arbitrarily place the label and it doesn't effect clicking; thus seemed reasonable at the time. In my hindsight I'm looking to correct this "bad idea" as I'm sure Qt has a way around this lol.

wysota
22nd June 2012, 23:14
the text displayed on the button in designer *IS* the label yes?
No, it's not.

However I still don't know why you want to place text over an image. Could you please explain that?

tescrin
22nd June 2012, 23:21
Imagine you have a GUI button likethis (http://www.google.com/imgres?q=button&um=1&hl=en&sa=N&biw=1180&bih=782&tbm=isch&tbnid=3FRicYVvBf4_tM:&imgrefurl=http://www.psdgraphics.com/psd/3d-red-push-button/&docid=k5wF1xO5TSrJ2M&imgurl=http://www.psdgraphics.com/wp-content/uploads/2009/07/push-button.jpg&w=610&h=458&ei=7uDkT_a5Hoae2QWoiITaCQ&zoom=1&iact=hc&vpx=111&vpy=181&dur=345&hovh=194&hovw=259&tx=154&ty=85&sig=115927255730312965939&page=1&tbnh=126&tbnw=160&start=0&ndsp=28&ved=1t:429,r:0,s:0,i:153) in the UI you're replicating; except for two things:
-The text is a standard text
-The graphics folder you've been given only contains the picture of the button (and it's animated variants) and none of them have the text on them

In order to reproduce the UI visually, I'll need to add that text (and have) so that it looks the same.

wysota
22nd June 2012, 23:36
Style the button with stylesheets and don't use any icons or implement your own custom button from scratch.

tescrin
22nd June 2012, 23:45
Hmm, not sure how to use the style sheets in that regard yet.

I'm guessing the way forward is to just bite the bullet and finish my custom implementation going from QAbstractPushButton and then figure out how to get the label to add into the Designer with it.

Just to make sure; does a custom widget allow you to have two place-able objects when you add a single one to the designer? I.E. When I look at Button Box in designer, they're stuck together. Being able to manipulate each one would be beneficial, but in the worst case, not necessary.


EDIT: to clarify, I *was* using QPushButton, I'm saying I *should* use QAbstract and finish the implementation I've already started going from there instead.

wysota
23rd June 2012, 01:13
Hmm, not sure how to use the style sheets in that regard yet.

I'm guessing the way forward is to just bite the bullet and finish my custom implementation going from QAbstractPushButton and then figure out how to get the label to add into the Designer with it.

Just to make sure; does a custom widget allow you to have two place-able objects when you add a single one to the designer? I.E. When I look at Button Box in designer, they're stuck together. Being able to manipulate each one would be beneficial, but in the worst case, not necessary.


EDIT: to clarify, I *was* using QPushButton, I'm saying I *should* use QAbstract and finish the implementation I've already started going from there instead.

Please don't try stacking widgets onto each other. That's a dead-end. If you don't know how to use stylesheets then either learn it or subclass QPushButton (or QAbstractButton, it doesn't really matter) and use QPainter::drawText() to draw the text over the image you want (you can draw the image using QPainter::drawPixmap() or QPainter::drawImage()).

amleto
23rd June 2012, 11:08
I'm not *soley* restricted to designer; but the idea is two fold:
-all the GUI elements should be able to be placed/sized/messed with in designer because non-programming types may be the ones finalizing the GUI *wayyyy* down the line.
-It makes sense to this in general as it's much easier to size/place/mess with GUI elements in the GUI instead of trial and error with pixel numbers in code. :S


Hang on there - it sounds very much like you are not using layouts. That is also a bad idea!

sedi
23rd June 2012, 17:59
I've had the same problem (if I've read yours correctly). I've tried several approaches to have text written across the icon on a QPushButton. This is what finally worked for me:

Working code:

QPixmap pixmap(":/Icons/icon.png");
pixmap=pixmap.scaled(QSize(ui->appButton->width(),ui->appButton->height()),Qt::IgnoreAspectRatio,Qt::SmoothTransfor mation);
QPalette palette;
palette.setBrush(ui->appButton->backgroundRole(),QBrush(pixmap));
ui->appButton->setFlat(true);
ui->appButton->setAutoFillBackground(true);
ui->appButton->setPalette(palette);

What did not work (text at the side) was:


ui->appButton->setFlat(true);
ui->appButton->setAutoFillBackground(true);
QIcon img(":/Icons/icon.png");
ui->appButton->setIcon(img);
ui->appButton->setIconSize(QSize(ui->appButton->width(),ui->appButton->height()));


What I could not use (no scaling possible) was:


ui->appButton->setStyleSheet("background-image: url(:/Icons/icin.png);");