Qt与C++:类"AddDialog"没有成员"carNameEdit"



我按照Qt的教程创建我的第一个项目。我已经重命名了一些小工具,但我一直在使用自己的名字。

但是,当我现在尝试在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:
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的所有元素都将成为小部件的子元素。

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

相关内容

  • 没有找到相关文章

最新更新