Custom QWidget inside a QTableWidgetItem
-
Hello everybody,
I'm trying to put a custom Widget called PushButtonElimina inside a cell in QTableWidget but PushButtonElimina is showed inside new window.In the snippet below the code that I used to create custom widget
PushButtonElimina::PushButtonElimina(QWidget *parent, int riga) : QWidget(parent), riga{riga} { pWidget = new QWidget(); azione = new QPushButton(); azione->setParent(parent); pixmap = new QPixmap(":/img/Resources/images/notification-error_114458.png"); QIcon icona(*pixmap); azione->setIconSize((QSize(32, 32))); azione->setIcon(icona); azione->setFixedSize(32, 32); azione->setFlat(true); pLayout = new QHBoxLayout(pWidget); pLayout->addWidget(azione); pLayout->setAlignment(Qt::AlignCenter); pLayout->setContentsMargins(50, 0, 50, 0); pWidget->setLayout(pLayout); connect(azione, SIGNAL(clicked()), this, SLOT(on_pushButtonElimina_clicked())); }
and here where I use it
PushButtonElimina *azione = new PushButtonElimina(ui->tableWidgetIngredienti, riga); connect(azione, SIGNAL(eliminaRiga()), this, SLOT(on_pushButtonElimina_clicked())); ui->tableWidgetIngredienti->setCellWidget( riga, 2, azione );
Any suggestion to solve this problem? I prefer to create a custom widget because I used it around all the project and I want to avoid code replications.
Thanks in advanced
Ric
-
@riczam said in Custom QWidget inside a QTableWidgetItem:
pWidget = new QWidget();
Here you create a new widget and put all other widgets into it for no reason.
-
@Christian-Ehrlicher do you suggest to use only QPushButton and put it inside the cell?
How can I set padding in the cell if I do not set a layout? -
@riczam said in Custom QWidget inside a QTableWidgetItem:
do you suggest to use only QPushButton and put it inside the cell?
No, I just told you that you create a new QWidget inside your custom widget and put all in there instead in your custom widget. And this causes your problem.
-
@Christian-Ehrlicher thanks for your tips! I removed the internal Widget and it works as expected.
Here the correct code
PushButtonElimina::PushButtonElimina(QWidget *parent, int riga) : QWidget(parent), riga{riga} { azione = new QPushButton(); azione->setParent(parent); pixmap = new QPixmap(":/img/Resources/images/notification-error_114458.png"); QIcon icona(*pixmap); azione->setIconSize((QSize(32, 32))); azione->setIcon(icona); azione->setFixedSize(32, 32); azione->setFlat(true); pLayout = new QHBoxLayout(this); pLayout->addWidget(azione); pLayout->setAlignment(Qt::AlignCenter); pLayout->setContentsMargins(50, 0, 50, 0); this->setLayout(pLayout); connect(azione, SIGNAL(clicked()), this, SLOT(on_pushButtonElimina_clicked())); }