Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connecting action to QPushButton and Slots questions
Qt 6.11 is out! See what's new in the release blog

Connecting action to QPushButton and Slots questions

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.5k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    EStudley
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Code_ReaQtor
      wrote on last edited by
      #2

      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();
      //etc@

      Then in your ATMWindow, use connect()
      @Accounts *accounts=new Account();//just guessing if your class name is "Accounts"
      QOBject::connect(depositbutton,SIGNAL(clicked(),accounts,SLOT(deposit()));
      QOBject::connect(withdrawbutton,SIGNAL(clicked(),accounts,SLOT(withdraw()));@

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

      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]
      }@

      Please visit my open-source projects at https://github.com/Code-ReaQtor.

      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