QTableWidget cell related signals

Use a piece of code to detail the relevant signals emitted when QTableWidget’s cells are clicked (including single-click and double-click) and the content is edited.
(1) Mainly include cellChanged, cellActivated, cellClicked, cellDoubleClicked, cellEntered, cellPressed and corresponding item class signals
(2) Timing of sending each signal
(3) The order in which signals are sent

Through the following code, we explore in detail what Qt does behind the scenes between clicking one cell and clicking another cell.

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->resize(500,300);
    QTableWidget* table=new QTableWidget(3,5);
    //Enable mouse tracking for the table
    table->setMouseTracking(true);
    this->init_table(table);
    this->setup_connections(table);
    this->setup_connections2(table);
    this->setup_connections3(table);
    this->setCentralWidget(table);
}

void MainWindow::init_table(QTableWidget *table)
{
    //After initializing the table, there is no need to double-click to edit the table. Click the cell to send a signal: QTableWidget::itemClicked
    for(int i=0;i<table->rowCount();i + + )
    {
        for(int j=0;j<table->columnCount();j + + )
        {
            table->setItem(i,j,new QTableWidgetItem(""));
        }
    }
}

void MainWindow::setup_connections(QTableWidget *table)
{
    //Send this signal when the enter key is pressed in the current cell
    connect(table, & amp;QTableWidget::cellActivated,[=](int row,int col){
        qDebug()<<"QTableWidget::cellActivated"<<row<<","<<col;
    });
    //The content of the cell is modified and the signal is sent.
    //Content modification refers to data submission to model or cell?
    connect(table, & amp;QTableWidget::cellChanged,[=](int row,int col){
        qDebug()<<"QTableWidget::cellChanged"<<row<<","<<col;
    });

    //This signal will be emitted when a cell is clicked to select.
    //The click signal is emitted when the left mouse button is released.
    //The process of clicking on a cell to create a selection will also have a click action, but if the end cell of the selection is not in the clicked cell, the signal will not be emitted.
    connect(table, & amp;QTableWidget::cellClicked,[=](int row,int col){
        qDebug()<<"QTableWidget::cellClicked"<<row<<","<<col;
    });

    //A signal for clicking a cell must be sent before the double-click cell signal is sent.
    //Double-clicking the right button will also emit this signal by default, but double-clicking the right button will not emit a click signal.
    connect(table, & amp;QTableWidget::cellDoubleClicked,[=](int row,int col){
        qDebug()<<"QTableWidget::cellDoubleClicked"<<row<<","<<col;
    });

    //(1) When the mouse tracking of the table is turned on, the signal is sent immediately when the mouse moves into the cell.
    //(2) Without turning on the mouse tracking of the table:
    //This signal is emitted when the left or right mouse button is pressed and starting to move (drag)
    //This signal will be emitted every time the mouse enters a cell.
    connect(table, & amp;QTableWidget::cellEntered,[=](int row,int col){
        qDebug()<<"QTableWidget::cellEntered"<<row<<","<<col;
    });

    //Emitted when a cell is clicked. Unlike cellClicked, it is emitted when the left or right mouse button is pressed.
    connect(table, & amp;QTableWidget::cellPressed,[=](int row,int col){
        qDebug()<<"QTableWidget::cellPressed"<<row<<","<<col;
    });


}

void MainWindow::setup_connections2(QTableWidget *table)
{
    //================================================ ===================
    //When the selected state switches to the current cell by clicking a cell,
    //order:
    //currentCellChanged -> itemSelectionChanged -> cellPressed

    //If you use the keyboard arrow keys to move the selection, the order:
    //itemSelectionChanged -> currentCellChanged


    connect(table, & amp;QTableWidget::currentCellChanged,[=](int crow,int ccol,int prow,int pcol){
        qDebug()<<"QTableWidget::currentCellChanged("<<crow<<","<<ccol<<")<-("<<prow<<","<<pcol <<")";
    });

    //This signal is emitted when the currently selected cell is switched.
    //But if the current cell has not been edited, that is, its QTableWidgetItem is nullptr
    //The following signal will not be emitted when switching the selected state between two cells that have not been edited.
    //Switching from an edited cell to an unedited cell, or from an unedited cell to an edited cell will emit this signal
    connect(table, & amp;QTableWidget::currentItemChanged,[=](QTableWidgetItem *current, QTableWidgetItem *previous){
        qDebug()<<"QTableWidget::currentItemChanged"<<current<<"<-"<<previous;
    });

    //Click an edited cell to send the signal
    //Click a cell that has not been edited, that is, its QTableWidgetItem is nullptr, and the signal will not be emitted.
    //Same as cellClicked, emit a signal when the mouse is lifted
    connect(table, & amp;QTableWidget::itemClicked,[=](QTableWidgetItem *item){
        qDebug()<<"QTableWidget::itemClicked:"<<item;
    });

    //Click an edited cell to send the signal
    //Click a cell that has not been edited, that is, its QTableWidgetItem is nullptr, and the signal will not be emitted.
    //Same as cellPressed, emit a signal when the mouse is pressed
    connect(table, & amp;QTableWidget::itemPressed,[=](QTableWidgetItem *item){
        qDebug()<<"QTableWidget::itemPressed:"<<item;
    });

    connect(table, & amp;QTableWidget::itemSelectionChanged,[=](){
        qDebug()<<"QTableWidget::itemSelectionChanged";
    });
}

void MainWindow::setup_connections3(QTableWidget *table)
{
    //The signal here is written as QTableView:: because the following signals are inherited from QTableView

    //Same as QTableWidget::cellActivated, this signal is emitted when the current cell presses enter.
    connect(table, & amp;QTableView::activated,[=](const QModelIndex & amp;index){
        qDebug()<<"QTableView::activated"<<index;
    });

    //Emitted when the cell is clicked (the mouse button is lifted) and issued after QTableWidget::cellClicked
    connect(table, & amp;QTableView::clicked,[=](const QModelIndex & amp;index){
        qDebug()<<"QTableView::clicked"<<index;
    });

    //Mouse behavior is the same as QTableWidget::cellDoubleClicked
    //Emitted after QTableWidget::cellDoubleClicked
    connect(table, & amp;QTableView::doubleClicked,[=](const QModelIndex & amp;index){
        qDebug()<<"QTableView::doubleClicked"<<index;
    });

    //Mouse behavior is the same as QTableWidget::cellEntered
    //Emitted after QTableWidget::cellEntered
    connect(table, & amp;QTableView::entered,[=](const QModelIndex & amp;index){
        qDebug()<<"QTableView::entered"<<index;
    });

    //Emitted when clicking a cell (button pressed) and issued after QTableWidget::cellPressed
    connect(table, & amp;QTableView::pressed,[=](const QModelIndex & amp;index){
        qDebug()<<"QTableView::pressed"<<index;
    });

    //This signal will only be emitted when table->setMouseTracking(true);
    //Send this signal on the window where the mouse moves from outside the window into the table window (excluding the area containing cells)
    connect(table, & amp;QTableView::viewportEntered,[=](){
        qDebug()<<"QTableView::viewportEntered";
    });
}