Bruno Serpa
Bruno Serpa

Reputation: 23

Cannot add Widgets to QMainWindow using Qt Designer and Clion

I am starting to learn Qt for a project and I would like to use CLion to do it. Having said that I followed the official tutorial to configure Qt on CLion:

https://www.jetbrains.com/help/clion/qt-tutorial.html

I set both Qt Designer and Creator as External Tools, so I can edit the .ui files. Then I created a Qt Widgets Executable Project and a Qt UI Class that inherits QMainWindow (CLion helps creating the project and the class automatically). The problem is that when I open my .ui file in Qt Designer it doesn't let me add or edit widgets. I also tried in Qt Creator and got the same problem.

As you can see in the image below, the Main Window should have the grid, but it doesn't.
this

If I create a Qt UI Class that inherits from QWidget I am able to edit the widgets.

I tried creating a new project directly in Qt Creator and it works fine there.

Qt Version: 6 | CLion Version: 2021.1.2 | OS: Ubuntu 20.04.2 LTS

Upvotes: 1

Views: 1461

Answers (2)

Hakan Kaya
Hakan Kaya

Reputation: 46

I had the same problem in CLion. When we inspected the ".ui" file, we realized that this was the case because there was no "central widget". We found a workaround, but yet we can not get results for a definitive solution.

Solution:

Right-click on the ".ui" file and mark it as "Mark as Plain Text". Then open the file. It should be in XML format like this;

<?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>

Add this Central widget code just below QMainWindow:

    <widget class="QWidget" name="centralwidget">
        <property name="sizePolicy">
            <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
                <horstretch>0</horstretch>
                <verstretch>0</verstretch>
            </sizepolicy>
        </property>
    </widget>

It should look like this;

<?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>

Now when you open it with Qt Designer in External Tools, you will see that the problem will be solved.

Upvotes: 2

Semih Ufuk G&#252;ler
Semih Ufuk G&#252;ler

Reputation: 21

There isn't a centralWidget

add;

   <widget class="QWidget" name="centralwidget">
    <property name="sizePolicy">
     <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
      <horstretch>0</horstretch>
      <verstretch>0</verstretch>
     </sizepolicy>
    </property>
   </widget>

Right under around;

  <property name="windowTitle">
   <string>MainWindow</string>
  </property>

That should fix the issue. Normally it should be generated automatically, but somereason clion doesnt do it, intendation may be broken btw.

Upvotes: 2

Related Questions