Pregunta

I am using QGridLayout to add one QPushButton and one QTextEdit. but whenever i am hiding and removing QTextEdit widget then QPushButton is coming on the position of QTextEdit.

Initially QPushButton is in position lets say (0,0) and QTextEdit is in position (100,0). Then on removing QTextEdit QPushButton is coming on QTextEdit position ie, at position (100,0).

layout->addWidget(button1,0,0,1,1, Qt::AlignCenter);
layout->addWidget(text1,0,1,1,1); 

On pressing delete key i am removing QTextWidget:

layout->removeWidget(text1);

So i think i have to set the position of QGridLayout so that everytime after removing widget it should be in its proper position.

enter image description here enter image description here

¿Fue útil?

Solución

Based upon your screenshot it looks like the column is completely removed when you remove the QTextWidget. You can try experimenting with setting the visibility of the QTextWidget to false rather than removing it.

text1->setVisible( false );

Although based upon the QGridLayout doc this may have the same result.

Or when you remove the QTextEdit replace it with a QSpacerItem so the column isn't completely remove.

If that still doesn't work I would next try changing your align of the QPushButton to Qt::AlignLeft.

layout->addWidget(button1,0,0,1,1, Qt::AlignLeft);

If you're not happy with that I would try using a QHBoxLayout rather than the QGridLayout to layout the items horizontally. Or you could always use the QHBoxLayout within the first row/column of the QGridLayout, it's ultimately up to your future use cases.

For example:

QHBoxLayout* hLayout = new QHBoxLayout();
hLayout->addWidget( button1, 0, Qt::AlignLeft );
hLayout->addWidget( text1, 0, Qt::AlignLeft );
qGridLayout->addLayout ( hLayout, 0, 0, Qt::AlignLeft );

Again with the QHBoxLayout and QGridLayout (might no longer need to use the grid) you may have to play with the alignments to get the results you want. Something like this might work

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top