4

There is QTableWidget. I need to send cellChanged signal with row column and text. How can I do this?

--

I have already connected signal with slot. I need to send signal.

Ufx
  • 2,595
  • 12
  • 44
  • 83

2 Answers2

6

You have to use connect to catch signal cellChanged(int,int) when a cell is changed:

connect(yourTableWidget, SIGNAL(cellChanged(int, int)), this, SLOT(doSomething(int, int)));

You have to create a slot, for example doSomething:

public slots:
void doSomething(int row, int column)
{
    // Get cell text
    QString text = yourTableWidget->item(row,column)->text();

    // Emit 
    emit somethingIsDone(row,column,text);
}

Create the signal somethingIsDone (or use an existing signal) which use (int,int,QString) parameters (the parameters could be in another order)

signals:
    void somethingIsDone(int row, int column, QString text);
Rémi
  • 525
  • 1
  • 15
  • 26
1

You must make a slot function and use QObject::connect to connect its with cellChanged signal.

For example:

QTableWidget* widget;
widget = new QTableWidget(this);
connect(widget, SIGNAL(cellChanged(int, int)), otherObject, SLOT(youSlot(int, int));

In your slot you can get QTableWidgetItem using received parameters: row and column number. And here you can emit your own signal containing also text.

QTableWidgetItem* item = widget->item(row, column);
QString textFromItem = item->data(Qt::UserRole);
emit cellChanged(row, column, textFromItem);

Of course, previously you have to declare your own signal:

signals:
   void cellChanged(int row, int col, QString text);

Your signal can be connected to other slot in the same way as cellChanged(int, int)

trivelt
  • 1,913
  • 3
  • 22
  • 44