Есть у QLabel есть одна особенность. Её минимальный размер определяется текстом, который в неё записан. Это приводит к тому что длинный текст принудительно увеличивает ширину интерфейса.
В большинстве случаев это выглядит плохо.
Как с этим бороться?
🔸 Обрезать текст заранее, задав лимит по длине строки. В этом случае мы теряем часть визуальной информации. Не всегда угадаешь нужный размер. В разных OS шрифт используется разный.
🔸 Делать перенос строки. Тогда мы получим изменение размера в другую сторону, что тоже поломает интерфейс.
🔸 Переопределить paintEvent() и сделать кастомный рендеринг текста. Можно, но слишком сложно для такой задачи.
Проще всего обрезать текст под текущий размер виджета используя класс QFontMetrics.
Он имеет готовый метод elidedText(), который просто вызываем по событию resizeEvent.
Я также добавил установку ToolTip чтобы всегда можно было увидеть полный текст при наведении курсора.
🌎 Код здесь
#qt#source
🌴 Maldives Atolls — a natural wonder created from coral reefs and the crystal-clear waters of the Indian Ocean 🌊🐠
✨ The Maldives Atolls are the lowest-lying country in the world. They consist of hundreds of coral islands scattered over an area of more than 90,000 km² 🏝. This archipelago is a diver's dream 🤿 and a paradise for snorkeling enthusiasts
#Maldives | @voyage
🌴 Vaavu Atoll – a Maldivian paradise! 🐠
Seclusion in nature, comfort, and a stunning underwater world make this the perfect spot for both relaxation and adventure!
✨
#Maldives | @voyage
🌴 Vaavu Atoll – a Maldivian paradise! 🐠
Seclusion in nature, comfort, and a stunning underwater world make this the perfect spot for both relaxation and adventure!
✨🌊
📏
📏
📏
📏
📏
📏
📏
📏
#Maldives | @voyage
🌴 Maldives Atolls — a natural wonder created from coral reefs and the crystal-clear waters of the Indian Ocean 🌊🐠
✨ The Maldives Atolls are the lowest-lying country in the world. They consist of hundreds of coral islands scattered over an area of more than 90,000 km² 🏝. This archipelago is a diver's dream 🤿 and a paradise for snorkeling enthusiasts 🐬
📏
📏
📏
📏
📏
📏
📏
📏
#Maldives | @voyage
🌴 Vaavu Atoll – a Maldivian paradise! 🐠
📏
📏
📏
📏
📏
📏
📏
📏
Seclusion in nature, comfort, and a stunning underwater world make this the perfect spot for both relaxation and adventure!
✨🌊
📏
📏
📏
📏
📏
📏
📏
📏
#Maldives🇲🇻@voyage
🌴 Vaavu Atoll – a Maldivian paradise! 🐠
📏
📏
📏
📏
📏
📏
📏
📏
Seclusion in nature, comfort, and a stunning underwater world make this the perfect spot for both relaxation and adventure!
✨🌊
📏
📏
📏
📏
📏
📏
📏
📏
#Maldives🇲🇻@voyage
🌊 Shipwreck in Keyodhoo – the pearl of Vaavu Atoll!
🟠This sunken treasure lures divers from all over the world. Explore the deep and witness the magic of the underwater realm! 🐠🤿
📏
📏
📏
📏
📏
📏
📏
📏
#Maldives🇲🇻@voyage