QMenu cannot read function Slot



  • My intent is to create a context menu to copy to the clipboard the contents of a cell. 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: No such slot MainWindow::copyToClipboard(tw, row, col)"

    Although the function is properly declared. This is my code:

    header file:

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();
    

    public slots:
    void ProvideContextMenu(const QPoint& pos);
    void copyToClipboard(QTableWidget *tw, int row, int col);
    private:
    Ui::MainWindow *ui;

    QTableWidget* MyTableWidgetOne;
    QTableWidget* MyTableWidgetTwo;
    
    QClipboard *clipboard;
    

    };

    #endif // MAINWINDOW_H
    @

    cpp file:
    @
    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(tw, row, col)));
    menu.exec(tw->mapToGlobal(pos));
    }

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


  • Moderators

    You need to use the value type and not the name to use in your connect statement.

    @
    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());
    }
    @



  • [quote author="koahnig" date="1401968112"]You need to use the value type and not the name to use in your connect statement.

    @
    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());
    }
    @[/quote]

    The error message still return


  • Moderators

    Did you try a complete rebuild all?



  • Yes, this is the error message:

    "QObject::connect: No such slot MainWindow::copyToClipboard(QTableWidget, int,int) in ..\untitled4\mainwindow.cpp:768"

    http://postimg.org/image/4o29pyyvx/

    I'm using Qt 5.3 with Qt Creator 3.1.1


  • Moderators

    Sorry, I had a typing error in my response
    @
    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());
    }
    @

    You need basically to copy the minimal declaration as you need in a header file without the leading void.
    @
    void copyToClipboard(QTableWidget *tw, int row, int col); // that common and convenient
    void copyToClipboard(QTableWidget *, int, int); // that is minimal and for compilation identical
    void copyToClipboard(*tw, row, col); // wrong cannot work
    @

    During run time the parameter list types are compared and if they do not match the error message is displayed. You need read the error message carefully. After the change it did not have the pointer.



  • Now display this error:

    http://postimg.org/image/jev2p9k4r/


  • Lifetime Qt Champion

    Hi,

    The slot signature must match the signal signature or have less (but still matching) parameters. You are trying to connect a signal which signature is triggered(bool) to a slot which is copyToClipboard(QTableWidget*, int, int). You can't do that.



  • So, what is the solution?


  • Moderators

    If you're using c++11 one way is a lambda:
    @
    void MainWindow::ProvideContextMenu(const QPoint& pos) // this is a slot
    {
    QTableWidget *tw = (QTableWidget *)sender();
    int row = tw->currentRow();
    int col = tw->currentColumn();

    QMenu menu;
    auto action = menu.addAction("Test Item");
    connect(action, &QAction::triggered, [&](){
        copyToClipboard(tw, row, col);
    });
    menu.exec(tw->mapToGlobal(pos));
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.