Qt : How to alternate alignment of a QTextEdit in a QVBoxLayout?

  alignment, c++, qt, qvboxlayout

I would like to add QTextEdit widgets in a layout and alternate the alignment

QVBoxLayout *lt = new QVBoxLayout;

for(int i=0;i<5;i++)
{
    text1 = new QTextEdit;
    text1->setText("Hello world !" + QString::number(i));
    text1->setMaximumSize(100,30);
    lt->addWidget(text1);
    lt->setAlignment(Qt::AlignLeft);

    if(i%2)
    {
        lt->setAlignment(Qt::AlignRight);
    }
    else
    {
        lt->setAlignment(Qt::AlignLeft);
    }
}

setLayout(lt);

However, the QTextEdits, are all align on the left.

Is there a solution to my problem ?

Source: Windows Questions C++

LEAVE A COMMENT