c++ - Qt和C++:将QLineEdit添加到QTabWidget

标签 c++ qt qlineedit tabwidget

我想将QLineEdit添加到QTabWidget,但是我总是得到一个

Segmentation fault, SIGSEGV


我是通过以下方式做到的:
QHBoxLayout *layout = new QHBoxLayout;
QWidget *Tab = new QWidget(ui->tabWidget);
ui->tabWidget->addTab(Tab, "Tab1");
Tab->setLayout(layout);

QLineEdit *lE = new QLineEdit();
lE->setObjectName("Text");
lE->setText("Hello");
ui->tabWidget->widget(0)->layout()->addWidget(lE);
这种方法适用于添加QPushButton,但是对于QLineEdit则不可行。

最佳答案

您可以将小部件添加到单元格
例:

//
QWidget* pWidget = new QWidget();
QLineEdit* foo = new QLineEdit(this);
foo->setText("Edit");
QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(foo);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0, 0, 0, 0);
pWidget->setLayout(pLayout);

this->ui->myTable->setCellWidget(1, 1, pWidget);

关于c++ - Qt和C++:将QLineEdit添加到QTabWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62828540/

相关文章:

c++ - std::underlying_type :SFINAE 是否可以防止未定义的行为?

python - 类型错误 : range() integer end argument expected, 已获取 int

qt - 如何将 QLineEdit 添加到菜单栏

python - QLineEdit 对象的 PyQt 集合

python - 如何根据单击的按钮选择行编辑

c++ - C 与 C++ 中的 const 正确性

C++ 类内线程并发

c++ - 如何正确扩展接口(interface)?

c++ - <命令行> :0:3: warning: ISO C++11 requires whitespace after the macro name

c++ - 为什么 QFileDialog::selectFile 不起作用?