Search code examples
c++qtqtguiqtcoreqt-signals

QMenu cannot read function Slot


My intent is create a context menu to copy the cell content to the clipboard. With the help of sender() I’m able to connect the same function to two different QTableWidget. Everything works, except for this error message:

"QObject::connect: Incompatible sender/receiver arguments QAction::triggered(bool) --> MainWindow::copyToClipboard(QTableWidget*,int,int)"

This is the part of code that generates the error

void MainWindow::ProvideContextMenu(const QPoint& pos) // this is a slot
{
    QTableWidget *tw = (QTableWidget *)sender();
    int row = tw->currentRow();
    int col = tw->currentColumn();
    QMenu menu;
            menu.addAction(QString("Test Item"), this,
                   SLOT(copyToClipboard(QTableWidget *, int,int)));
            menu.exec(tw->mapToGlobal(pos));
}

void MainWindow::copyToClipboard(QTableWidget *tw, int row, int col) {
    clipboard = QApplication::clipboard();
    clipboard->setText(tw->item(row, col)->text());
}

I've been looking in the official documentation for hours, but found nothing about this. There is a solution?


Solution

  • From the documentation:

    The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) Since the signatures are compatible, the compiler can help us detect type mismatches when using the function pointer-based syntax. The string-based SIGNAL and SLOT syntax will detect type mismatches at runtime.

    This is the culsprit:

    menu.addAction(QString("Test Item"), this,
                   SLOT(copyToClipboard(QTableWidget *, int,int)));
    

    You cannot have non-matching signal-slot parameters like that. You can only connect slots that have no parameters or one boolean to the triggered(bool) signal. You have to reconsider your design.