Get index Row in QTableWidget [SOLVED]
-
Hi Qt Warrior...
i have got some of problem to get index row on the QTableWidgetthis is the link of snapshot i have been created:
http://pbrd.co/1foTebDsnappet code:
@
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setUpUi(this);QSqlQuery query ("select *from employees");;
ui->tableWidget->setRowCount(query.size());
ui->tableWidget->setHorizontalHeaderLabels(QString(" IDdbEmp ; Last Name ; First Name ; Email ; Country").split(";"));signalMapper = new QSignalMapper(this);
int size = ui->tableWidget->rowCount();
for ( y=0; y < size ; ++y)
{
query.next();
detailButton = new QPushButton(this);
detailButton->setText(QString::fromUtf8("Delete"));
ui->tableWidget->setItem(y,0,new QTableWidgetItem(query.value(0).toString()));
ui->tableWidget->setItem(y,1,new QTableWidgetItem(query.value(1).toString()));
ui->tableWidget->setItem(y,2,new QTableWidgetItem(query.value(2).toString()));
ui->tableWidget->setItem(y,3,new QTableWidgetItem(query.value(3).toString()));
ui->tableWidget->setItem(y,4,new QTableWidgetItem(query.value(4).toString()));
ui->tableWidget->setCellWidget(y,5,detailButton);
connect(detailButton,SIGNAL(clicked()),signalMapper,SLOT(map()));
signalMapper->setMapping(detailButton,y);
}query.clear();
connect(signalMapper,SIGNAL(mapped(int)),this,SLOT(detail(int)));
connect(ui->button1,SIGNAL(clicked()),this,SLOT(plus()));}
MainWindow::~MainWindow()
{
delete ui;}
void MainWindow::plus()
{/*! if(!(emp)) emp= new EmployeeOrg(this); emp->show();*/
}
void MainWindow::detail(int row)
{//ui->statusBar->showMessage(QString::fromUtf8("PushButton changet at row %1, value at: %2 ").arg(row+1));
qDebug()<< ui->tableWidget->item(row, 0)->text();ui->tableWidget->removeRow(row ); QSqlQuery query; query.exec(QString("delete from employees where empID=%1").arg(row));
}
@when i click the button of Delete , the query of delete not working well
I hope the Qt warrior can give the best solution for my problem
Thanks :) -
How is it that row numer corresppnds to emplD in database. Should it not be:
str = "delete from employees where empID=" + item(row, 0)->text();
query.exec(str);Also you should check the status of your exec. Something like:
bstatus = query.exec();
if (bstatus == false) {
sqlError = query.lastError();
errorMessage = sqlError.databaseText();
qWarning() << errorMessage;
} -
thanks dude
its workwhen i deleted of each row in qtablewidget, also i need to know the empID on my database
:)