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 resizes when moving
Forum Updated to NodeBB v4.3 + New Features

Custom MessageBox resizes when moving

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 329 Views 2 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.
  • D Offline
    D Offline
    Dummie1138
    wrote on 6 Jan 2023, 14:42 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • D Dummie1138
      6 Jan 2023, 15:04

      @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.

      C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 6 Jan 2023, 15:17 last edited by
      #4

      @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 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 6 Jan 2023, 15:01 last edited by
        #2

        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.

        D 1 Reply Last reply 6 Jan 2023, 15:04
        0
        • C Chris Kawa
          6 Jan 2023, 15:01

          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.

          D Offline
          D Offline
          Dummie1138
          wrote on 6 Jan 2023, 15:04 last edited by
          #3

          @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.

          C 1 Reply Last reply 6 Jan 2023, 15:17
          0
          • D Dummie1138
            6 Jan 2023, 15:04

            @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.

            C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 6 Jan 2023, 15:17 last edited by
            #4

            @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 Reply Last reply
            0

            1/4

            6 Jan 2023, 14:42

            • Login

            • Login or register to search.
            1 out of 4
            • First post
              1/4
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved