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

690 views Asked by At

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

QVBoxLayout *lt = new QVBoxLayout;

/* I would like to align them like that :
   -------------
   | One       |
   |       Two |
   | Three     |
   |      Four |
   | Five      |
   ------------
*/

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 ?

1

There are 1 answers

0
Timothée On BEST ANSWER

Just checking in the Qt doc i found that I just needed to add some arguments when adding my widget in the QVBoxLayout Here is the code :

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);
    
    if(i%2)
    {
        lt->addWidget(text1, 0, Qt::AlignRight);
    }
    else
    {
        lt->addWidget(text1, 0, Qt::AlignLeft);
    }
}