How to Remove dynamically Added Button in QGridLayout
-
This is my code ,
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtSql/QSqlQuery> #include <QDebug> static QString array[100]={""}; int val=0; int val2=0; int number=0; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE"); mydb.setDatabaseName("E:/QT Offline/Qt5.12.12/GLIDelete/Bar_Data.db"); if(!mydb.open()) { qDebug() << "Can't Connect to DB !"; } else { qDebug() << "Connected Successfully to DB !"; } } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { ui->stackedWidget->setCurrentIndex(1); ui->gridLayout->update(); ui->tabWidget->setCurrentIndex(0); ui->tabWidget->update(); QSqlQuery query; query.prepare("SELECT name FROM test"); query.exec(); int count=0; while(query.next()) { QString s=query.value(0).toString(); array[count]=s; count++; //qDebug()<<s; } int count1=0; const QSize btnSize = QSize(150, 50); for (int i=0;i<15 ;i++ ) { for (int j=0;j<5 ;j++ ) { if(count1==count) break; else { QPushButton *button = new QPushButton(array[count1]); button->setFixedSize(btnSize); button->setObjectName(array[count1]); connect(button, &QPushButton::clicked, this,&MainWindow::cald); ui->gridLayout->addWidget(button,i,j); count1++; } } } } void MainWindow::cald() { QObject *senderObj = sender(); // This will give Sender object QString senderObjName = senderObj->objectName(); ui->stackedWidget->setCurrentIndex(1); } void MainWindow::on_pushButton_2_clicked() { ui->stackedWidget->setCurrentIndex(0); } void MainWindow::on_pushButton_3_clicked() { ui->stackedWidget->setCurrentIndex(2); ui->gridLayout->update(); ui->tabWidget->setCurrentIndex(0); ui->tabWidget->update(); QSqlQuery query; query.prepare("SELECT name FROM test"); query.exec(); int count=0; while(query.next()) { QString s=query.value(0).toString(); array[count]=s; count++; } int count1=0; const QSize btnSize = QSize(150, 50); for (int i=0;i<15 ;i++ ) { for (int j=0;j<5 ;j++ ) { if(count1==count) break; else { QPushButton *button = new QPushButton(array[count1]); button->setFixedSize(btnSize); button->setObjectName(array[count1]); connect(button, &QPushButton::clicked, this,&MainWindow::cald2); ui->gridLayout_2->addWidget(button,i,j); val=i; val2=j; count1++; } } } } void MainWindow::cald2() { QObject *senderObj = sender(); // This will give Sender object QString senderObjName = senderObj->objectName(); QLayoutItem* item = ui->gridLayout_2->itemAtPosition(val,val2); QWidget * widget = item->widget(); int value = ui->gridLayout_2->indexOf(widget); QLayoutItem * it = ui->gridLayout_2->itemAt(value); QWidget * widgets = it->widget(); while( ui->gridLayout_2->count() ) { qDebug()<<ui->gridLayout_2->count(); if( widgets ) { ui->gridLayout_2->removeWidget(widgets); delete widgets; } } QSqlQuery query; query.prepare("Delete from test where name='"+senderObjName+"'"); if(query.exec()) { qDebug()<<"Deleted Test : "<<senderObjName; } ui->stackedWidget->setCurrentIndex(2); } void MainWindow::on_pushButton_4_clicked() { ui->stackedWidget->setCurrentIndex(0); }
UI Page : -
Ui->Stackedwidget->currentIndex(0);
.Ui->Stackedwidget->currentIndex(1);
Ui->Stackedwidget->currentIndex(2);Iam using two gridlayout in two different pages , Retrieves buttons name from database ,
query.prepare("SELECT name FROM test");
If i click on the delete button and delete it , it doesn't delete ,
how to delete it correctly,
-
@Ramkumar-Mohan said in How to Remove dynamically Added Button in QGridLayout:
QLayoutItem* item = ui->gridLayout_2->itemAtPosition(val,val2);
Correct way is :
QLayoutItem* item = ui->gridLayout_2->itemAtPosition(val,val2);
ui->gridLayout_2->removeItem(item);
QWidget *del_wid = item->widget();
if(del_wid)
{
delete del_wid;
}Note : always first remove item then delete widget form any kind of layout.
-
@Ramkumar-Mohan Just check documentation: https://doc.qt.io/qt-5/qlayout.html#takeAt
-