The general solution to the time-harmonic wave equation on a string may be written as
$$y(x,t)=\sin(\omega t)\left[A\sin(kx) + B\cos(kx)\right].$$
The condition at $x=0$ is that $y(0,t)=A_0\sin(\omega t)$, and so we find that $B=A_0$.  We further require that $y(L,t)=0$, where $L$ is the distance between $P$ and $Q$.  This requirement yields
$$A\sin(kL) + A_0\cos(kL) = 0,$$
or
$$ A = -A_0\frac{\cos(kL)}{\sin(kL)}. $$
The total solution is then given by 
$$ y(x,t) = A_0\left[\frac{\sin(\omega t)\cos(kx)\sin(kL)}{\sin(kL)} - \frac{\sin(\omega t)\sin(kx)\cos(kL)}{\sin(kL)}\right]. $$
Using trigonometric identities we may then write
$$y(x,t) = A_0\left[\frac{\cos(\omega t-kx+kL)}{2\sin(kL)} - \frac{\cos(\omega t +kx-kL)}{2\sin(kL)}\right].$$

Notice that the first term in the square brackets represents the forward-propagating wave (from $P$ to $Q$), while the second term represents the backward propagating wave.  Further notice that the two terms have opposite sign when evaluated at $x=L$.  This opposite sign could also be considered a factor of $\cos(\pi)$.  Hence, the reflection at the rigid interface yields a phase factor of $\pi$:
$$y(x,t) = A_0\left[\frac{\cos(\omega t-kx+kL)}{2\sin(kL)} + \frac{\cos(\omega t +kx-kL+\pi)}{2\sin(kL)}\right].$$