![](/img/trans.png)
[英]Promote QWidget to QMainWindow or add QMainWindow to QWidget from Qt Designer
[英]Cannot add Widgets to QMainWindow using Qt Designer and Clion
我开始为一个项目学习 Qt,我想用 CLion 来做。 说了这么多,我按照官方教程在CLion上配置Qt:
https://www.jetbrains.com/help/clion/qt-tutorial.html
我将 Qt Designer 和 Creator 都设置为外部工具,所以我可以编辑 .ui 文件。 然后我创建了一个 Qt Widgets Executable Project 和一个 Qt UI Class,它继承了 QMainWindow(CLion 帮助创建项目和 ZA2F22ED4F8EBCBB1DABC2) 问题是当我在 Qt Designer 中打开 my.ui 文件时,它不允许我添加或编辑小部件。 我还尝试了 Qt Creator 并遇到了同样的问题。
如果我创建一个继承自 QWidget 的 Qt UI Class 我可以编辑小部件。
我尝试直接在 Qt Creator 中创建一个新项目,它在那里工作正常。
Qt 版本:6 | CLion 版本:2021.1.2 | 操作系统:Ubuntu 20.04.2 LTS
没有 centralWidget
添加;
<widget class="QWidget" name="centralwidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
就在周围;
<property name="windowTitle">
<string>MainWindow</string>
</property>
那应该可以解决问题。 通常它应该自动生成,但由于某些原因 clion 不这样做,顺便说一句,意图可能会被打破。
我在 CLion 中遇到了同样的问题。 当我们检查“ .ui ”文件时,我们意识到情况确实如此,因为没有“中央小部件”。 我们找到了一种解决方法,但我们无法获得最终解决方案的结果。
解决方案:
右键单击“ .ui ”文件并将其标记为“标记为纯文本”。 然后打开文件。 它应该是这样的 XML 格式;
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author/>
<comment/>
<exportmacro/>
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
</widget>
<pixmapfunction/>
<connections/>
</ui>
在 QMainWindow 下方添加这个 Central 小部件代码:
<widget class="QWidget" name="centralwidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
它应该是这样的;
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author/>
<comment/>
<exportmacro/>
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</widget>
<pixmapfunction/>
<connections/>
</ui>
现在当你在External Tools中用Qt Designer打开它,你会看到问题解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.