Important: Please read the Qt Code of Conduct -

Same function for different widget

  • I have two different QTableWidget, MyTableWidgetOne and MyTableWidgetTwo, and I want connect the event that drops the context menu to each of them:

    void MainWindow::ProvideContextMenu(const QPoint& pos) // this is a slot
    QMenu *menu = new QMenu;
    menu->addAction(QString("Test Item"), this, SLOT(test_slot()));

    Now, there is a way to not rewrite the function twice, once for each of the two tables?

  • Moderators

    There are couple of ways of course.
    One of them is to make the QTableWidget pointer a parameter of the slot. You can then use sind or lambda to connect to appropriate widget.

    Another option, if the signal is emitted by the table widgets, is to use sender() method inside your slot. It will point to the sending object. Cast is to QTableWidget and there you go.

Log in to reply