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. Qt QMessageBox get rid of label left margin
Forum Updated to NodeBB v4.3 + New Features

Qt QMessageBox get rid of label left margin

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 602 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.
  • L Offline
    L Offline
    lamozg6
    wrote on last edited by
    #1

    I want to display some information message, for that I decided to use QMessageBox with no icon, problem is there is a huge gap between left part of widget and text, tried to remove it by setting margins and spacings to 0, but everything was removed except that left part.

    Here is a simple example of what I have.

    QMessageBox messageBox(QMessageBox::NoIcon, "My Title", "This is my text that I want to display.\nBut there is annoying left margin that I can't remove whatever I try.\n", QMessageBox::StandardButton::Ok);
    messageBox.exec();
    

    enter image description here

    Any ideas how to do that, or I should create my own simple widget?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      well you could do like

      void MainWindow::on_pushButton_4_clicked()
      {
          QMessageBox messageBox(QMessageBox::Warning, "My Title",
                                 "This is my text that I want to display.\nBut there is annoying left margin that I can't remove whatever I try.\n",
                                 QMessageBox::StandardButton::Ok);
          auto labelIcon = messageBox.findChild< QLabel *>("qt_msgboxex_icon_label");
          if (labelIcon) {
              labelIcon->setMaximumSize(QSize(0, 0));
          }
          auto layout = messageBox.findChild< QGridLayout *>();
          if (layout ) {
              layout->setSpacing(0);
              layout->setContentsMargins(0, 0, 12, 0);
          }
          messageBox.exec();
      }
      

      and get this.

      alt text

      do note we find internal widgets and fiddle with them. this might break in later Qt version if the rename the labels.
      That said. Not so likely to ever happen.

      1 Reply Last reply
      2
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        @mrjj hack may work, or it might not. It bypasses the established interface for the "canned widget" you are attempting to use. If you want total control and forward compatibility then design your own message box widget.

        I light my way forward with the fires of all the bridges I've burned behind me.

        mrjjM 1 Reply Last reply
        3
        • Kent-DorfmanK Kent-Dorfman

          @mrjj hack may work, or it might not. It bypasses the established interface for the "canned widget" you are attempting to use. If you want total control and forward compatibility then design your own message box widget.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Kent-Dorfman

          Well, it is indeed a hack.
          I totally agree. If the poster wants the best solution, making a custom dialog is the way to go.
          Also in his case, he doesn't want the icons, etc so really no gain in using the build-in messageBox.
          But it worked for me in win/linux from 5.5 -> 5.15.2 so the risk seems very minor.

          1 Reply Last reply
          1

          • Login

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