Custom MessageBox resizes when moving
-
wrote on 6 Jan 2023, 14:42 last edited by
Hi. I have made a custom Messagebox.
MessageBoxCenter::MessageBoxCenter(QWidget *parent): QMessageBox(parent), ui(new Ui::MessageBoxCenter) { ui->setupUi(this); //Build a QDialog Box. dialogBox = new QDialog(this); //Set size of QDialog. dialogBox->resize(450, 120); dialogBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); QVBoxLayout *boxLayout = new QVBoxLayout(dialogBox); dialogBox->setLayout(boxLayout); //Add a QLabel boxText = new QLabel(this); boxLayout->addWidget(boxText); boxText->setText("Set the text properly, you miserable waste of human processing power!"); boxText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); //qDebug() << "sizeHint boxText " << boxText->sizeHint(); boxText->setWordWrap(true); boxText->setAlignment(Qt::AlignCenter); dialogBox->setStyleSheet("font: 20px; color: navy; border: none; outline: none;"); //Add a QButton boxButton = new QPushButton("Ok", this); boxButton->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;}" "QPushButton:pressed {background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, stop: 1 #f6f7fa) }"); boxButton->setMinimumSize(80, 50); boxLayout->addWidget(boxButton, 0, Qt::AlignCenter); //qDebug() <<"setup ButtonSize " << boxButton->size(); connect(boxButton, SIGNAL(clicked()), dialogBox, SLOT(close())); }
When I attempt to use this Messagebox somewhere, like so,
MessageBoxCenter passwordWrongBox(this); passwordWrongBox.setText("Password incorrect!"); passwordWrongBox.resize(300, 120); passwordWrongBox.exec();
The Messagebox will automatically resize to the minimum size. I would like to know why this undesirable behavior is occurring, as well as how to stop it. I haven't been able to find any documentation that QDialog automatically resizes when it is moved, so this is a bit confusing to me.
Please let me know if more information is required.
-
@Chris-Kawa All I want from this class is something that looks like a QMessageBox, but only has one button, in the middle. I am aware that this code is a bit convoluted, my skills in QT remain basic at this stage.
@Dummie1138 said:
All I want from this class is something that looks like a QMessageBox
Then just use QMessageBox, e.g.
QString title = "Friendly title"; QString text = "Password incorrect!"; QMessageBox box(QMessageBox::NoIcon, title, text, QMessageBox::Ok); box.findChild<QDialogButtonBox*>()->setCenterButtons(true); box.exec();
Btw. message box button placement in a QMessageBox conforms to platform guidelines. It's usually not a good idea to break them.
-
You are doing some very weird and unusual things. First you have a class that derives from QMessageBox, which is a class that already has a layout. Then you add a designer generated ui to it, which would basically break the base class and override its ui. And then you add a QDialog as a child of the messagebox without any layout, which just puts the dialog inside the messagebox without any size constraints on the parent. Of course the dialog size has nothing to do with the message box size. You are mixing 3 different ways to make a ui in a single widget and they inevitably break each other.
I feel you've super massively overcomplicated your class. What exactly are you trying to achieve? It looks like all you would need ia a simple QDialog without any of that QMessageBox or designer stuff.
-
You are doing some very weird and unusual things. First you have a class that derives from QMessageBox, which is a class that already has a layout. Then you add a designer generated ui to it, which would basically break the base class and override its ui. And then you add a QDialog as a child of the messagebox without any layout, which just puts the dialog inside the messagebox without any size constraints on the parent. Of course the dialog size has nothing to do with the message box size. You are mixing 3 different ways to make a ui in a single widget and they inevitably break each other.
I feel you've super massively overcomplicated your class. What exactly are you trying to achieve? It looks like all you would need ia a simple QDialog without any of that QMessageBox or designer stuff.
wrote on 6 Jan 2023, 15:04 last edited by@Chris-Kawa All I want from this class is something that looks like a QMessageBox, but only has one button, in the middle. I am aware that this code is a bit convoluted, my skills in QT remain basic at this stage.
-
@Chris-Kawa All I want from this class is something that looks like a QMessageBox, but only has one button, in the middle. I am aware that this code is a bit convoluted, my skills in QT remain basic at this stage.
@Dummie1138 said:
All I want from this class is something that looks like a QMessageBox
Then just use QMessageBox, e.g.
QString title = "Friendly title"; QString text = "Password incorrect!"; QMessageBox box(QMessageBox::NoIcon, title, text, QMessageBox::Ok); box.findChild<QDialogButtonBox*>()->setCenterButtons(true); box.exec();
Btw. message box button placement in a QMessageBox conforms to platform guidelines. It's usually not a good idea to break them.
1/4