Qt with C++: class "AddDialog" 没有成员 "carNameEdit"

Qt with C++: class "AddDialog" has no member "carNameEdit"

我正在关注 Qt 中的这个 tutorial 以创建我的第一个项目。我已经重命名了一些小部件,但我一直在使用我自己的名字。

但是当我现在尝试在 AddDialog 中访问标签的文本时,AddDialog 似乎没有那个标签,即使名称是相同的,我在教程中也有它。

代码如下:

carrental.cpp

#include "carrental.h"
#include "stdafx.h"
#include "adddialog.h"
#include "ui_adddialog.h"

CarRental::CarRental(QWidget* parent)
    : QWidget(parent)
{
    ui.setupUi(this);
}

void CarRental::on_addButton_clicked()
{
    AddDialog dialog(this);

    if (dialog.exec()) {
        QString carName = dialog.carNameEdit->text();
        QString carModel = dialog.carModelEdit->text();

        if (!carName.isEmpty() && !carModel.isEmpty()) {
            QListWidgetItem* item = new QListWidgetItem(carName, ui.carList);
            item->setData(Qt::UserRole, carModel);
            ui.carList->setCurrentItem(item);
        }
    }
}

adddialog.h:

#pragma once

#include <QDialog>
#include "ui_adddialog.h"

class AddDialog : public QDialog
{
    Q_OBJECT

public:
    AddDialog(QWidget* parent = Q_NULLPTR);

private:
    Ui::AddDialog ui;
};

adddialog.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>AddDialog</class>
 <widget class="QDialog" name="AddDialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>345</width>
    <height>230</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>AddDialog</string>
  </property>
  <widget class="QWidget" name="layoutWidget">
   <property name="geometry">
    <rect>
     <x>60</x>
     <y>50</y>
     <width>221</width>
     <height>131</height>
    </rect>
   </property>
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
       <layout class="QGridLayout" name="inputLayout">
        <item row="0" column="0">
         <widget class="QLabel" name="carNameText">
          <property name="text">
           <string>Car Name:</string>
          </property>
         </widget>
        </item>
        <item row="0" column="1">
         <widget class="QLineEdit" name="nameEdit">
          <property name="text">
           <string>Name</string>
          </property>
         </widget>
        </item>
        <item row="1" column="0">
         <widget class="QLabel" name="carModelText">
          <property name="text">
           <string>Car Model:</string>
          </property>
         </widget>
        </item>
        <item row="1" column="1">
         <widget class="QLineEdit" name="carModelEdit"/>
        </item>
       </layout>
      </item>
      <item>
       <spacer name="verticalSpacer">
        <property name="orientation">
         <enum>Qt::Vertical</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>20</width>
          <height>40</height>
         </size>
        </property>
       </spacer>
      </item>
     </layout>
    </item>
    <item row="1" column="0">
     <layout class="QHBoxLayout" name="okLayout">
      <item>
       <spacer name="horizontalSpacer">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>40</width>
          <height>20</height>
         </size>
        </property>
       </spacer>
      </item>
      <item>
       <widget class="QPushButton" name="okButton">
        <property name="text">
         <string>OK</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections>
  <connection>
   <sender>okButton</sender>
   <signal>clicked()</signal>
   <receiver>AddDialog</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>251</x>
     <y>167</y>
    </hint>
    <hint type="destinationlabel">
     <x>100</x>
     <y>196</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

谢谢

carNameEdit 和 carModelEdit 是私有 ui 对象的属性,因此您无法访问它们,一个可能的解决方案是使它们 public 但另一个更好的选择是创建 public 方法公开字符串:

public:
    AddDialog(QWidget* parent = Q_NULLPTR);
    QString carName() const;
    QString carModel() const;
private:
    Ui::AddDialog ui;
QString AddDialog::carName() const
{
    return ui.carNameEdit->text();
}

QString AddDialog::carModel() const
{
    return ui.carModelEdit->text();
}

然后改为:

QString carName = dialog.carName();
QString carModel = dialog.carModel();

来自@eyllanesc 的好回答的小补充;您可以使用 QObject::findChild Qt 功能:

你的情况可能是:

QLineEdit *carModelEdit= dialog.findChild<QLineEdit*>("carModelEdit");
qDebug() << carModelEdit->text();

它有效,因为在 ui.setupUi(this); 之后,您 UI 的所有元素都成为您的小部件的子元素。

对于单元测试非常有用,但是是否应该在生产代码中使用它有点值得怀疑。