The Q-Ship was created (in its modern form) by the Royal Navy during World War I. WWI saw the first modern use of submarine warfare on a massive scale, as the German U-boat flotilla attempted to blockade the British Isles. The intent was to cut off Britain from both resupply and, via blocking its merchant marine, one of its primary sources of income. The situation began to look desperate for the British, with over half a million tons of shipping sunk monthly for much of 1915.
The convoy system was effective at reducing losses, but it meant that U-boats could prey upon solo ships with near impunity as most ASW craft were tied up escorting the convoys. Even in convoy, the escorts couldn't be everywhere. Any and all means of blunting the U-boat force was duly considered. The Q-Ships were one of them. Named for their initial base (Queenstown, Ireland) and the initial numbers assigned to them in RN service (Q-prefixed), the Q-ships were intended to take advantage of two things - cruiser warfare rules and the relative scarcity of torpedos on U-boats on patrol. It was becoming clear the expenditure of torpedoes was turning out to be the prime limiter of U-boat patrol times.
Cruiser warfare was defined in international law, and there was great argument over whether it applied to U-boats. In essence, cruiser warfare required a naval vessel attacking a civilian ship to first declare its intentions and thus give the crew and passengers a chance to abandon ship. The U-boats strongly preferred to ignore the cruiser warfare rules, as on the surface the fragility and awkwardness of these craft was at its most dangerous. However, there was another reason to fight under cruiser warfare rules - U-boats were equipped with deck guns. Deck-mounted naval cannon could be used to sink relatively fragile merchant ships quite effectively, and each ship sunk using these weapons meant torpedoes saved.
As a consequence of this, and most especially as a consequence of the fact that submarines of the day were much, much slower submerged than on the surface, the majority of attacks from U-boats tended to come from surfaced units. Given this, the Royal Navy saw an opening. The Q-ship was an ostensibly merchant ship - small freighter, trawler, or tanker - that was in fact equipped with ASW armaments hidden or disguised by false structures or paint. The notion was that when a U-boat closed to attack a Q-ship, the latter would wait until it was in range before hauling its naval colors up the mast, exposing its hidden guns, and blasting away. Many were equipped with hedgehogs and depth charges to continue the attack if their target submerged, and they all carried small arms.
Although some U-boats were sunk by RN Q-ships in WWI (14 of them) the U-boats destroyed nearly double that number of Q-ships, and many more of the stealthy fighters spent uneventful tours steaming back and forth across the ocean, never sighting the enemy (or unable to bring them to combat). After the first few engagements, the U-boat force quickly dispersed the news that the Q-ships existed, and German submarine crews traded all information on Q-ships they saw, enabling the submarines - as the offense - to decline engagement with ships they recognized.
World War II, which also saw U-boats attempting to strangle the UK, also saw Q-ship use. However, by this time, submarines were engaged in 'unrestricted submarine warfare' - i.e. submerged surprise attacks, preferably at night, with no warning - and thus Q-ships were of little value. The U.S. operated several for a time as well, with almost no useful results. By the end of the war, they had been converted for use in other duties. As many pointed out, the money and crew used to support their operation would probably have been more useful employed elsewhere, and in World War I, minefields sank more U-boats than Q-ships did. Those were demonstrably cheaper, and didn't take crew away from other duties.
(IN 5 28/30)