How to interpret $\langle x|\hat Q|x'\rangle$ for a quantum mechanical operator $\hat Q$? I stumbled on this question while answering the following question:
Given a general wavefunction $\psi(x)$ and an operator $\hat Q$, with eigenvalue spectrum $\{q_r\}$ and corresponding eigenfunctions $\{u_r\left(x\right)\}$, show that the expectation value of $\hat Q$ is given by $$\langle\hat Q\rangle = \int_{-\infty}^\infty{\psi^*\hat Q \psi \,dx}$$
I started of with the ususal definition of the expected value and inserted the identity operator in position representation: $$\langle\hat Q\rangle = \langle \psi|\hat Q|\psi\rangle = \langle \psi|\left(\int dx|x\rangle\langle x|\right) \hat Q\left(\int  {dx}'|x'\rangle\langle x'|\right)|\psi\rangle$$
Taking $\langle\psi|$ and $|\psi\rangle$ into the integral gives: $$\left(\int dx\langle \psi|x\rangle\langle x|\right) \hat Q\left(\int  {dx}'|x'\rangle\langle x'|\psi\rangle\right)$$
Pulling the 2nd integral into the first:$$\int dx\int  {dx}'\langle \psi|x\rangle\langle x'|\psi\rangle\langle x| \hat Q|x'\rangle$$
This is the point where I'm stuck. If $\langle x|\hat Q|x'\rangle = \hat Q \delta(x-x')$, then the last expression would evaluate to the answer. However I  don't know why this should be true and even if it is true I wouldn't know how to prove this equality.
 A: It is not true in general that $\langle x|\hat{Q}|x'\rangle = \hat{Q}\delta(x-x')$. What is happening here is that you are using the symbol $\hat{Q}$ to mean 2 slightly different objects.
What is the relationship between $\hat{p}$ and $-\imath\hbar\frac{\partial}{\partial x}$? We generally say that 
$$\hat{p} = -\imath\hbar\frac{\partial}{\partial x}$$
however this lead to writing things like $-\imath\hbar\frac{\partial}{\partial x}|\psi\rangle$. The problem with this expression is that $|\psi\rangle$ is not a function of $x$, so we can't talk about its $x$ dependence, let alone differentiate it. The problem here is that $\hat{p}$ operates on the space of states while $-\imath\hbar\frac{\partial}{\partial x}$ operates on the space of wavefunctions. The correct relation between them is 
$$
\langle x|\hat{p}|\psi\rangle = -\imath\hbar\frac{\partial}{\partial x}\langle x|\psi\rangle\;.
$$Now there is a very natural relationship between these space 
\begin{align}
\psi(x) &= \langle x|\psi\rangle \\
|\psi\rangle &= \int\mathrm{d}x\;\psi(x)|x\rangle
\end{align}
so generally identifying operators on the 2 spaces does not cause much confusion.
Now in your case in the problem is that you are using the same symbol $\hat{Q}$ to refer to the operator on both that space of states, eg $\langle x|\hat{Q}|x'\rangle$ and the operator in the space of wavefunctions, eg. $\psi^*\hat{Q}\psi$. The solution is that you must use equivalent of the relation we used for the momentum operator above. This has the slightly confusing form
$$
\langle x|\hat{Q}|\psi\rangle = \hat{Q}\psi(x)
$$
where $\hat{Q}$ operates on kets on the left hand side and on wavefunctions on the right hand side. 
Alternatively we can do the equivalent of applying the momentum operator by fourier transforming and then multiplying by $p$. In this case you insert the identity into the above expression to obtain
$$\int \mathrm{d}x'\; \langle x|\hat{Q}|x'\rangle\langle x'|\psi\rangle = \hat{Q}\psi(x)$$
where, again, $\hat{Q}$ on the left hand side operates on the space of states and $\hat{Q}$ on the right hand side operates on the space wavefunctions.
A: The question that you were trying to answer is incorrect. If a system is in a state $|\Psi \rangle$ then the expectation values is indeed 
$$
\langle Q \rangle = \langle \Psi | Q |\Psi \rangle
$$
by definition and this is precisely
$$
\int dx \int dx' \Psi(x) \Psi^*(x') \langle x |Q | x' \rangle
$$
and unless $Q$ commutes with $x$ you in general do not have $\langle x |Q | x' \rangle= Q(x) \delta(x-x')$.
In fact perhaps the trivialest example of an operator that does not commute with position is momentum (Uncertainty principle). Since momementum is a generator of translation we immediate see that the the following translation opearator
$$
Q(a) = \int dx~ |x+a \rangle \langle x|
$$
is an example of the above for $a \ne 0$.
A: Further to By Symmetry's answer, from $\int dx' \langle x|\hat{Q}| x'\rangle\psi(x') = \hat{Q}\psi(x)$ we obtain the functional derivative $\langle x|\hat{Q}| x'\rangle=\dfrac{\delta \hat{Q}\psi(x)}{\delta \psi(x')}$. As a sanity check we consider the special case where $\hat{Q}$ is a c-number valued function of $x$. Since $\dfrac{\delta \psi(x)}{\delta \psi(x')}:=\delta(x-x')$, this special case gives the expected $\langle x|\hat{Q}| x'\rangle=Q(x)\delta(x-x')$  since $\dfrac{\delta Q(x)}{\delta \psi(x')}=0$. (If you prefer, you can see this argument as motivating that last equation.)
