我正在尝试使用 Qt Creator 编译用 C++ 编写的代码。我正在尝试创建第二个窗口,该窗口将在单击主窗口中的菜单项后出现,并且该窗口应具有单独的 .ui 文件。但是在编译期间,我收到此错误:

/home/martin/Code/C++/Map_generator/map_generator/preferenceswindow.cpp:4: error: prototype for 'Ui::PreferencesWindow::PreferencesWindow(QWidget*)' does not match any in class 'Ui::PreferencesWindow'
 Ui::PreferencesWindow::PreferencesWindow(QWidget *parent)
 ^

/home/bo/Code/C++/Map_generator/build-map_generator-Desktop-Debug/ui_preferenceswindow.h:88: error: candidates are: constexpr Ui::PreferencesWindow::PreferencesWindow(Ui::PreferencesWindow&&)
     class PreferencesWindow: public Ui_PreferencesWindow {};
           ^

有什么事吗

preferenceswindow.h
#ifndef PREFERENCESWINDOW_H
#define PREFERENCESWINDOW_H

#include <QWidget>

namespace Ui
{
    class PreferencesWindow;
}

class PreferencesWindow : public QWidget
{
    Q_OBJECT
public:
    explicit PreferencesWindow(QWidget *parent = 0);

signals:

//public Q_SLOTS:
private:
    PreferencesWindow *uip;
};
#endif // PREFERENCESWINDOW_H

preferenceswindow.cpp
#include "preferenceswindow.h"
#include "ui_preferenceswindow.h"

Ui::PreferencesWindow::PreferencesWindow(QWidget *parent)
    :QWidget(parent), uip(new Ui::PreferencesWindow)
{
    uip->setupUi(this);
}

preferenceswindow.ui文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>PreferencesWindow</class>
 <widget class="QWidget" name="PreferencesWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>326</width>
    <height>159</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>200</x>
     <y>100</y>
     <width>99</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text">
    <string>Save</string>
   </property>
  </widget>
  <widget class="QSpinBox" name="spinBox">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>100</y>
     <width>81</width>
     <height>27</height>
    </rect>
   </property>
   <property name="minimum">
    <number>1</number>
   </property>
   <property name="maximum">
    <number>200</number>
   </property>
   <property name="value">
    <number>15</number>
   </property>
  </widget>
  <widget class="QSpinBox" name="spinBox_2">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>20</y>
     <width>81</width>
     <height>27</height>
    </rect>
   </property>
   <property name="minimum">
    <number>1</number>
   </property>
   <property name="maximum">
    <number>1000</number>
   </property>
   <property name="value">
    <number>100</number>
   </property>
  </widget>
  <widget class="QSpinBox" name="spinBox_3">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>60</y>
     <width>81</width>
     <height>27</height>
    </rect>
   </property>
   <property name="minimum">
    <number>1</number>
   </property>
   <property name="maximum">
    <number>1000</number>
   </property>
   <property name="value">
    <number>60</number>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>30</y>
     <width>67</width>
     <height>17</height>
    </rect>
   </property>
   <property name="text">
    <string>Columns</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>70</y>
     <width>67</width>
     <height>17</height>
    </rect>
   </property>
   <property name="text">
    <string>Rows</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_3">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>110</y>
     <width>67</width>
     <height>17</height>
    </rect>
   </property>
   <property name="text">
    <string>Grid Size</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

最佳答案

当您希望将uip变量声明为PreferencesWindow时,将其声明为Ui::PreferencesWindow:

private:
    PreferencesWindow *uip;

更改为
private:
    Ui::PreferencesWindow *uip;

您还需要在 preferenceswindow.cpp 中定义Ui::PreferencesWindow构造函数。这不是您想要的。您要在那里定义PreferencesWindow构造函数。像这样:
PreferencesWindow::PreferencesWindow(QWidget *parent)
    :QWidget(parent), uip(new Ui::PreferencesWindow)
{
    uip->setupUi(this);
}

09-08 09:29