Reputation: 715
I am trying to follow along with the Qt5 Visual Studio tutorial here: http://doc.qt.io/qtvstools/qtvstools-getting-started.html
The tutorial makes a main window, and a popup dialog box. The main window is working, and the dialog box is presented as it should when you click the "Add" button on the main window.
In the dialog box, there are two QLineEdit objects that I would like to get the value from. But I cannot seem to access these members of the dialog.
The two objects within the dialog window are "nameEdit" (highlighted) and "emailEdit" (below it):
The errors I get:
-"class 'AddDialog' has no member 'nameEdit'.
-"class 'AddDialog' has no member 'emailEdit'
Can you see what is the reason I cannot access members of my AddDialog class?
Here is the AddressBook.cpp file (my main window).
#include "AddressBook.h"
#include "AddDialog.h"
#include "ui_AddDialog.h"
AddressBook::AddressBook(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
void AddressBook::on_addButton_clicked()
{
AddDialog dialog(this);
if (dialog.exec())
{
QString name = dialog.nameEdit->text();
QString email = dialog.emailEdit->text();
if (!name.isEmpty() && !email.isEmpty())
{
QListWidgetItem *item = new QListWidgetItem(name, ui.addressList);
item->setData(Qt::UserRole, email);
ui.addressList->setCurrentItem(item);
}
}
}
void AddressBook::on_addressList_currentItemChanged()
{
QListWidgetItem *curItem = ui.addressList->currentItem();
if (curItem) {
ui.nameLabel->setText("Name: " + curItem->text());
ui.emailLabel->setText("Email: " + curItem->data(Qt::UserRole).toString());
}
else {
ui.nameLabel->setText("<No item selected>");
ui.emailLabel->clear();
}
}
The AddDialog.h (dialog box):
#include <QDialog>
#include "ui_AddDialog.h"
class AddDialog : public QDialog
{
Q_OBJECT
public:
AddDialog(QWidget *parent = Q_NULLPTR);
~AddDialog();
private:
Ui::AddDialog ui;
};
And AddDialog.cpp (the dialog box):
#include "AddDialog.h"
AddDialog::AddDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
}
I tried to just copy what I think is the relevant code to the issue, but just in case, here is everything else if it is needed: https://github.com/davek99/DK-QT_Address_Book/
Upvotes: 2
Views: 766
Reputation: 866
The errors I get:
-"class 'AddDialog' has no member 'nameEdit'
-"class 'AddDialog' has no member 'emailEdit'
So the compiler is telling you that there is no member "nameEdit" or "emailEdit" in your AddDialog class.
So, if you take a look at the AddDialog class header file, you can see there is only one class member declared, "Ui::AddDialog ui".
So you expected "nameEdit" and "emailEdit" to be there as well, but they are not. So where are they? Well, they are in the Ui::AddDialog class.
So, if you take a look at the generated ui_AddDialog.h, you will see that the "nameEdit" and "emailEdit" members are in that class.
So in order to access these two members from your AddDialog class, you need to make accessor functions available in AddDialog that reference them from the included Ui::AddDialog class.
For example:
QString AddDialog::name() {
return ui.nameEdit->text();
}
So then when you use AddDialog in your AddressBook class, you can simply call dialog.name().
I hope this is clear, let me know if you don't understand any part.
Upvotes: 1