Results 1 to 14 of 14

Thread: Pass Clicks through a label

  1. #1
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Pass Clicks through a label

    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:
    Qt Code:
    1. void clickLabel::mousePressEvent(QMouseEvent *e)
    2. {
    3. //stop tracking the mouse so it's free to grab. Might be unnecessary.
    4. releaseMouse();
    5. //tell others we've been pressed and hand off the MouseEvent
    6. emit pressed(e);
    7. }
    To copy to clipboard, switch view to plain text mode 
    and implemented:
    Qt Code:
    1. void aniButton::stealMousePress(QMouseEvent *e)
    2. {
    3. //track the mouse cursor
    4. grabMouse();
    5. //deliver the mouse press to ourselves as if it were ours to begin with
    6. mousePressEvent(e);
    7. }
    To copy to clipboard, switch view to plain text mode 


    (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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pass Clicks through a label

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pass Clicks through a label

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pass Clicks through a label

    Maybe we should start from the beginning. What do you want to use those buttons and labels for?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pass Clicks through a label

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pass Clicks through a label

    Quote Originally Posted by tescrin View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pass Clicks through a label

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pass Clicks through a label

    Quote Originally Posted by tescrin View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pass Clicks through a label

    Imagine you have a GUI button likethis 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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pass Clicks through a label

    Style the button with stylesheets and don't use any icons or implement your own custom button from scratch.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    tescrin (22nd June 2012)

  12. #11
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pass Clicks through a label

    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.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pass Clicks through a label

    Quote Originally Posted by tescrin View Post
    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()).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Pass Clicks through a label

    Quote Originally Posted by tescrin View Post
    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!
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  15. #14
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Pass Clicks through a label

    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:
    Qt Code:
    1. QPixmap pixmap(":/Icons/icon.png");
    2. pixmap=pixmap.scaled(QSize(ui->appButton->width(),ui->appButton->height()),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    3. QPalette palette;
    4. palette.setBrush(ui->appButton->backgroundRole(),QBrush(pixmap));
    5. ui->appButton->setFlat(true);
    6. ui->appButton->setAutoFillBackground(true);
    7. ui->appButton->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

    What did not work (text at the side) was:
    Qt Code:
    1. ui->appButton->setFlat(true);
    2. ui->appButton->setAutoFillBackground(true);
    3. QIcon img(":/Icons/icon.png");
    4. ui->appButton->setIcon(img);
    5. ui->appButton->setIconSize(QSize(ui->appButton->width(),ui->appButton->height()));
    To copy to clipboard, switch view to plain text mode 

    What I could not use (no scaling possible) was:
    Qt Code:
    1. ui->appButton->setStyleSheet("background-image: url(:/Icons/icin.png);");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to simulate menu clicks?
    By oficjalne100 in forum Qt Programming
    Replies: 0
    Last Post: 12th March 2012, 13:50
  2. Replies: 1
    Last Post: 7th February 2012, 22:04
  3. Simulate left and right clicks?
    By hakermania in forum Newbie
    Replies: 5
    Last Post: 8th June 2011, 11:44
  4. Replies: 2
    Last Post: 20th December 2010, 17:51
  5. Replies: 3
    Last Post: 12th May 2010, 13:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.