更改按钮大小时增加按钮字体大小 [英] Increase button font size when button size is changing

查看:99
本文介绍了更改按钮大小时增加按钮字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Qt应用程序,它的主窗口有五个按垂直顺序排列的按钮. 它们都有相同的大小.

I'm having a Qt application with a main window that has five buttons aligned in a vertical order. They all have the same size.

我要做的就是在应用全屏显示时增加按钮标签的字体大小. 我真的很感激不需要太多代码的解决方案……希望这可以在Qt Designer中完成,但是我找不到方法.

All I want to do is to increase the font size of the button label when the app goes fullscreen. I would really appreciate a solution that does not need too much code ... was hoping that this was something that could be done in Qt Designer, but I couldn't find a way how to.

有什么建议吗?

最好

guitarflow

guitarflow

推荐答案

在设计器中我想不出任何办法,但实际上并不需要太多代码.这是一个概念的快速证明.您想考虑边距(使用QStyle::pixelMetrics等),但是您知道了.

I can't think of any way to do it in designer, but it's really not too much code. Here's a quick-and-dirty proof of concept. You'd want to take into account margins (using QStyle::pixelMetrics and the like), but you get the idea.

#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();
}

这篇关于更改按钮大小时增加按钮字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆