How to call a function from one class into another(main) class in Qt Creator?



  • Present State: I have designed a keypad GUI in qt creator by making a separate class and i have displayed the entered text locally in that GUI. But I need to merge this keypad with my main GUI.

    I have implemented'friend function concept' to achieve the task. But I m facing some errors in this

    I m attaching the code along with the question:

    keypad class file :

    kp16.h

    namespace Ui {
    class kp16;
    }

    class MainWindow;

    class kp16 :
    public QWidget
    {
    Q_OBJECT

    public:
    explicit kp16(QWidget *parent = 0);

    ~kp16();
    QString dat;
    void handles();

    private:
    Ui::kp16 *ui;

    friend class Mainwindow ; / /Now mainwindow class can access private members of kp16
    friend int MainWindow::inputdata( ); ERROR COMING / / Inputdata() function of mainwindow can access internal members of kp16

    };
    #endif //KP16_H

    mainwindow.h

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    int inputdata( kp16 val ); ERROR COMING //declare inputdata function in mainwindow
    int i=0;

    ~MainWindow();
    public slots:

    kp16.cpp

    kp16::kp16(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::kp16)
    {
    ui->setupUi(this);
    connect(ui->k_pushButton_2,SIGNAL(clicked()),this,SLOT(handles()));
    }

    kp16::~kp16()
    {
    delete ui;
    }

    void kp16::handles()
    {
    QPushButton* button = (QPushButton*)(sender());
    QString fd=button->text();
    dat.append(fd);
    ui->label->setText(dat);
    }

    mainwindow.cpp

    kp16 *kp; //creating instance of kp16

    int inputdata( kp16 val) // friend function definition
    {
    QString movedata=0;
    movedata=val.dat;
    return movedata.toInt();
    }

    void MainWindow::on_lineEdit_textEdited(const QString &arg1)
    {
    QString displayKeypad;
    displayKeypad= inputdata(*kp);

    ui->lineEdit->setText(displayKeypad);
    }

    ERRORS:

    1. error: prototype for 'friend int MainWindow::inputdata()'
    does not match any in class 'MainWindow'
    [in kp16.h]
    2.error: candidate is: int MainWindow::inputdata(kp16) [In mainwindow.h]


  • Lifetime Qt Champion

    Hi
    Why do you need a friend function ?
    To get data from kp16 you can just let it emit a signal and connect that to s slot in
    mainwindow to get what ever its sending.



  • Exactly. There is no use of the friend function over here


Log in to reply