Results 1 to 6 of 6

Thread: Why does QMenuBar resize icons?

  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Why does QMenuBar resize icons?

    Hi all,

    In the small example below I am inserting a small pixmap into a QAction in a
    QMainWindow's menubar. I'm using a pixmap ("key.png") that is 19 by 9 pixels,
    but QT resizes it to 16x7.

    A 19x9 pixels image would easily fit on the menubar, so why does QT feel a need to resize? Is there a way to have it shown in its original size?

    (QT4.2, X-Windows)

    /Joakim

    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QMenu>
    4. #include <QMenuBar>
    5.  
    6.  
    7. int main( int argc, char **argv )
    8. {
    9. QApplication app( argc, argv );
    10.  
    11. QMainWindow *mw = new QMainWindow();
    12. mw->show();
    13.  
    14. QMenuBar *mb = mw->menuBar();
    15. QMenu *menu = new QMenu("File");
    16. mb->addMenu(menu);
    17. menu->addAction("About", &app, SLOT(aboutQt()));
    18.  
    19. QAction *act = mb->addAction("");
    20. act->setIcon(QPixmap("key.png"));
    21.  
    22. app.exec();
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Why does QMenuBar resize icons?

    QMenu uses QStyle::PM_SmallIconSize. You could use a custom style and provide larger PM_SmallIconSize.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    drhex (26th October 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Why does QMenuBar resize icons?

    Alright, I found out what style the menubar was using by calling metaObject()->className() on it. It was QPlastiqueStyle,,
    so I replaced the style with an instance of the class below:

    Qt Code:
    1. class myStyle: public QPlastiqueStyle
    2. {
    3. Q_OBJECT
    4. public:
    5. int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const {
    6. int s = QPlastiqueStyle::pixelMetric(metric, option, widget);
    7. if (metric == QStyle::PM_SmallIconSize) {
    8. s = 20;
    9. }
    10. return s;
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 

    Small icons can now be upto 20x20 pixels instead of 16x16. And it works!
    But one thing still bothers me, I had to hardcode "QPlastiqueStyle". What if another style had been specified on the commandline? Is there a way to modify the behaviour of the current style rather than forcing in a specific style?

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Why does QMenuBar resize icons?

    Quote Originally Posted by drhex View Post
    But one thing still bothers me, I had to hardcode "QPlastiqueStyle". What if another style had been specified on the commandline? Is there a way to modify the behaviour of the current style rather than forcing in a specific style?
    There's a technique called proxy style. A proxy style redirects all other than overridden calls to the "original" style.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    drhex (27th October 2006)

  7. #5
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Why does QMenuBar resize icons?

    can you give me you code ,I have the same proble,I am a new guy at qt

    could you sent a email to me
    ouyangzy2005@163.com

  8. #6
    Join Date
    Nov 2010
    Posts
    4
    Qt products
    Qt4

    Question Re: Why does QMenuBar resize icons?

    Hi, i use above code
    int s = QPlastiqueStyle:: pixelMetric(metric, option, widget);
    if (metric == QStyle::PM_SmallIconSize) {
    s = 50;
    }
    then the icon and text overlap, it seems the text position doesn't change when the icon become large.
    How to solve this problem?

Similar Threads

  1. Unable to resize a QCanvas.
    By yellowmat in forum Newbie
    Replies: 2
    Last Post: 21st June 2006, 13:52
  2. SVG icons on a QPushButton?
    By Kumula in forum Qt Programming
    Replies: 2
    Last Post: 22nd May 2006, 22:03
  3. Facing problem with tool bar icons
    By jnana in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2006, 09:37
  4. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 17:30
  5. postponing resize event
    By Honestmath in forum Qt Programming
    Replies: 11
    Last Post: 26th February 2006, 01:32

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.