Results 1 to 4 of 4

Thread: Complex button contents

  1. #1
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Complex button contents

    Hello. I'm trying to subclass QPushButton to contain arbitrary contents. My first attempt looks a bit like this:

    Qt Code:
    1. ComplexButton::ComplexButton (QWidget *parent) : QPushButton(Parent) {
    2.  
    3. l->addWidget(new Foo(this));
    4. l->addWidget(new Bar(this));
    5. l->addWidget(new Baz(this));
    6. setLayout(l);
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    Unfortunately the button is a minimal size and the contents are squashed. I tried putting the QVBoxLayout in a generic QWidget and adding the line
    Qt Code:
    1. setMinimumSize(generic->sizeHint())
    To copy to clipboard, switch view to plain text mode 
    Which at least made the button big enough for the contents.

    But that still isn't what I want!

    I want the buttons to expand their width to fill their parent, causing the parent to widen if necessary. This would happen automatically if I hadn't subclassed QPushButton but merely put a long string in it. I can't recreate that behaviour for love nor money. I've been playing around with the size policy but that hasn't helped and I don't readlly understand it. I imagine the trick is with QPushButton's default layout which I'm replacing, but calling layout() returns a null pointer, and now I'm thoroughly confused.

    To sum up: I want a subclass of QPushButton with an arbitrary widget as contents and a space-filling width behaviour.

    Clue, please? Cheers.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Complex button contents

    have you tried calling adjustSize on the button?
    (Might be of course that QPushButton did not expect to be subclassed in this way.)

  3. #3
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Complex button contents

    and does size policy work?

  4. #4
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Complex button contents

    I've tried guessing some sizePolicy code, nothing worked.

Similar Threads

  1. button with backgr and icon using stylesheets
    By s_p_t10 in forum Qt Programming
    Replies: 0
    Last Post: 7th May 2008, 20:19
  2. problem about mouse button events
    By vql in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2008, 10:14
  3. question about button
    By narumi in forum Newbie
    Replies: 2
    Last Post: 21st January 2008, 05:44

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.