Guest User

Untitled

a guest
Aug 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. Increase button font size when button size is changing
  2. #include <QtGui>
  3.  
  4. class FontAdjustingButton : public QPushButton {
  5. public:
  6. explicit FontAdjustingButton(QWidget *parent = NULL) : QPushButton(parent) {
  7. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  8. }
  9. protected:
  10. void resizeEvent(QResizeEvent *event) {
  11. int button_margin = style()->pixelMetric(QStyle::PM_ButtonMargin);
  12. QFont f = font();
  13. f.setPixelSize(event->size().height() - button_margin * 2);
  14. setFont(f);
  15. }
  16. };
  17.  
  18. int main(int argc, char **argv) {
  19. QApplication app(argc, argv);
  20. QWidget w;
  21. QVBoxLayout *layout = new QVBoxLayout;
  22. for (int i = 0; i < 5; ++i) {
  23. FontAdjustingButton *btn = new FontAdjustingButton;
  24. btn->setText(QString("Hello, world %1").arg(i));
  25. layout->addWidget(btn);
  26. }
  27. w.setLayout(layout);
  28. w.show();
  29. return app.exec();
  30. }
Add Comment
Please, Sign In to add comment