Ufx
Ufx

Reputation: 2695

QTableWidget. Emit cellChanged signal

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.

Upvotes: 4

Views: 11815

Answers (2)

Rémi
Rémi

Reputation: 533

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);

Upvotes: 6

trivelt
trivelt

Reputation: 1965

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)

Upvotes: 1

Related Questions