In McKenna v. Wright, a panel of the Second Circuit discussed the interesting question of whether the issue of qualified immunity can be raised on a motion to dismiss. Their answer was yes, which is surely the right answer, even though I disagree with many things they said.
The Supreme Court precedents emphasize that qualified immunity should be considered at the earliest possible stage. Even where the trial court is convinced that the plaintiff has not failed to state a claim, qualified immunity also involves the issue of whether the plaintiff's rights were clearly established at the time of the defendant's actions. If the law was unclear, as it often is, then the defendant should win, even on a motion to dismiss.
I certainly agree, however, with the point made in the McKenna opinion that only on a motion for summary judgment can the issue be litigated without encumbrance from unsupportable allegations in the Complaint, which must be taken as true on a motion to dismiss.