PDA

View Full Version : QTableWidgetItem and QToolButton : who's my neighbour?



gruzlor
24th January 2011, 09:10
Hi,

I'm starting with Qt and CPP. Here is my problem : I have a QTableWidget with data and I want to add a button in the first cell of every row to delete the row.
I subclassed the QToolButton class and wanted to add as a attribute a pointer to the neighbour cell.
The idea was that the user could delete randomly any row and every deleteRowButton would know its row with neighbourCell->row() (the widget inserted a cell using setItem() can't know the current row, am I right?).

Here is my code :
the creation of the row with the creation of the deleteRowButton, located in my mainwindow (airflowcalculator.cpp) :

tableItem->insertRow(newRow);
tableWidgetEraseItem = new deleteRowButton(*tableWidgetName);
tableItem->setCellWidget(newRow, 0, tableWidgetEraseItem);
tableWidgetName = new QTableWidgetItem("my name");
tableItem->setItem(newRow, 1, tableWidgetName);

the slot to remove the row in my mainwindow (airflowcalculator.cpp)

void airflowCalculator::removeAVComponent(int row)
{
tableItem->removeRow(row);
tableItem->selectRow(tableItem->currentRow());
}

The deleteRowButton class (deleterowbutton.cpp)


#include "deleterowbutton.h"
#include <QIcon>

deleteRowButton::deleteRowButton(const QTableWidgetItem & neighbourCellTemp)
: QToolButton()
{
neighbourCell = neighbourCellTemp;
setIcon(QIcon(":/images/delete.png"));
connect(this, SIGNAL(clicked()),this, SLOT(deleteRowSlot()));
}

void deleteRowButton::deleteRowSlot()
{
emit deleteRow(neighbourCell->row());
}


Of course, I suspect the argument passing to the deleteRowButton but I'm a little bit lost after having tried with pointers and references :confused:

Thanks in advance!

tbscope
24th January 2011, 09:27
(the widget inserted a cell using setItem() can't know the current row, am I right?).
I'm not sure what you mean, but newRow is the row? So it does know the row?

Note that any item can report its current row:
http://doc.qt.nokia.com/4.7/qtablewidgetitem.html#row



...
tableWidgetEraseItem = new deleteRowButton(*tableWidgetName);
...
tableWidgetName = new QTableWidgetItem("my name");
...
This logic seems wrong. Shouldn't tableWidgetName be created before tableWidgetEraseItem?

maxpaayne
24th January 2011, 23:03
QToolButton class provides quick access to commands or options, usually used in a QToolBar. QToolButton supports self-consciousness. In automatic mode to rise, the button draws a 3D frame when you move your mouse over it. The function is automatically activated when the button is used in a QToolBar. This is also udes in deletion of any.