Important: Please read the Qt Code of Conduct -

Connecting action to QPushButton and Slots questions

  • I'm making a simple ATM machine application. I need to connect functions that are currently in my Accounts.h file like deposit() to buttons declared in main of my ATM.cpp file. Accounts.h and ATM.cpp are the only two files I currently have.

    I've been trying to connect the actions of deposit(), withdraw(), ect., to their respective buttons. The buttons are in the QWidget ATMWindow.

    Where would I put the public slots? Do I need to make a new header file for my QWidget?

  • I have 2 methods in mind:

    METHOD 1: Signal-Slot way
    In you Accounts.h, the functions that you need should be declared as "public slots".
    @public slots:
    void deposit();
    void withdraw();

    Then in your ATMWindow, use connect()
    @Accounts *accounts=new Account();//just guessing if your class name is "Accounts"

    METHOD 2: Just a public function
    In you Accounts.h, the functions that you need should be declared as "public".
    void deposit();
    void withdraw();

    Suppose you are using .ui file in Qt Creator, just right click onto a button then look for "go to slot" then find "clicked". It will generate a function for you where you can call the functions:

    @void ATMWindows::on_depositbutton_clicked() //just an example... this should be generated by Qt Creator
    accounts->deposit(); //make sure youu have declared accounts in the header file so that it can be accessed globally [in your class]

Log in to reply