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. [SOLVED] Problem with QMessageBox
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Problem with QMessageBox

Scheduled Pinned Locked Moved General and Desktop
12 Posts 2 Posters 3.7k 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.
  • V Offline
    V Offline
    valeSimu
    wrote on last edited by
    #1

    I would like to create a QMessageBox::critical which would automatically adapt its size depending on the size of the content (the message text). I tried but I have some problems...

    This is the message I would like to display on the QMessageBox

    @ std::wstring tmpStrEx;
    QDomNode tmpNodeParent = noderemoved;

    while(!tmpNodeParent.isNull() && tmpNodeParent.nodeName().toStdWString() != L"root"){
    std::wstring tmpStrTmp;
    
    tmpStrTmp = L"<"
               + tmpNodeParent.nodeName().toStdWString()
               + L" nm=\""
               + tmpNodeParent.toElement().attribute(QS(_T("nm")), QS(_T("undef")) ).toStdWString()
               + L"> ";
    }
    
    QString mex = "Error while reading vector.<br/><br/>Please check in:<br/><br/> " + tmpStrTmp ;@
    

    tmpStrTmp can be something like <vct nm="name"> <vcx nm="xyz">

    I tried in these ways to build the QMessageBox:

    First a simple:

    @ QMessageBox::critical(this, "Error", mex);@

    This shows half message without the content in tmpStrTmp like this:

    Error while reading vector.

    Please check in:

    I thought tmpStrTmp was the problem but if I put only tmpStrTmp in the QMessageBox, it shows its content. So, I thought it was a problem of space and I tried these two ways:

    @
    QMessageBox msgBox;
    QSpacerItem* spacer = new QSpacerItem(500, 500, QSizePolicy::Minimum, QSizePolicy::Expanding);
    msgBox.setTextFormat(Qt::RichText);
    msgBox.setText( mex );
    QGridLayout* layout = (QGridLayout*)msgBox.layout();
    layout->addItem(spacer, layout->rowCount(), 0, 1, layout->columnCount());
    msgBox.exec();@

    @ QMessageBox *box = new QMessageBox::QMessageBox(QMessageBox::critical, "Error", mex);

     QGridLayout *layout = qobject_cast<QGridLayout *>(box->layout(&#41;);
     if (layout) {
        QTextEdit *edit = new QTextEdit(mex);
        edit->setReadOnly(true);
        layout->addWidget(edit, 0, 1);
     }
     box->exec(&#41;;
     delete box;@
    

    but without success...any help? thanks

    1 Reply Last reply
    0
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #2

      Hi, what happens if you analyse your QString by

      @
      qDebug() << tmpStrTmp;
      @

      or show the QMessagBox with tmpStrTmp only?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        valeSimu
        wrote on last edited by
        #3

        yes, if I show only tmpStrTmp in the QMessageBox, it shows its content.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clochydd
          wrote on last edited by
          #4

          I overlooked that - so your problem is the size of your QMessageBox?
          Can you describe how it looks or post a picture?

          1 Reply Last reply
          0
          • V Offline
            V Offline
            valeSimu
            wrote on last edited by
            #5

            By using this code

            @ QString mex = "Error while reading vector.<br/><br/>Please check in:<br/><br/> " + tmpStrTmp ;
            QMessageBox::critical(this, "Error", mex);@

            the QMessageBox looks like this:

            https://drive.google.com/a/hydros.ch/file/d/0B7vJt21ZesT0bkJMY2Y2dFFKNTA/edit?usp=sharing

            and with this code

            @ QString mex = tmpStrTmp ;
            QMessageBox::critical(this, "Error", mex);@

            the QMessageBox looks like this:

            https://drive.google.com/a/hydros.ch/file/d/0B7vJt21ZesT0SGIwN3FxM0Q1c2s/edit?usp=sharing

            Can you see the photos?

            1 Reply Last reply
            0
            • V Offline
              V Offline
              valeSimu
              wrote on last edited by
              #6

              So I don't think that the problem is the size cause, the dialog adapt itself according to the content...

              1 Reply Last reply
              0
              • C Offline
                C Offline
                clochydd
                wrote on last edited by
                #7

                I can see the pictures and I think I understand your problem now and will try to simulate it here.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  clochydd
                  wrote on last edited by
                  #8

                  Hi,
                  I made some tests with your messages and I think your problem has something to do with the special characters in your tmpStrTmp.

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    valeSimu
                    wrote on last edited by
                    #9

                    I thought it too, maybe because there are < and >? but why I can display tmpStrTmp without problem but with the rest of the message not? strange

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      clochydd
                      wrote on last edited by
                      #10

                      You are right: it really sounds strange!
                      I would do this: Change all special chars by a placeholder then rebuild the string by replacing the placeholders step by step with the specials.

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        valeSimu
                        wrote on last edited by
                        #11

                        you know? I solved the problem....< and > caused the problem, I replaced them with & lt; and & gt; and worked! I think because "mex" contains html tag (<br/>) and so it interpreted all like html....I don't know how to explain it with the right words, but I hope you understood. Thank you Clochydd, I really appreciate your help! Have a nice day? evening? bye

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          clochydd
                          wrote on last edited by
                          #12

                          You are welcome, valeSimu!
                          Please sign your thread as solved, by adding a prepending [SOLVED] to your first post.
                          Have a nice day, too!

                          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