水平对齐动态创建的小部件 qt c++
horizontally Aligning dynamically created widgets qt c++
下面的代码是我用来基于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]);
}
同时生成了标签和行编辑,并且在标签中描述了字符串列表的项目!但是我面临的问题是每个行编辑对应的标签没有与该行编辑水平对齐!我该如何纠正?
您可以将LineEdit 和Label 放在QFrame 中,而不是将QFrame 放在垂直Layout 中。或者你对 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
下面的代码是我用来基于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]);
}
同时生成了标签和行编辑,并且在标签中描述了字符串列表的项目!但是我面临的问题是每个行编辑对应的标签没有与该行编辑水平对齐!我该如何纠正?
您可以将LineEdit 和Label 放在QFrame 中,而不是将QFrame 放在垂直Layout 中。或者你对 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