Results 1 to 4 of 4

Thread: auto resize font in QSpinBox

  1. #1
    Join Date
    Mar 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default auto resize font in QSpinBox

    Any idea haw to auto re size the font (point size) of the text in the spin box?


    I have a spin box in a window and when resizing the window the spin box is automatically re sized but the point size of the text (font size) in the spin box stays the same.
    I've tried to reimplement the MainWindow::resizeEvent() in order to increase the font size according to the new size(height) of the spin box:

    void MainWindow::resizeEvent(QResizeEvent */*event*/)
    {
    ui->spinBox->setStyleSheet(QString("font: %1pt").arg(ui->spinBox- >geometry().height() - 20));
    }

    but I think that it somehow recursively calls it self, increases the font size to much and crash.

  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: auto resize font in QSpinBox

    You should call MainWindow::resizeEvent(event) in the end.

    Here is another alternate you can try.

    Qt Code:
    1. class SpinBox : public QSpinBox
    2. {
    3. public:
    4. explicit SpinBox(QWidget * parent = 0) : QSpinBox(parent) { }
    5.  
    6. protected:
    7. void resizeEvent(QResizeEvent * event)
    8. {
    9. int h = event->size().height();
    10.  
    11. QFont f = font();
    12. f.setPixelSize(h - 20);
    13. setFont(f);
    14.  
    15. QSpinBox::resizeEvent(event);
    16. }
    17. };
    18.  
    19. int main(int argc, char *argv[])
    20. {
    21. QApplication a(argc, argv);
    22.  
    23. SpinBox s;
    24. s.show();
    25.  
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 
    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
    Mar 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: auto resize font in QSpinBox

    Thank you Santosh for helping,
    when I test your code it's ok, but it is the case when you show only the spinbox. Unfortunately if I place the spinbox in a main window, the same recursive calling of the resizeEvent() function happens, the spinbox increases too much and the program crashes. I'v attached the code I'm testing.
    Everything best, Goran.
    Attached Files Attached Files

  4. #4
    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: auto resize font in QSpinBox

    Setting the size policy to Ignore seems to work works
    Qt Code:
    1. spinbox.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    To copy to clipboard, switch view to plain text mode 
    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.

  5. The following user says thank you to Santosh Reddy for this useful post:

    GoranShekerov (5th March 2015)

Similar Threads

  1. QTreeView: auto-resize
    By papillon in forum Qt Programming
    Replies: 8
    Last Post: 23rd October 2011, 22:18
  2. QPushButton's font is auto bold in WinCE
    By lovelypp in forum Qt Programming
    Replies: 0
    Last Post: 10th July 2008, 13:47
  3. QTextEdit auto resize
    By bunjee in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 19:59
  4. QLabel - auto font scaling possible?
    By Byngl in forum Qt Programming
    Replies: 1
    Last Post: 11th October 2007, 16:50
  5. How to get a QDockWidget to auto resize?
    By bitChanger in forum Qt Programming
    Replies: 4
    Last Post: 6th January 2006, 16:10

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.