Centering text on a QLabel with a QDialog parent
Solved
General and Desktop
-
Hi. I made a QLabel w/ a QDialog parent. This is the code.
QDialog *needMoreSelections = new QDialog(this); needMoreSelections->setWindowTitle("Not enough inputs!"); //Set size of QDialog. needMoreSelections->setFixedSize(450, 200); needMoreSelections->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); //Add a QLabel QLabel *nMSText = new QLabel(needMoreSelections); nMSText->setText("At least 2 inputs have to be selected, you miserable waste of processing power!"); nMSText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); nMSText->setWordWrap(true); nMSText->setAlignment(Qt::AlignCenter); needMoreSelections->setStyleSheet("font: 20px; color: navy; border: none; outline: none;"); needMoreSelections->exec();
And this is the result.
I have set the Alignment of the QLabel to center. Why is the text still slanted to the left? I'm assuming it's because the QLabel is placed to the left of the QDialog, but I haven't found a function that would allow me to center the QLabel in the QDialog.
-
@Dummie1138
Yes, the text in theQLabel
is centred, but theQLabel
itself is not. Always add a layout to widgets (your dialog) and add sub-widgets (your label) onto that. -
I have modified the code according to JonB's suggestions.
QDialog *needMoreSelections = new QDialog(this); needMoreSelections->setWindowTitle("Not enough inputs!"); //Set size of QDialog. needMoreSelections->setFixedSize(450, 200); needMoreSelections->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); QVBoxLayout *nMSLayout = new QVBoxLayout; nMSLayout->setAlignment(Qt::AlignCenter); needMoreSelections->setLayout(nMSLayout); //Add a QLabel QLabel *nMSText = new QLabel(this); nMSLayout->addWidget(nMSText); nMSText->setText("At least 2 inputs have to be selected, you miserable waste of processing power!"); nMSText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); nMSText->setWordWrap(true); nMSText->setAlignment(Qt::AlignCenter); needMoreSelections->setStyleSheet("font: 20px; color: navy; border: none; outline: none;"); //Need to set to center and add image. needMoreSelections->exec();
The results are satisfactory.