PDA

View Full Version : Connect a QPushButton click and a QTableWidgetItem ready for editing



aabilio
11th July 2012, 04:50
First of all, I apologize for my English level, long ago I do not practice.

I was trying to do this by my own for a while but I can't. So I need help.
This is the situation:
I have a QPushButton and a QTableWidget. I want that when somebody clicks on the button, a specific cell (the first empty row on the 2 Column position) of the table, set to be editable with some caracteres by default already there ("%" <- exactly).
Now I'm using this SLOT:


void MainWindow::SLOT_searchNewName(void)
{
this->ClientTable->setFocus();
for (int i = 0; i<TOTAL_FACT; i++)
{
if (!this->ClientTable->item(i,APAGAR) || this->Tabla_Cliente->item(i,APAGAR)->text().isEmpty())
{
// First: delete contents Row of Row who column APAGAR does not exist or is empty
this->ClientTable->removeCellWidget(i,DESC);
for (int b=1; b<6; b++) // Colums
if (this->ClientTable->item(i,b))
delete this->ClientTable->item(i,b);

// Second: Prepare the cell:
QTableWidgetItem *i_name=new QTableWidgetItem("%");
this->ClientTable->setItem(i,NAME,i_name);
this->ClientTable->setCurrentItem(i_name);
this->ClientTable->setItemSelected(i_name,true);
this->ClientTable->setCurrentCell(i,NAME);

break;
}
}
}

But this is not exactly what I need. This put the "%" in the correct cell, and make it "Selected", so I can edit it just typing, but, and thats is the important part, by deleting the previos caracter that were there (in this case, the "%", which is crucial, because is a token to search on a DB:


else if (Column == NAME)
{
this->combo = new QComboBox();
QObject::connect(combo,SIGNAL(activated(int)),this ,SLOT(SLOT_backAfterSearch(int)));

if (this->ClientTable->item(Row,NAME)->text().startsWith("%")) // <-- That's the token
{
L = this->db.getMedicamentoByName(this->ClientTable->item(Row,NAME)->text());
for (l=L.begin(); l != L.end(); ++l) combo->addItem(l->name+" - "+l->desc);
this->ClientTable->setCellWidget(Row,DESC,combo);
this->combo->setMaxVisibleItems(50);
this->combo->showPopup();

}
}

).

I want that when the button is clicked, everything works just like now, but when I start typing in the cell given by the button, the words that were there don't be deleted. I believe that have to be something to do that: make that the cell is directly editable with the cursor after the text that is it's there.