Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. India
  4. How to call a function from one class into another(main) class in Qt Creator?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved India
3 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SHUBHAM SINGH RAO
    wrote on last edited by SHUBHAM SINGH RAO
    #1

    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]

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      3
      • E Offline
        E Offline
        ethanscott
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved