当按钮大小改变时增加按钮字体大小

发布于 2024-12-14 10:17:18 字数 191 浏览 2 评论 0原文

我有一个 Qt 应用程序,其主窗口有五个按垂直顺序排列的按钮。 它们都有相同的尺寸。

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

有什么建议吗?

最好的,

吉他流

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.

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.

Any suggestions?

Best,

guitarflow

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2024-12-21 10:17:18

我想不出有什么方法可以在设计器中做到这一点,但它确实没有太多代码。这是一个快速而简单的概念证明。您想要考虑边距(使用 QStyle::pixelMetrics 等),但您明白了。

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文