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. Format content of messageBox
Qt 6.11 is out! See what's new in the release blog

Format content of messageBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.3k 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.
  • C Offline
    C Offline
    ChiaoHuang
    wrote on last edited by
    #1

    Hi ,
    I'd like to show some message.
    I will set the message to QString class and then show messagebox

    Unlucky, it is not what I want in the messagebox.

    This is my example code

        char *info[] = {"Apple", "Banana", "Strawberrie", "Grape"};
        char tmp[1024];
        QString s;
    
        for(int i=0; i<30; i=i+10)
        {
            for(int j=0; j<4; j++)
            {
                sprintf(tmp, "[%2d] %15s = %2d\n", i, info[j], j);
                s.append(tmp);
            }
        }
    
        qDebug() << s;
        QMessageBox::information(NULL,  "Fruits",  s, QMessageBox::Yes, QMessageBox::Yes);
    

    The result
    alt text

    I want the content of messagebox same as the debug message.
    How to do?

    Thanks a lot

    M 1 Reply Last reply
    0
    • C ChiaoHuang

      Hi ,
      I'd like to show some message.
      I will set the message to QString class and then show messagebox

      Unlucky, it is not what I want in the messagebox.

      This is my example code

          char *info[] = {"Apple", "Banana", "Strawberrie", "Grape"};
          char tmp[1024];
          QString s;
      
          for(int i=0; i<30; i=i+10)
          {
              for(int j=0; j<4; j++)
              {
                  sprintf(tmp, "[%2d] %15s = %2d\n", i, info[j], j);
                  s.append(tmp);
              }
          }
      
          qDebug() << s;
          QMessageBox::information(NULL,  "Fruits",  s, QMessageBox::Yes, QMessageBox::Yes);
      

      The result
      alt text

      I want the content of messagebox same as the debug message.
      How to do?

      Thanks a lot

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      @ChiaoHuang
      Use a fixed width /mono spaced font.

      1 Reply Last reply
      3
      • C Offline
        C Offline
        ChiaoHuang
        wrote on last edited by
        #3

        @mpergand
        I am sorry for the late reply
        I am trying it, but it doesn't work.

            msgFont.setStyleHint(QFont::Monospace);
            msg.setFont(msgFont);
            msg.setFixedWidth(1200);
            msg.setText(s);
            msg.setStandardButtons(QMessageBox::Yes);
            msg.exec();
        

        alt text

        Thanks

        JonBJ M C 3 Replies Last reply
        0
        • C ChiaoHuang

          @mpergand
          I am sorry for the late reply
          I am trying it, but it doesn't work.

              msgFont.setStyleHint(QFont::Monospace);
              msg.setFont(msgFont);
              msg.setFixedWidth(1200);
              msg.setText(s);
              msg.setStandardButtons(QMessageBox::Yes);
              msg.exec();
          

          alt text

          Thanks

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @ChiaoHuang
          What type/declaration is this msg variable? It does not seem to obey msg.setFixedWidth(1200); either? Originally you used QMessageBox::information() which is static, so what is going on now in your code?

          I only know that https://stackoverflow.com/a/22587461/489865 from 2014 claims to work, you could test that (with a suitable combobox populated, or hard-coded value). If it really does not work you could test out HTML font markup, or even QSS/CSS.

          1 Reply Last reply
          0
          • C ChiaoHuang

            @mpergand
            I am sorry for the late reply
            I am trying it, but it doesn't work.

                msgFont.setStyleHint(QFont::Monospace);
                msg.setFont(msgFont);
                msg.setFixedWidth(1200);
                msg.setText(s);
                msg.setStandardButtons(QMessageBox::Yes);
                msg.exec();
            

            alt text

            Thanks

            M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            @ChiaoHuang
            try:

            msg.setFont(QFont("courier")); // or monaco or any fixed font name available on your system
            

            or get the default fixed font with:

            QFont font=QFontDatabase::systemFont(QFontDatabase::FixedFont);
            
            C 1 Reply Last reply
            1
            • C ChiaoHuang

              @mpergand
              I am sorry for the late reply
              I am trying it, but it doesn't work.

                  msgFont.setStyleHint(QFont::Monospace);
                  msg.setFont(msgFont);
                  msg.setFixedWidth(1200);
                  msg.setText(s);
                  msg.setStandardButtons(QMessageBox::Yes);
                  msg.exec();
              

              alt text

              Thanks

              C Offline
              C Offline
              ChiaoHuang
              wrote on last edited by
              #6

              @ChiaoHuang
              I created a "QMessageBox msg" for using Font.
              Yes, I don't know why the msg.setFixedWidth(1200) is not working.

              Thank you. I think the HTML font markup should work.

              1 Reply Last reply
              0
              • M mpergand

                @ChiaoHuang
                try:

                msg.setFont(QFont("courier")); // or monaco or any fixed font name available on your system
                

                or get the default fixed font with:

                QFont font=QFontDatabase::systemFont(QFontDatabase::FixedFont);
                
                C Offline
                C Offline
                ChiaoHuang
                wrote on last edited by
                #7

                @mpergand said in Format content of messageBox:

                msg.setFont(QFont("courier"));

                Thank you so much
                It is working now after I used msg.setFont(QFont("consolas"));

                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