欢迎访问mehome的博客

Tomorrow is another day.
Fork me on GitHub

Qt界面布局问题

一、环境及问题

1.1环境

  • 系统:Window10 64 企业版
  • Qt:Qt5.12.12
  • VS:vs2017企业版

1.2问题

  1. 主界面程序通过代码添加控件和布局,控件和布局不显示。
  2. QWidget界面程序通过代码添加布局不生效。

二、解决办法

  1. 主界面程序继承自QMainWindow,所以需要添加QWidget,然后通过setCentralWidget设置中心控件。
  2. QWidget、主界面程序通过代码添加布局不生效可能原因:new的QHBoxLayout或QVBoxLayout设置了依赖,这样会导致设置布局失败(例如:QHBoxLayout mp_hlayout_3 = new QHBoxLayout(this);)。

三、相关参考

  • 主界面布局问题参考:qt助手(Assistan)或官方文档的QMainWindow类的说明有相关描述
    • 具体描述:Note: Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.


posted @ 2022-01-20 00:57  mehome  阅读(433)  评论(0编辑  收藏  举报