On pages 100-103 of volume 2 of The Psalms in Israel's Worship, Sigmund Mowinckel speculates as to how certain Psalms got to be attributed to particular events in David's life, or to Moses, or Solomon. Mowinckel does not think that le-David necessarily means that a Psalm was composed by David, but rather that it was composed for the use of David's house. After all, Mowinckel argues, Psalm 18:50 refers to David's descendants, which indicates that the Psalm here applies to one of David's descendants! But, at some point, le-David was understood to mean that a Psalm was composed by David. And, through a process of darash, Psalms were related to specific events in the life of David, Moses, or Solomon.
For example, Psalm 18 appears to pertain to the deliverance of the Psalmist from foreign enemies in battles, and David was someone who fought many battles with foreign enemies. Consequently, the Psalm was related to David after he had been delivered from his enemies, and from Saul. (Many say that the "Saul" part was inserted later, on account of its awkwardness.)
In Psalm 63:3, the Psalmist says that his "flesh longeth for God in a dry and thirsty land", and the scribes took this literally and applied the Psalm to David's experience in the wilderness of Judah.
Psalm 52 was related to Doeg telling Saul that David had come to Abimelech, even though it's not a perfect fit: the Psalm says that the enemy lied and hurt the Psalmist, when Doeg did not lie, and his deed hurt the priests, not David himself. But the Psalm mentions a gibbor, and I Samuel 21:8 and 22:17ff. appear to class Doeg with the king's gibborim, and so the scribes said that the Psalm was about Doeg.
Psalm 90 is ascribed to Moses, which Mowinckel says does not work, for the Psalm is individualistic, which indicates its lateness, plus it doesn't appear to be the work of a young nation in the wilderness, but rather concerns a settled people suffering at the hands of greedy people and armies. But scribes saw things in the Psalm that reminded them of the generation that died in the wilderness, in the time of Moses. The Psalmist lamented that people die at age seventy, which was applied to the wilderness generation---since the age of adulthood is thirty, and the wanderers were in the wilderness for forty years. Forty years were about to be up, and they'd be dead, unable to enjoy the Promised Land. And the Psalm also asks that the worshipers of the LORD see God's work, while their sons see God's glory. For the scribes, the implication here is that only the sons---of the wilderness generation---would see God's glory, by entering the Promised Land.
Psalm 72 is applied to Solomon, for it talks about a glorious king, who is also a king's son (and thus not David), and whom shall be given the gold of Sheba.
According to Mowinckel, the scribes did not arbitrarily apply Psalms to certain events in David's life, or to Moses, or to Solomon. Rather, they based their ascriptions on what they thought were clues within the Psalms themselves.