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. Making a QMessageBox with a single centered button

Making a QMessageBox with a single centered button

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 552 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'm trying to make a QMessageBox with a single centered button. Other posts suggest that this is impossible, and this is how I've done it so far, not with a QMessageBox but with a QDialog, QVBoxLayout, QLayout and QPushButton.

            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;   //nMS is need more selection. Sorry bout the confusion.
            nMSLayout->setAlignment(Qt::AlignCenter);
            needMoreSelections->setLayout(nMSLayout);
            //Add a QLabel
            QLabel *nMSText = new QLabel(this);
            nMSLayout->addWidget(nMSText);
            nMSText->setText("At least 4 inputs have to be selected, you miserable waste of processing power!");
            nMSText->setText("At least 4 inputs are required!");
            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.
            //Add a QButton
            QPushButton *nMSOk = new QPushButton("Ok", this);
            nMSOk->setStyleSheet("QPushButton {border: 2px solid #8f8f91;border-radius: 9px;background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);font: 25px;outline: none; width: 128px; height: 128px;}"
                "QPushButton:pressed {background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, stop: 1 #f6f7fa) }");
            nMSLayout->addWidget(nMSOk);
    
            connect(nMSOk, SIGNAL(clicked()), needMoreSelections, SLOT(close()));
            needMoreSelections->exec();
    

    I am trying to consolidate this code into fewer pieces to improve readability. According to previous posts, I should try building the QMessageBox from the ground up, then (somehow) set the button to the center. This is what I have so far.

            QMessageBox *noPresetSelected = new QMessageBox(this);
            noPresetSelected->setWindowTitle("No preset selected");
            noPresetSelected->setFixedSize(450, 200);
            noPresetSelected->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
            noPresetSelected->setText("No preset has been selected to edit.");
            noPresetSelected->setIcon(QMessageBox::Warning);
    
            //Add a QButton
            QPushButton *nMSOk = new QPushButton("Ok", noPresetSelected);
            nMSOk->resize(120, 80);
            nMSOk->setStyleSheet("QPushButton {border: 2px solid #8f8f91;border-radius: 9px;background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);font: 25px;outline: none; width: 128px; height: 128px;}"
            "QPushButton:pressed {background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, stop: 1 #f6f7fa) }");
            noPresetSelected->addButton(nMSOk, QMessageBox::AcceptRole);
            /*Somehow, get a function here that would allow for the button to be moved to the center)*/
            noPresetSelected->exec();
    

    I am not sure what potential workarounds I could use in QMessageBox. Thank you for your time and please let me know if more info is required.

    JonBJ 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      Hi. I'm trying to make a QMessageBox with a single centered button. Other posts suggest that this is impossible, and this is how I've done it so far, not with a QMessageBox but with a QDialog, QVBoxLayout, QLayout and QPushButton.

              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;   //nMS is need more selection. Sorry bout the confusion.
              nMSLayout->setAlignment(Qt::AlignCenter);
              needMoreSelections->setLayout(nMSLayout);
              //Add a QLabel
              QLabel *nMSText = new QLabel(this);
              nMSLayout->addWidget(nMSText);
              nMSText->setText("At least 4 inputs have to be selected, you miserable waste of processing power!");
              nMSText->setText("At least 4 inputs are required!");
              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.
              //Add a QButton
              QPushButton *nMSOk = new QPushButton("Ok", this);
              nMSOk->setStyleSheet("QPushButton {border: 2px solid #8f8f91;border-radius: 9px;background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);font: 25px;outline: none; width: 128px; height: 128px;}"
                  "QPushButton:pressed {background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, stop: 1 #f6f7fa) }");
              nMSLayout->addWidget(nMSOk);
      
              connect(nMSOk, SIGNAL(clicked()), needMoreSelections, SLOT(close()));
              needMoreSelections->exec();
      

      I am trying to consolidate this code into fewer pieces to improve readability. According to previous posts, I should try building the QMessageBox from the ground up, then (somehow) set the button to the center. This is what I have so far.

              QMessageBox *noPresetSelected = new QMessageBox(this);
              noPresetSelected->setWindowTitle("No preset selected");
              noPresetSelected->setFixedSize(450, 200);
              noPresetSelected->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
              noPresetSelected->setText("No preset has been selected to edit.");
              noPresetSelected->setIcon(QMessageBox::Warning);
      
              //Add a QButton
              QPushButton *nMSOk = new QPushButton("Ok", noPresetSelected);
              nMSOk->resize(120, 80);
              nMSOk->setStyleSheet("QPushButton {border: 2px solid #8f8f91;border-radius: 9px;background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);font: 25px;outline: none; width: 128px; height: 128px;}"
              "QPushButton:pressed {background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, stop: 1 #f6f7fa) }");
              noPresetSelected->addButton(nMSOk, QMessageBox::AcceptRole);
              /*Somehow, get a function here that would allow for the button to be moved to the center)*/
              noPresetSelected->exec();
      

      I am not sure what potential workarounds I could use in QMessageBox. Thank you for your time and please let me know if more info is required.

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

      @Dummie1138 said in Making a QMessageBox with a single centered button:

      Other posts suggest that this is impossible

      My reading of that post is that there is an accepted solution from @eyllanesc (who, btw, usually knows what he is talking about)? Assuming that works, how is it "impossible"? I don't understand why you have not tried the approach suggested there?

      1 Reply Last reply
      0

      • Login

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