PDA

View Full Version : Overlay text of labels



iwatsu
23rd September 2011, 01:45
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?



MyDialog::MyDialog(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
// test input
mTermName.append("term 1");
mTermName.append("term 2");
mValues.append(1.23);
mValues.append(3.4322);

QGridLayout *mMainLayout = new QGridLayout(this);
mGridLayout = new QGridLayout;
Initialize(); // initializes mGridLayout with its content
mMainLayout->addLayout(mGridLayout, 0, 0);

QPushButton *btn = new QPushButton("press me");
Q_ASSERT(connect(btn, SIGNAL(clicked()), this, SLOT(slotPressed())));
mMainLayout->addWidget(btn, 1, 0);

setLayout(mMainLayout);
}


void MyDialog::slotPressed()
{
mValues[0] = 2.5;
Initialize();
}

void MyDialog::Initialize()
{
int nbrTerms = 2;
int row = 1;
for(int i=0; i<nbrTerms; i++)
{
QString termName = QString("%1").arg(mTermName[i]);
double termValue = mValues[i];

QLabel *nameLabel = new QLabel(termName);
QString parameterValueText = QString("%1").arg( termValue, 1, 'f', 3 );

QLabel *termValueLabel = new QLabel(parameterValueText);
mGridLayout->addWidget(nameLabel, row, 1);
mGridLayout->addWidget(termValueLabel,row, 2);
row++;
}
}

franz
23rd September 2011, 06:13
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.