Results 1 to 3 of 3

Thread: Filling QToolButton with an image

  1. #1
    Join Date
    Aug 2013
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Filling QToolButton with an image

    Hello.

    Is there any way to fill the whole button with its assigned icon?

    I am doing it this way, so that images should exactly match the size of the buttons, but there's always an empty margin around the images.

    Qt Code:
    1. QSize size = ui->plano_1->size();
    2. qDebug() << Q_FUNC_INFO << QString("icon size: %1,%2").arg(size.width()).arg(size.height());
    3. ui->plano_1->setIcon(QIcon(myBudget->getPlano(1)));
    4. ui->plano_1->setIconSize(size);
    5. ui->plano_1->setContentsMargins(0, 0, 0, 0);
    To copy to clipboard, switch view to plain text mode 

    As you see, I also tried using setContentsMargins(0,0,0,0). But it doesn't seem to make any difference.

    Thank you for any response

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Filling QToolButton with an image

    Qt Code:
    1. QSize size = ui->plano_1->size();
    To copy to clipboard, switch view to plain text mode 
    Size of the tool button is valid only after the tool button is visible (i.e show() is called). Size of any widget is not valid before show() is called on it or it's parent.

    setContentsMargins() is only used by the layout manager and does not affect the widget content.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jun 2014
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Filling QToolButton with an image

    Hi,
    I didn't manage to strech the icon to more than it's original size, so if you icon size is 16x16, this will be the icon's bigget size.
    What you can do, is to change the button's size, and make the margins smaller. In my main window, the button's default size is 32x32, and a lot of the button's area is empty.
    Once I changed the button's maximum size, it looked better.

    myButton->setMaximumSize(QSize(16, 16));

    Now the icon is covering almost the whole button.

    Good luck!

Similar Threads

  1. QGraphicsItem filling with pattern
    By fvila in forum Qt Programming
    Replies: 1
    Last Post: 24th November 2010, 15:21
  2. Filling a QGraphicsItem
    By c_srikanth1984 in forum Qt Programming
    Replies: 15
    Last Post: 6th July 2009, 15:34
  3. Filling an image
    By Caius Aérobus in forum Qt Programming
    Replies: 6
    Last Post: 6th July 2008, 22:13
  4. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 18:53
  5. filling scrollview
    By illuzioner in forum Qt Programming
    Replies: 1
    Last Post: 17th February 2006, 23:00

Tags for this Thread

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.