{"id":77,"date":"2018-10-17T00:16:10","date_gmt":"2018-10-16T16:16:10","guid":{"rendered":"https:\/\/blog.simbot.net\/?p=77"},"modified":"2020-12-20T09:01:30","modified_gmt":"2020-12-20T01:01:30","slug":"qtscalefont","status":"publish","type":"post","link":"https:\/\/blog.simbot.net\/2018\/10\/17\/qtscalefont\/","title":{"rendered":"QT\u7a97\u53e3\u7f29\u653e\u2014\u2014\u652f\u6301\u4efb\u610f\u5206\u8fa8\u7387"},"content":{"rendered":"

QT\u7a97\u53e3\u7f29\u653e\u2014\u2014\u652f\u6301\u4efb\u610f\u5206\u8fa8\u7387<\/h1>\n

\u4f5c\u8005\uff1ahws000\uff08hws.000#163.com\uff09
\n\u58f0\u660e\uff1a\u7248\u6743\u6240\u6709\uff0c\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u3002
\n\u51fa\u5904\uff1ahttps:\/\/blog.simbot.net\/index.php\/2018\/10\/17\/qtscalefont\/<\/a><\/p>\n

\u901a\u8fc7\u4fee\u6539\u5b50\u7a97\u53e3\u5927\u5c0f\u53ca\u5b57\u53f7\uff0c\u9002\u5e94\u4efb\u610f\u5206\u8fa8\u7387\u3002<\/p>\n

1.\u65b9\u6cd5\u63cf\u8ff0<\/h2>\n

\uff081\uff09\u4f7f\u7528setMaximumSize\u3001setMinimumSize\u51fd\u6570\u4fee\u6539\u5b50\u7a97\u53e3\u5927\u5c0f\uff0c\u4f7f\u5176\u8ddf\u968f\u4e3b\u7a97\u53e3\u53d8\u5316\uff1b
\n\uff082\uff09\u5bbd\u3001\u9ad8\u7f29\u653e\u6bd4\u4f8b\u4e0d\u540c\u65f6\uff0c\u901a\u8fc7qt\u7684\u81ea\u52a8\u5e03\u5c40\u529f\u80fd\uff0c\u8c03\u6574\u5b50\u7a97\u53e3\u7684\u4f4d\u7f6e\uff1b
\n\uff083\uff09\u901a\u8fc7setPointSizeF\u51fd\u6570\u63a7\u5236\u5b57\u4f53\u53d8\u5316\u3002
\n\u81ea\u52a8\u5e03\u5c40\u529f\u80fd\u4e0d\u8bb2\u89e3\uff0c\u53ea\u8bb2\uff081\uff09\uff083\uff09\u3002<\/p>\n

2.\u4e0a\u4ee3\u7801<\/h2>\n
\n
\n\n\n\n
\n
1<\/div>\n
2<\/div>\n
3<\/div>\n
4<\/div>\n
5<\/div>\n
6<\/div>\n
7<\/div>\n
8<\/div>\n
9<\/div>\n
10<\/div>\n
11<\/div>\n
12<\/div>\n
13<\/div>\n
14<\/div>\n
15<\/div>\n
16<\/div>\n
17<\/div>\n
18<\/div>\n
19<\/div>\n
20<\/div>\n
21<\/div>\n
22<\/div>\n
23<\/div>\n
24<\/div>\n
25<\/div>\n
26<\/div>\n
27<\/div>\n
28<\/div>\n
29<\/div>\n
30<\/div>\n
31<\/div>\n
32<\/div>\n
33<\/div>\n
34<\/div>\n
35<\/div>\n
36<\/div>\n
37<\/div>\n
38<\/div>\n
39<\/div>\n
40<\/div>\n
41<\/div>\n
42<\/div>\n
43<\/div>\n
44<\/div>\n
45<\/div>\n
46<\/div>\n
47<\/div>\n
48<\/div>\n
49<\/div>\n
50<\/div>\n
51<\/div>\n
52<\/div>\n
53<\/div>\n<\/td>\n
\n
\n
void<\/code> MainWindow::resizeEvent(QResizeEvent* event)<\/code><\/div>\n
{<\/code><\/div>\n
    <\/code>QSize size = event->size();<\/code><\/div>\n
<\/div>\n
    <\/code>float<\/code> scaleX = size.width() \/ 1920.0f;<\/code>\/\/\u539f\u59cb\u5206\u8fa8\u7387<\/code><\/div>\n
    <\/code>float<\/code> scaleY = size.height() \/ 1080.0f;<\/code><\/div>\n
    <\/code>float<\/code> scale = std::min(scaleX, scaleY);<\/code>\/\/\u4e0d\u662f\u7b49\u6bd4\u7f29\u653e\u65f6\uff0c\u9009\u62e9\u6700\u5c0f\u7684\u7f29\u653e\u6bd4\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u52a8\u5e03\u5c40\u8c03\u6574\u7a97\u53e3\u4f4d\u7f6e<\/code><\/div>\n
<\/div>\n
    <\/code>const<\/code> QObjectList& list = <\/code>this<\/code>->children();<\/code>\/\/\u679a\u4e3e\u5b50\u7a97\u53e3\uff0c\u5e76\u8bbe\u7f6e\u5927\u5c0f<\/code><\/div>\n
    <\/code>foreach(QObject* obj, list) {<\/code><\/div>\n
        <\/code>if<\/code> (obj->isWidgetType()) {<\/code><\/div>\n
            <\/code>QWidget* child = (QWidget*)obj;<\/code><\/div>\n
            <\/code>if<\/code> (std::<\/code>abs<\/code>(scale - _lastScale) > 0.03) {<\/code>\/\/\u5f53\u7f29\u653e\u6bd4\u4f8b\u8d85\u8fc7\u9608\u503c\u65f6\u624d\u8fdb\u884c\uff0c\u56e0\u4e3a\u7a97\u53e3\u5bbd\u9ad8\u662f\u6574\u6570\uff0c\u9891\u7e41\u7f29\u653e\u4f1a\u4f7f\u7a97\u53e3\u53d8\u5f62<\/code><\/div>\n
                <\/code>scaleChildWidget(child, scale \/ _lastScale);<\/code><\/div>\n
            <\/code>}<\/code><\/div>\n
        <\/code>}<\/code><\/div>\n
    <\/code>}<\/code><\/div>\n
    <\/code><\/div>\n
    <\/code>if<\/code> (std::<\/code>abs<\/code>(scale - _lastScale) > 0.03) {<\/code><\/div>\n
        <\/code>_lastScale = scale;<\/code><\/div>\n
    <\/code>}<\/code><\/div>\n
}<\/code><\/div>\n
<\/div>\n
void<\/code> MainWindow::scaleChildWidget(QWidget* widget, <\/code>float<\/code> scale)<\/code><\/div>\n
{<\/code><\/div>\n
    <\/code>const<\/code> QObjectList& list = widget->children();<\/code><\/div>\n
    <\/code>foreach(QObject* obj, list) {<\/code><\/div>\n
        <\/code>if<\/code> (obj->isWidgetType()) {<\/code><\/div>\n
            <\/code>QWidget* child = (QWidget*)obj;<\/code><\/div>\n
<\/div>\n
            <\/code>if<\/code> (scale < 1) {<\/code><\/div>\n
                <\/code>scaleChildWidget(child, scale);<\/code>\/\/\u5904\u7406\u6240\u6709\u5b50\u7a97\u53e3<\/code><\/div>\n
            <\/code>}<\/code><\/div>\n
<\/div>\n
            <\/code>QSize max = child->maximumSize();<\/code><\/div>\n
            <\/code>QSize min = child->minimumSize();<\/code><\/div>\n
            <\/code>if<\/code> (max.width() < 65536) {<\/code>\/\/\u5b50\u7a97\u53e3\u9700\u8981\u8bbe\u7f6e\u521d\u59cb\u503c<\/code><\/div>\n
                <\/code>child->setMaximumSize(max * scale);<\/code><\/div>\n
            <\/code>}<\/code><\/div>\n
            <\/code>if<\/code> (min.width() > 0) {<\/code><\/div>\n
                <\/code>child->setMinimumSize(min * scale);<\/code><\/div>\n
            <\/code>}<\/code><\/div>\n
<\/div>\n
            <\/code>QFont font = child->font();<\/code>\/\/\u7f29\u653e\u5b57\u4f53<\/code><\/div>\n
            <\/code>font.setPointSizeF(font.pointSizeF() * scale);<\/code><\/div>\n
            <\/code>child->setFont(font);<\/code><\/div>\n
<\/div>\n
            <\/code>if<\/code> (scale > 1) {<\/code><\/div>\n
                <\/code>scaleChildWidget(child, scale);<\/code><\/div>\n
            <\/code>}<\/code><\/div>\n
        <\/code>}<\/code><\/div>\n
    <\/code>}<\/code><\/div>\n
}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"

\u901a\u8fc7\u4fee\u6539\u5b50\u7a97\u53e3\u5927\u5c0f\u53ca\u5b57\u53f7\uff0c\u9002\u5e94\u4efb\u610f\u5206\u8fa8\u7387\u3002
\n1.\u65b9\u6cd5\u63cf\u8ff0
\n \uff081\uff09\u4f7f\u7528setMaximumSize\u3001setMinimumSize\u51fd\u6570\u4fee\u6539\u5b50\u7a97\u53e3\u5927\u5c0f\uff0c\u4f7f\u5176\u8ddf\u968f\u4e3b\u7a97\u53e3\u53d8\u5316\uff1b
\n\uff082\uff09\u5bbd\u3001\u9ad8\u7f29\u653e\u6bd4\u4f8b\u4e0d\u540c\u65f6\uff0c\u901a\u8fc7qt\u7684\u81ea\u52a8\u5e03\u5c40\u529f\u80fd\uff0c\u8c03\u6574\u5b50\u7a97\u53e3\u7684\u4f4d\u7f6e\uff1b
\n\uff083\uff09\u901a\u8fc7setPointSizeF\u51fd\u6570\u63a7\u5236\u5b57\u4f53\u53d8\u5316\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[20,22,21],"_links":{"self":[{"href":"https:\/\/blog.simbot.net\/wp-json\/wp\/v2\/posts\/77"}],"collection":[{"href":"https:\/\/blog.simbot.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.simbot.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.simbot.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.simbot.net\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":8,"href":"https:\/\/blog.simbot.net\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":118,"href":"https:\/\/blog.simbot.net\/wp-json\/wp\/v2\/posts\/77\/revisions\/118"}],"wp:attachment":[{"href":"https:\/\/blog.simbot.net\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.simbot.net\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.simbot.net\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}