Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要为 QWidget 设置 sizepolicy 以便在布局中调整大小时小部件的宽度始终是 6 的倍数(宽度 = 6、12、18、24、30、36 ...),高度始终是4. 我该怎么做?
您需要处理小部件的调整大小事件。子类化小部件并重新实现resizeEvent方法,或者为小部件安装事件过滤器。在调整大小事件处理程序中检查当前大小并在必要时更正它:
resizeEvent
QSize size = widget->size(); size.setWidth(qRound(size.width() / 6) * 6); size.setHeight(qRound(size.height() / 4) * 4); if (size != widget->size()) { widget->resize(size); }