I want the QLabel to take the size of the text. Following doesn't work. It has made QLabel quite big by default.
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QLabel m_QLabel_choose_interval;
};
#endif // MAINWINDOW_H
.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
m_QLabel_choose_interval.setParent (this);
m_QLabel_choose_interval.setText ("Choose interval:");
m_QLabel_choose_interval.setFrameStyle (QFrame::Panel | QFrame::Sunken);
m_QLabel_choose_interval.setAlignment (Qt::AlignCenter);
}
m_QLabel_choose_interval
is declared as a class member.
I tried what's suggested in this link: Dynamic text size QLabel
r = m_QLabel_choose_interval.fontMetrics().boundingRect("My text");
m_QLabel_choose_interval.setText ("Choose interval:");
m_QLabel_choose_interval.setFrameStyle (QFrame::Panel | QFrame::Sunken);
m_QLabel_choose_interval.setAlignment (Qt::AlignCenter);
m_QLabel_choose_interval.setFixedWidth (r.width());
m_QLabel_choose_interval.setFixedHeight (r.height());
This resulted in the label being too small for the text. Part of the text was hidden.