Есть у QLabel есть одна особенность. Её минимальный размер определяется текстом, который в неё записан. Это приводит к тому что длинный текст принудительно увеличивает ширину интерфейса.
В большинстве случаев это выглядит плохо.
Как с этим бороться?
🔸 Обрезать текст заранее, задав лимит по длине строки. В этом случае мы теряем часть визуальной информации. Не всегда угадаешь нужный размер. В разных OS шрифт используется разный.
🔸 Делать перенос строки. Тогда мы получим изменение размера в другую сторону, что тоже поломает интерфейс.
🔸 Переопределить paintEvent() и сделать кастомный рендеринг текста. Можно, но слишком сложно для такой задачи.
Проще всего обрезать текст под текущий размер виджета используя класс QFontMetrics.
Он имеет готовый метод elidedText(), который просто вызываем по событию resizeEvent.
Я также добавил установку ToolTip чтобы всегда можно было увидеть полный текст при наведении курсора.
🌎 Код здесь
#qt#source
🚨 Urgent Update: Gaza Humanitarian Crisis 🚨
WHO warns of a "death sentence" for vulnerable hospital patients in Gaza due to Israel's evacuation order. Moving them within 24 hours is impossible and inhumane. UN calls it "impossible" and EU diplomat calls it "utterly unrealistic". Hospitals are at breaking point, with limited electricity and critical shortages. Time is running out to prevent a catastrophe. Urgent delivery of supplies and a humanitarian corridor needed. #SaveGaza#HumanitarianCrisis
https://www.aljazeera.com/news/2023/10/13/israels-evacuation-order-for-gaza-death-sentence-for-patients-who-says
Subscribe to @BadVolfNews
🚨GENOCIDE ALERT🚨 Gaza is being subjected to a horrifying campaign of violence. Over 11,000 dead, including 1,400 children. Hospitals and schools targeted. Palestinians denied access to basic necessities. This is genocide. We must recognize the humanity of the Palestinian people and demand an end to this atrocity. #SaveGaza#EndGenocide
https://www.aljazeera.com/opinions/2023/11/12/genocide-in-gaza-a-call-for-urgent-global-action
Subscribe to @BadVolfNews
🔻 Gaza Strip: A graveyard for children. UN warns of more deaths due to dehydration amid Israel's relentless attacks. Over 8,500 Palestinians, mostly children and women, killed. Shocking figures reveal 420 children killed or injured daily. Ceasefire and humanitarian aid urgently needed. Water crisis and trauma pose additional threats. Mental health support desperately required for over 800,000 children. Collateral damage? No safe place in Gaza. International attention needed! #SaveGaza#ChildrenUnderAttack
https://www.aljazeera.com/news/2023/10/31/gaza-has-become-a-graveyard-for-thousands-of-children-un
Subscribe to @BadVolfNews