Qt and C++: Add QLineEdit to QTabWidget

645 Views Asked by At

I want to add a QLineEdit to a QTabWidget but I always get a

Segmentation fault, SIGSEGV

I did it the following way:

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);

This way works for adding a QPushButton but somehow it doesn't work for a QLineEdit.

1

There are 1 best solutions below

0
ΦXocę 웃 Пepeúpa ツ On

you can add a widget to a cell

example:

//
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);