Home > Back-end >  Qt with C : class "AddDialog" has no member "carNameEdit"
Qt with C : class "AddDialog" has no member "carNameEdit"

Time:03-18

i am following this tutorial from Qt to create my first porject. I have renamed some Widgets, but I am then consistently using my own names.

But when I now try to acces the text of a label in AddDialog, AddDialog doesn´t seem to have that Label, even tough the names are the same and I have it like in the tutorial.

Here is the code:

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  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  name="layoutWidget">
   <property name="geometry">
    <rect>
     <x>60</x>
     <y>50</y>
     <width>221</width>
     <height>131</height>
    </rect>
   </property>
   <layout  name="gridLayout">
    <item row="0" column="0">
     <layout  name="verticalLayout">
      <item>
       <layout  name="inputLayout">
        <item row="0" column="0">
         <widget  name="carNameText">
          <property name="text">
           <string>Car Name:</string>
          </property>
         </widget>
        </item>
        <item row="0" column="1">
         <widget  name="nameEdit">
          <property name="text">
           <string>Name</string>
          </property>
         </widget>
        </item>
        <item row="1" column="0">
         <widget  name="carModelText">
          <property name="text">
           <string>Car Model:</string>
          </property>
         </widget>
        </item>
        <item row="1" column="1">
         <widget  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  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  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>

Thanks

CodePudding user response:

carNameEdit and carModelEdit are attributes of the private ui object so you cannot access them, a possible solution is to make them public but another better option is to create public methods that expose the strings:

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();
}

Then change to:

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

CodePudding user response:

Small addition to good answer from @eyllanesc; You can use QObject::findChild Qt feature:

In your case it might be:

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

It works, because after ui.setupUi(this); all elements of your UI become children of your widget.

Quite useful for unit testing, but whether you should use it in your production code is a bit questionable.

  • Related