#includeint main(int argc, char *argv[]) { QLabel label("Hello World!"); label.setFixedWidth(100); label.show(); return 0; }
#includeThis code creates a QString variable that contains the text to be displayed in the label. The length of the text is calculated and multiplied by 10 to get the desired width in pixels. The QLabel widget is created with the text and the setFixedWidth function is called with the calculated width. The widget is then displayed on the screen using the show() function. Package library: Qt (C++ library for developing cross-platform applications)#include int main(int argc, char *argv[]) { QString text = "This text is longer"; int width = text.length() * 10; QLabel label(text); label.setFixedWidth(width); label.show(); return 0; }