I don't think he ever clearly broke the 4th wall in the sense of acting as though he was aware he was a character on a TV show. But this line from "All Good Things..." was the closest I found:
Q: Oh, but it is, and we have. Time may be eternal, Captain, but our patience is not. It's time to put an end to your trek through the stars, make room for other more worthy species.
Of course you don't have to interpret this as Q being aware he's on the show "Star Trek", I took it as just a little title drop by the writers in keeping with this episode's feel as a grand closing statement on the show, but it is at least leaning on the fourth wall.
edit: Thanks to @T-1000's Son for pointing out that there is a fourth-wall-breaking moment in the (non-canon) game Star Trek: Borg. The complete set of scenes in the game can be seen in these six videos: one, two, three, four, five, six. In part 5, at about 6:10 in, there's a moment where the player character makes a foolish choice and Q (in the guise of a Borg) seems to give up on the player, saying "ugh, I think I need to renew myself in some alternate reality...c'mon everybody, let's go. Such an amateur." Then he walks off with the other characters as the dramatic music cuts out and suddenly we hear what sound like the voices of the film crew in the background (they speak in ordinary-sounding English even though the scene is set on a Borg ship), as if the director just called "cut". After that Q comes back and says "oh, all right", snaps his fingers, and the player character gets to go back to an earlier stage before they made the bad choice.