The use of Active Power Filters (APFs) in future power grids with high penetration of nonlinear loads is unavoidable. Voltage Source Inverters (VSIs) interfacing Photovoltaic (PV) generator could play the APF role in addition to power supply. In this paper, the control of a PV-fed multifunctional grid-connected three-phase VSI is addressed with nonlinear and unbalanced load. The control objective is threefold. The first one is to deliver the maximum available power from the PV source to the grid satisfying power quality standards. The second one is the Voltage Balancing Control for DC-link capacitors to guarantee correct operation of the VSI. The last one is shunt APF control to compensate for nonlinear and unbalanced load harmonics, reactive power, and unbalanced sequences. A quasi-Proportional-Resonant (PR) controller with harmonic compensators is proposed for the current control loop. The quasi-PR controller parameters are determined through optimization algorithms such as Particle Swarm Optimization (PSO), Genetic Algorithm (GA), and a combination of both PSO and GA. The aim of the objective function is to improve static and dynamic behavior. The different gains at the fundamental resonant frequency and the selected odd harmonics are obtained for the proposed quasi-PR controller. The dynamic characteristics of the optimized quasi-PR controllers show superiority against conventional ones in terms of gain margin, phase margin, overshoot, and robustness. With the proposed control scheme, the harmonics, reactive power, and unbalanced sequences are appropriately compensated. The performance of the PV-fed VSI shunt APF under irradiance change, load change, and distorted grid voltage conditions is validated through numerical simulations performed on hbox {PSIM}^{copyright } software. The results show that the grid currents Total Harmonic Distortion for irradiance change case study are 4.57%, 4.57%, and 3.22% in phase a, b, and c with the proposed control method, while they are 9.25%, 5.65%, and 10.12% with conventional instantaneous p-q theory.