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. Custom messagebox does not appear in the specified size range
Qt 6.11 is out! See what's new in the release blog

Custom messagebox does not appear in the specified size range

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 921 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.
  • KaguroK Offline
    KaguroK Offline
    Kaguro
    wrote on last edited by
    #1

    Hi Guys!
    I created a custom messagebox class like this:
    2022-04-07_10h16_23.png
    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Then from the looks of it, you should evaluate the size of the text using QFontMetrics and resize the widget.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      KaguroK 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        How are you building your widget ?

        How would you determine that the widget should expand its width ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • KaguroK Offline
          KaguroK Offline
          Kaguro
          wrote on last edited by
          #3

          @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! :)

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Then from the looks of it, you should evaluate the size of the text using QFontMetrics and resize the widget.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            KaguroK 1 Reply Last reply
            1
            • SGaistS SGaist

              Then from the looks of it, you should evaluate the size of the text using QFontMetrics and resize the widget.

              KaguroK Offline
              KaguroK Offline
              Kaguro
              wrote on last edited by
              #5

              @SGaist Thank you very much for your help! :) It's works with the QFontMetrics and after it resize the widget! Thanks again! :)

              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