Custom messagebox does not appear in the specified size range
-
Hi Guys!
I created a custom messagebox class like this:
It comes from QDialog and contains qlabels and X and ok qpushbutton. My problem is that i would like to appear in a range width. So if the message text (its qlabel too) can contain in a smaller width then appear that width if not then longer width.
So i set minimum width to 200 and maximum width 1200. But the messagebox ALWAYS appear in 200 (so the minimum width) and i dont know how can i achive this.
Any idea?
Thanks for your helping! -
Then from the looks of it, you should evaluate the size of the text using QFontMetrics and resize the widget.
-
Hi,
How are you building your widget ?
How would you determine that the widget should expand its width ?
-
@SGaist said in Custom messagebox does not appear in the specified size range:
How would you determine that the widget should expand its width
The message QLabel has a minwidth and maxwidth (i set it with setminwidth and setmaxwidth). I would like to achive when the text longer then minwidth start increasing the width of the dialog continuously up to maxwidth ( if necessary ) if the text is more longer, wrap the text (its working).
Here is my code:MessageBoxD.cpp
MessageBoxD::MessageBoxD(QWidget *parent) : QDialog(0, Qt::FramelessWindowHint) { QFile file("./messagebox.qss"); file.open(QFile::ReadOnly); QString styleSheet = file.readAll(); this->setStyleSheet(styleSheet); this->setMinimumWidth(400); this->setMaximumWidth(1500); this->setWindowFlags(Qt::FramelessWindowHint | Qt::SubWindow); this->h = new QHBoxLayout(this); h->setSpacing(0); h->setContentsMargins(0,0,0,0); this-> gl = new QGridLayout; this->m_parent = parent; this->buttonBox = new QDialogButtonBox(); buttonBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); buttonBox->setContentsMargins(7,7,7,7); h->addWidget(buttonBox); this->cs = new QLabel(this); cs->setWordWrap(true); cs->setObjectName("body"); cs->setAlignment(Qt::AlignCenter); m_parent->installEventFilter(this); this->installEventFilter(this); QLabel *ic = new QLabel; QLabel *bot = new QLabel; bot->setLayout(h); header = new MessageHeader("INFORMÁCIÓ",Type::Information,this); header->setWordWrap(true); ic->setFixedWidth(55); ic->setFixedHeight(55); bot->setFixedHeight(55); header->setFixedHeight(55); header->installEventFilter(this); this->bClose = new QPushButton(this); bClose->setFocusPolicy(Qt::NoFocus); bClose->setFixedSize(55,55); connect(bClose, &QPushButton::clicked,this, [=] { exitClick();}); gl->addWidget(ic,0,0,1,1); gl->addWidget(header,0,1,1,8); gl->addWidget(bClose,0,9,1,1); gl->addWidget(bot,4,1,1,9); gl->addWidget(cs,1,1,3,9); gl->setSpacing(0); gl->setContentsMargins(0,0,0,0); setAttribute(Qt::WA_TranslucentBackground); } void MessageBoxD::Set_HeaderText(const QString &text) { header->Set_Text(text); } void MessageBoxD::Set_Text(const QString &text) { cs->setText(text); } void MessageBoxD::showEvent(QShowEvent *) { QPushButton *button = new QPushButton("Ok"); button->setObjectName("choicebutton"); button->setProperty("ActionRole",1); button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); buttonBox->addButton(button,QDialogButtonBox::ActionRole); connect(button, &QPushButton::clicked,this, [=] { dialogButtonClicked(button);}); button->setCursor(Qt::PointingHandCursor); setLayout(gl); const QPoint global = m_parent->mapToGlobal(rect().topLeft()); this->move(global.x() + ((m_parent->width()/2)-(this->width()/2)), global.y() + ((m_parent->height()/2)-(this->height()/2))); } bool MessageBoxD::eventFilter(QObject *obj, QEvent *e) { if((e->type() == QEvent::MouseButtonPress && obj==header)) { position = QPoint(static_cast<QMouseEvent*>(e)->globalPosition().x()-geometry().x(), static_cast<QMouseEvent*>(e)->globalPosition().y()-geometry().y()); } if((e->type() == QEvent::MouseMove && obj==header)) { if(static_cast<QMouseEvent*>(e)->buttons() & Qt::LeftButton ) { QPoint toMove = static_cast<QMouseEvent*>(e)->globalPosition().toPoint() - position; move(toMove); } } } void MessageBoxD::dialogButtonClicked(QAbstractButton *button) { int actionRole = button->property("ActionRole").toInt(); done(actionRole); } void MessageBoxD::exitClick() { done(-1); }
Messageheader.cpp
MessageHeader::MessageHeader(const QString &Text,Type type,QWidget *parent) { if(type==Type::Information) this->setObjectName("lineinfo"); this->setContentsMargins(0,0,0,0); this->setText(Text.rightJustified(Text.length()+2, ' ')); this->m_parent = parent; } void MessageHeader::Set_Text(const QString &text) { this->setText(text.rightJustified(text.length()+2, ' ')); }
Thanks for your help! :)
-
Then from the looks of it, you should evaluate the size of the text using QFontMetrics and resize the widget.