Alexandre Bissonnette, a 29-year-old former politics student fixated on President Trump, the far right and Muslims, was sentenced on Friday to life in prison without the possibility of parole for 40 years for shooting six people dead in an attack on a mosque in Quebec City in January 2017.

During the attack, Mr. Bissonette shot several worshipers in the head. Nineteen people were injured, including one who was paralyzed for life.

Under Canadian law, Mr. Bissonnette could have gone to prison for 150 years — or 25 years for each of the six deaths. While underscoring the brutality of the attack, Justice François Huot of Quebec Superior Court suggested that such a harsh sentence would be excessive by denying the defendant the hope of ever leaving prison.

But Muslim leaders, including the mosque’s president, Mohamed Labidi, said they were deeply disappointed by Justice Huot’s sentence, saying it did not do justice to a horrific crime.

“This rampage left children without parents, destroyed lives, and this man can be free after 40 years?” he asked with incredulity. “We are very disheartened and upset.”

Justice Huot announced the sentence after a hearing of more than five hours, during which he gave a minute-by-minute account of the rampage, which he said was “premeditated, gratuitous and abject” and motivated by “visceral hatred toward Muslims.”

Several family members of victims sobbed.

Legal scholars said his decision was likely to be challenged on appeal and could end up before Canada’s Supreme Court. And once there, they said, it could become a seminal test of the constitutionality of consecutive life sentences.

Justice Huot discussed the 2011 Canadian criminal law, introduced by the previous Conservative government, that allows a judge to give sentences in 25-year increments in cases of multiple murders.

He said that a 25-year sentence was not severe enough in this case, given the severity of the crime, but that 50 years or more would be excessive.