In recent years, Wireless Sensor Networks (WSNs) have become a key technology for monitoring and tracking applications in a wide application range. A wireless sensor network (WSN) senses the environment, collects data, and sends it to a base station for analysis. A fundamental challenge in designing WSNs is maximizing their lifetimes, especially when energy is limited. Managing trust in the WSN is also a difficult task because trust is used when collaboration is critical to achieving reliable communication. As a result, this paper proposed a secure and energy-aware data aggregation and optimal routing scheme for WSNs based on the Quantum behaviour and Gaussian Mutation based Archimedes Optimization Algorithm (QGAOA). The proposed system comprises three parts: cluster formation, cluster heads (CHs) selection, and optimal routing. Initially, clusters are formed using the Voronoi-included K-means clustering algorithm. Then CHs are selected, and optimal routing is selected using QGAOA. Simulations are carried out to analyze the performance effectiveness of the proposed system with existing related techniques regarding some performance metrics. The system achieves an average network lifetime of 245.2 rounds, energy consumption of 4865.42 J, throughput of 95.6%, a delay of 14.84 ms, a packet delivery rate of 94.87%, and network reliability of 98.53%, which are superior to the existing methodologies for trust-based data aggregation in WSNs.