【问题标题】:horizontally Aligning dynamically created widgets qt c++水平对齐动态创建的小部件qt c++
【发布时间】:2018-03-03 14:17:28
【问题描述】:

我使用以下代码基于 QStringList(named newList) 动态生成 QLabel 和 QLineEdit(vertically) !

for(int i=0;i<newList.size();i++)
{
      QlineEdit *a=new QLineEdit();
      QLabel *b= new QLabel();

      ui->verticalLayout->addWidget(a);
      ui->verticalLayout_2->addWidget(b);

      b->setText(newList[i]);
}

生成了标签和行编辑,并且在标签中描述了字符串列表的项目!但是我面临的问题是每个行编辑对应的标签没有与该行编辑水平对齐!我该如何纠正这个问题?

【问题讨论】:

    标签: c++ qt alignment qlineedit qlabel


    【解决方案1】:

    您可以将 LineEdit 和 Label 放在 QFrame 中,然后将 QFrame 放在垂直布局中。或者你对 LineEdit 和 Label 使用水平布局,然后把它放到垂直布局中。

    编辑:(未经测试,只能快速编写!)

    for(int i=0;i<newList.size();i++)
    {
          QFrame *f = new QFrame();  
          QlineEdit *a=new QLineEdit( f );
          QLabel *b= new QLabel( f );
    
    
          ui->verticalLayout->addWidget(f);
    
          b->setText(newList[i]);
    }
    

    Exampels for different ways in comparison

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-24
      • 2019-12-12
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      相关资源
      最近更新 更多