Please i need help with this piece of Qt code.
It gives a compiled error "empList' was not declared in this scope" in the getValue() function.
This has got me stucked for a couple of weeks now retarding the pace i'm learning the language.
I would gladly appreciate any assistance.

//payroll.h
#ifndef PAYROLL_H
#define PAYROLL_H
#include <QtGui>
#include <QMainWindow>
class EmployeeForm;
class EmployeeData;

class Payroll: public QMainWindow
{
    Q_OBJECT
public:
    friend class Employeeform;
    Payroll(QWidget *parent = 0);
    QList <EmployeeData> *empList;
    EmployeeForm *registerForm;
    ~Payroll(){delete this;}
private:
    QListWidget *nameList;
    QLabel *centralWid;
    QPushButton *addbutton;
    QPushButton *delbutton;
    QPushButton *clsbutton;
    QPushButton *editbutton;
    QPushButton *upbutton;
    QPushButton *downbutton;
    QPushButton *quitbutton;
};

class EmployeeForm : public QWidget
{
        Q_OBJECT
public:
    EmployeeForm(QWidget *parent = 0);
    QLayout* windowLayout();
    void createWidget();
    EmployeeData *member;
public slots:
    void getValues();
private:
    QLabel *formLab;
    QString formTitle;
    QLabel *nameLab;
    QLabel *dateLab;
    QLabel *phoneLab;
    QLabel *emailLab;
    QLabel *hoursLab;
    QLineEdit *getName;
    QLineEdit *getPhone;
    QLineEdit *getEmail;
    QLineEdit *getDate;
    QSpinBox *hoursSpin;
    QSlider *hoursSlide;
    QPushButton *resetBtn;
    QPushButton *submitBtn;
    QPushButton *okBtn;
};

class EmployeeData
{
public:
    friend class EmployeeForm;
    friend class Payroll;
private:
    QString name;
    double basicSalary;
    int hrs;
    double grossSalary;
    double tax;
    double netSalary;
    double allowance;
    QString ID;
    QString phone;
    QString email;
};
#endif // PAYROLL_H



//main.cpp
#include <QApplication>
#include <QWidget>
#include "payroll.h"

Payroll::Payroll(QWidget *parent):QMainWindow(parent)
{
    centralWid = new QLabel(this);
    addbutton = new QPushButton("Add Member");
    delbutton = new QPushButton("Delete Member");
    clsbutton = new QPushButton("View Details");
    editbutton = new QPushButton("Edit Member");
    upbutton = new QPushButton("Shift Up");
    downbutton = new QPushButton("Shift Down");
    quitbutton = new QPushButton("Close");
    nameList = new QListWidget;
    empList = new QList<EmployeeData>;
    registerForm = new EmployeeForm;

    QVBoxLayout *btnLayout = new QVBoxLayout;
    QGridLayout *layout = new QGridLayout;
    btnLayout->addWidget(addbutton,0,0);
    btnLayout->addWidget(editbutton,1,0);
    btnLayout->addWidget(delbutton,2,0);
    btnLayout->addWidget(clsbutton,3,0);
    btnLayout->addWidget(downbutton,4,0);
    btnLayout->addWidget(upbutton,5,0);
    layout->addWidget(nameList,0,0);
    layout->addLayout(btnLayout,0,1);
    layout->addWidget(quitbutton,3,1,Qt::AlignRight);
    centralWid->setLayout(layout);

    for(int i=0; i<empList->size(); i++)
       { nameList->addItem(empList->at(i).name); }

    connect(addbutton, SIGNAL(clicked()), registerForm, SLOT(show()));
    connect(quitbutton, SIGNAL(clicked()), qApp, SLOT(quit()));
    this->setCentralWidget(centralWid);
    this->setMinimumSize(500,400);
}

EmployeeForm::EmployeeForm(QWidget *parent): QWidget(parent)
{
    this->createWidget();
    hoursSlide->setRange(6,16);
    hoursSlide->setFixedWidth(200);
    hoursSpin->setRange(6,16);
    hoursSpin->setFixedSize(70,25);
            connect(submitBtn, SIGNAL(clicked()), this, SLOT(getValues()));
            connect(hoursSpin, SIGNAL(valueChanged(int)),hoursSlide,SLOT(setValue(int)));
            connect(hoursSlide, SIGNAL(valueChanged(int)),hoursSpin,SLOT(setValue(int)));
            connect(okBtn, SIGNAL(clicked()), this, SLOT(close()));
    this->setLayout(windowLayout());
    this->setFixedSize(400,300);
}

void EmployeeForm::createWidget()
{
    formLab = new QLabel("<u>PLEASE ENTER YOUR DETAILS BELOW!</u>");
    nameLab = new QLabel("Name:");
    phoneLab = new QLabel("Phone Number:",this);
    emailLab = new QLabel("Email:", this);
    dateLab = new QLabel("Entry Date:", this);
    hoursLab = new QLabel("Hours Worked/Day:", this);
    hoursSpin = new QSpinBox(this);
    hoursSlide = new QSlider(Qt::Horizontal,this);
    getName = new QLineEdit(this);
    getEmail = new QLineEdit(this);
    getPhone = new QLineEdit(this);
    getDate = new QLineEdit(this);
    resetBtn = new QPushButton("Reset",this);
    submitBtn = new QPushButton("Submit",this);
    okBtn = new QPushButton("Done",this);
    member = new EmployeeData;
}

QLayout* EmployeeForm::windowLayout()
{
    QGridLayout *layout = new QGridLayout;
    QHBoxLayout *hlay = new QHBoxLayout;
    QHBoxLayout *getHours = new QHBoxLayout;
    getHours->addWidget(hoursSpin,Qt::AlignLeft);
    getHours->addWidget(hoursSlide,Qt::AlignRight);
    hlay->addWidget(resetBtn);
    hlay->addWidget(submitBtn);
    hlay->addWidget(okBtn);
    layout->addWidget(formLab,0,0,1,3, Qt::AlignCenter);
    layout->addWidget(nameLab,1,0);
    layout->addWidget(getName,1,1);;
    layout->addWidget(phoneLab,2,0);
    layout->addWidget(getPhone,2,1);
    layout->addWidget(emailLab,3,0);
    layout->addWidget(getEmail,3,1);
    layout->addWidget(dateLab,4,0);
    layout->addWidget(getDate,4,1);
    layout->addWidget(hoursLab,5,0);
    layout->addLayout(getHours,5,1,Qt::AlignLeft);
    layout->addLayout(hlay,6,0,1,2);
    return layout;
}

void EmployeeForm::getValues()
{
    member->name = getName->text();
    member->phone = getPhone->text();
    member->email = getEmail->text();
    member->hrs = hoursSpin->text().toInt();
    empList->append(*member);
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Payroll *window = new Payroll;
window->show();
return app.exec();
}

Recommended Answers

All 2 Replies

getValues is a member function of the EmployeeForm class. That class does not have a member called empList.

empList is a member of the Payroll class, so you can only access it through a Payroll object.

This is possibly a situation where the Payroll class should be a singleton (only one instance can exist), in which case, you could do something like thins:

Payroll::getInstance()->append(*member);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.