...
One theory alleges that it was a warning to French gentlemen learning formal dancing in the 18th century to mind where they placed their feet (pieds) and watch that the pigtails on their wigs didn’t flick across the faces of other dancers.
A second theory posits that it was a warning to type-setters when setting the lower-case letters p and q. These letters, when back-to-front in printers’ cases of lead type, were difficult to tell apart. The wrong choice might produce an embarrassing misprint on the page.
Most likely is that Ps and Qs are “pleases and thankyous”; in other words, it’s a warning (particularly to children) to be polite when visiting someone else’s home or entertaining them in your own.