c++ - 水平对齐动态创建的小部件 qt c++

标签 c++ qt alignment qlineedit qlabel

下面的代码是我用来基于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 放在垂直布局中。或者您对 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

关于c++ - 水平对齐动态创建的小部件 qt c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46357302/

相关文章:

c++ - 为什么 C++ 在函数定义中不接受 static 关键字?

c++ - 无法在 Mac 中使用 Boost + Qt

c++ - 我如何获得有关该错误的更多信息?

python dict时间之谜

Android:错误:使用未声明的标识符 'fseeko'

c++ - 店里价格翻倍?

qt - 如何禁用调整大小光标

android - ActionBar 中的 Logo 左对齐

python - 是否可以将 print 语句与 Python 中的中心对齐?

c++ - C++ Primer 5 ed.1.23从文件流读取