В QT, как я выравниваю элементы формы в полях другой группы?

IDisposable часто используется, чтобы использовать using оператор и использовать в своих интересах простой способ сделать детерминированную очистку управляемых объектов.

public class LoggingContext : IDisposable {
    public Finicky(string name) {
        Log.Write("Entering Log Context {0}", name);
        Log.Indent();
    }
    public void Dispose() {
        Log.Outdent();
    }

    public static void Main() {
        Log.Write("Some initial stuff.");
        try {
            using(new LoggingContext()) {
                Log.Write("Some stuff inside the context.");
                throw new Exception();
            }
        } catch {
            Log.Write("Man, that was a heavy exception caught from inside a child logging context!");
        } finally {
            Log.Write("Some final stuff.");
        }
    }
}
12
задан swongu 25 August 2009 в 22:09
поделиться

4 ответа

You can set the minimumWidth property on all of the labels inside the groupboxes to something that is just wide enough to display the widest label. That will get all of the labels inside the different group boxes aligned.

Now, to get the labels outside of the groupboxes aligned with those inside: First, I assume that each label/lineedit pair is in its own horizontal layout, or that you have multiple rows inside of a grid. In either case, what you can do is set the minimumWidth of the labels to the same value as the labels in the groupboxes. Finally, adjust the layoutLeftMargin, layoutRightMargin, and layoutSpacing properties on the horizontal (or grid) layout until the right and left edges of the label/lineedit pair align with those inside the groupboxes.

If you're not already using the Form Editor in Qt Creator, or Qt Designer, to build your UI, I found it to make this task fairly easy.

I have to admit, this feels a little kludgey, but in the simple test case I built, it seemed to work okay. On the other hand, this seems likely to break if the user changes the font size. Maybe there's a better way?

Hope this helps.

5
ответ дан 2 December 2019 в 23:07
поделиться

Используйте setGeometry (), чтобы установить одинаковую ширину для всех столбцов

0
ответ дан 2 December 2019 в 23:07
поделиться

Я не думаю, что он будет работать с наборами вложенных горизонтальных и вертикальных макетов. Вы рассматривали QGridLayout ?

1
ответ дан 2 December 2019 в 23:07
поделиться

Kenrogers предоставили решение, и вот некоторый неполный код, который я использовал, чтобы заставить его работать:

int width = 0 ;
QDialog* dialog ;
QList<QGridLayout*> layouts = dialog->findChildren<QGridLayout*>() ;
QList<QLabel*> labels ;
foreach ( QGridLayout* layout, layouts )
{
   // Loop through each layout and get the label on column 0.
   QLabel* foundLabel ;
   labels << foundLabel ;

   // Get the width.
   width = qMax( foundLabel->width(), width ) ;
}

foreach ( QLabel* label, labels )
{
   label->setMinimumWidth( width ) ;
}
1
ответ дан 2 December 2019 в 23:07
поделиться
Другие вопросы по тегам:

Похожие вопросы: