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