Results 1 to 2 of 2

Thread: Overlay text of labels

  1. #1
    Join Date
    Mar 2011
    Posts
    25
    Thanks
    7
    Qt products
    Qt4

    Default Overlay text of labels

    In this example the text of the labels are re-drawn and overlay each other when pressing the button. Is there a possibility to clear the layout mGridLayout before calling Initialize()? Or is the only solution to save the labels globally and simply set the text instead of creating them each time?

    Qt Code:
    1. MyDialog::MyDialog(QWidget *parent, Qt::WFlags flags)
    2. : QDialog(parent, flags)
    3. {
    4. // test input
    5. mTermName.append("term 1");
    6. mTermName.append("term 2");
    7. mValues.append(1.23);
    8. mValues.append(3.4322);
    9.  
    10. QGridLayout *mMainLayout = new QGridLayout(this);
    11. mGridLayout = new QGridLayout;
    12. Initialize(); // initializes mGridLayout with its content
    13. mMainLayout->addLayout(mGridLayout, 0, 0);
    14.  
    15. QPushButton *btn = new QPushButton("press me");
    16. Q_ASSERT(connect(btn, SIGNAL(clicked()), this, SLOT(slotPressed())));
    17. mMainLayout->addWidget(btn, 1, 0);
    18.  
    19. setLayout(mMainLayout);
    20. }
    21.  
    22.  
    23. void MyDialog::slotPressed()
    24. {
    25. mValues[0] = 2.5;
    26. Initialize();
    27. }
    28.  
    29. void MyDialog::Initialize()
    30. {
    31. int nbrTerms = 2;
    32. int row = 1;
    33. for(int i=0; i<nbrTerms; i++)
    34. {
    35. QString termName = QString("%1").arg(mTermName[i]);
    36. double termValue = mValues[i];
    37.  
    38. QLabel *nameLabel = new QLabel(termName);
    39. QString parameterValueText = QString("%1").arg( termValue, 1, 'f', 3 );
    40.  
    41. QLabel *termValueLabel = new QLabel(parameterValueText);
    42. mGridLayout->addWidget(nameLabel, row, 1);
    43. mGridLayout->addWidget(termValueLabel,row, 2);
    44. row++;
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Overlay text of labels

    Quote Originally Posted by iwatsu View Post
    In this example the text of the labels are re-drawn and overlay each other when pressing the button. Is there a possibility to clear the layout mGridLayout before calling Initialize()? Or is the only solution to save the labels globally and simply set the text instead of creating them each time?
    Globally will not be necessary. Keep pointers to the labels in your class and update the text when necessary.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. The following user says thank you to franz for this useful post:

    iwatsu (23rd September 2011)

Similar Threads

  1. How to put text labels on a figure
    By tommy in forum Qt Programming
    Replies: 3
    Last Post: 7th May 2009, 17:33
  2. Histogram axis labels as text
    By DKGear in forum Qwt
    Replies: 1
    Last Post: 30th December 2008, 09:54
  3. QScrollView Overlay
    By EricTheFruitbat in forum Qt Programming
    Replies: 5
    Last Post: 27th December 2006, 10:29
  4. Pixmap Overlay
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 22nd June 2006, 21:19
  5. Overlay
    By ToddAtWSU in forum Qt Programming
    Replies: 21
    Last Post: 24th May 2006, 14:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.