Subclassing QMessageBox
-
Hi! I would like some advice and opinions.
To make QMessageBox look the way I want, I have subclassed it.
Using QShowEvent to change its size.
It works, if you add a time margin with QTimer. It feels a bit unreliable. I have checked many times now, and it has always worked. Is it better to use QLayout?class MyMsgBox : public QMessageBox { Q_OBJECT public: explicit MyMsgBox(QWidget *parent = nullptr, const QSize &size = QSize(500, 500)) : QMessageBox(parent), m_size(size) { // qDebug() << "MyMsgBox created with size:" << m_size; } protected: void showEvent(QShowEvent *event) override { QTimer::singleShot(100, this, [this]() { this->setFixedSize(m_size); }); } private: QSize m_size; };
Thanks!
-
Nothing happens. QMessageBox doesn't have the properties to "resize" or "setFixedSize", as far as I understand.
void messageBox(const QString &message, QString &languagefile, const QSize &size, QMessageBox::Icon icon) { MyMsgBox *msgBox = new MyMsgBox(nullptr, size); msgBox->setAttribute(Qt::WA_DeleteOnClose); msgBox->setIcon(icon); msgBox->setWindowTitle(DISPLAY_NAME " " VERSION); msgBox->setText(message); msgBox->setInformativeText(languagefile); QPushButton *okButton = new QPushButton("Ok", msgBox); msgBox->addButton(okButton, QMessageBox::YesRole); // msgBox->setFixedSize(size); msgBox->exec(); }But it works great with zero lag!
QTimer::singleShot(0, this, [this]() { this->setFixedSize(m_size); }); -
QMessageBox is a QWidget and inherits both QWidget::resize() and QWidget::setFixedSize().
-
QMessageBox is a QWidget and inherits both QWidget::resize() and QWidget::setFixedSize().
@ChrisW67 said in Subclassing QMessageBox:
is a QWidget and inherits both QWidget::resize() and QWidget::setFixedSize().
Yes that's true. but I've never managed to change its size. "resize" and "setFixedSize" have never worked.
It's possible to influence indirectly by sizing the buttons.QPushButton *yesButton = new QPushButton(iconYes, tr("Restart Now"), msgBox); yesButton->setFixedSize(QSize(150, 40)); -
@ChrisW67 said in Subclassing QMessageBox:
is a QWidget and inherits both QWidget::resize() and QWidget::setFixedSize().
Yes that's true. but I've never managed to change its size. "resize" and "setFixedSize" have never worked.
It's possible to influence indirectly by sizing the buttons.QPushButton *yesButton = new QPushButton(iconYes, tr("Restart Now"), msgBox); yesButton->setFixedSize(QSize(150, 40));Hi, I think this topic helps.
ApparentlyQMessageBoxhas an internalQGridLayoutthat is configured to always be as compact as possible (makes sense for usual use cases of msg boxes).
So subclassingQMessageBoxdirectly only has little space for configurations.
SinceQMessageBoxis also some kind ofQDialog(through inheritance), subclassingQDialogmight be better for your custom message dialog class.
Then you have full control over size, layouts and the content. -
Nothing happens. QMessageBox doesn't have the properties to "resize" or "setFixedSize", as far as I understand.
void messageBox(const QString &message, QString &languagefile, const QSize &size, QMessageBox::Icon icon) { MyMsgBox *msgBox = new MyMsgBox(nullptr, size); msgBox->setAttribute(Qt::WA_DeleteOnClose); msgBox->setIcon(icon); msgBox->setWindowTitle(DISPLAY_NAME " " VERSION); msgBox->setText(message); msgBox->setInformativeText(languagefile); QPushButton *okButton = new QPushButton("Ok", msgBox); msgBox->addButton(okButton, QMessageBox::YesRole); // msgBox->setFixedSize(size); msgBox->exec(); }But it works great with zero lag!
QTimer::singleShot(0, this, [this]() { this->setFixedSize(m_size); });@posktomten said in Subclassing QMessageBox:
But it works great with zero lag!
Then the implication is that internal
QMessageBoxcode does some size changing of its own, perhaps as it is first shown. So if you want a solution what is wrong with yourQTimer::singleShot()(this is often required in certain Qt cases, you won't be the first one to use this), or maybe same in override ofshowEvent()(after calling base method first) would also work and be a bit neater?Otherwise you might want to roll your own
QDialogas @Pl45m4 writes. -
Hi, I think this topic helps.
ApparentlyQMessageBoxhas an internalQGridLayoutthat is configured to always be as compact as possible (makes sense for usual use cases of msg boxes).
So subclassingQMessageBoxdirectly only has little space for configurations.
SinceQMessageBoxis also some kind ofQDialog(through inheritance), subclassingQDialogmight be better for your custom message dialog class.
Then you have full control over size, layouts and the content.@Pl45m4 said in Subclassing QMessageBox:
Since QMessageBox is also some kind of QDialog (through inheritance), subclassing QDialog might be better for your custom message dialog class.
Then you have full control over size, layouts and the content.Is definitely a better solution. I was a little surprised that it was possible to use QEvent.
-
Thanks for all the feedback!
My solution doesn't feel right. Even though it seems to work.
Making your own messagebox from QDialog feels much better.
The reason for me to make my own messagebox is that I often want more width.And is probably a more stable solution that should work more securely with upcoming Qt versions.
-
Thanks for all the feedback!
My solution doesn't feel right. Even though it seems to work.
Making your own messagebox from QDialog feels much better.
The reason for me to make my own messagebox is that I often want more width.And is probably a more stable solution that should work more securely with upcoming Qt versions.
@posktomten said in Subclassing QMessageBox:
The reason for me to make my own messagebox is that I often want more width.
I feel you. Sometimes we have error messages that include the full path to a file. It doesn't fit inside the message boxes...
-
P posktomten has marked this topic as solved on