Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Centering text on a QLabel with a QDialog parent
Forum Updated to NodeBB v4.3 + New Features

Centering text on a QLabel with a QDialog parent

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 584 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    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.
    364d7969-e8b6-4f68-b4bb-d6a70c0519ef-image.png

    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.

    JonBJ 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      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.
      364d7969-e8b6-4f68-b4bb-d6a70c0519ef-image.png

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Dummie1138
      Yes, the text in the QLabel is centred, but the QLabel itself is not. Always add a layout to widgets (your dialog) and add sub-widgets (your label) onto that.

      1 Reply Last reply
      2
      • Dummie1138D Offline
        Dummie1138D Offline
        Dummie1138
        wrote on last edited by
        #3

        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.
        f3de741f-385a-4712-a967-9e5af782b654-image.png

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved