Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Increase button font size when button size is changing
- #include <QtGui>
- class FontAdjustingButton : public QPushButton {
- public:
- explicit FontAdjustingButton(QWidget *parent = NULL) : QPushButton(parent) {
- setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
- }
- protected:
- void resizeEvent(QResizeEvent *event) {
- int button_margin = style()->pixelMetric(QStyle::PM_ButtonMargin);
- QFont f = font();
- f.setPixelSize(event->size().height() - button_margin * 2);
- setFont(f);
- }
- };
- int main(int argc, char **argv) {
- QApplication app(argc, argv);
- QWidget w;
- QVBoxLayout *layout = new QVBoxLayout;
- for (int i = 0; i < 5; ++i) {
- FontAdjustingButton *btn = new FontAdjustingButton;
- btn->setText(QString("Hello, world %1").arg(i));
- layout->addWidget(btn);
- }
- w.setLayout(layout);
- w.show();
- return app.exec();
- }
Add Comment
Please, Sign In to add comment