Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: adapting signals / binding arguments to slots?

Tags:

c++

boost

qt

Is there any way to bind arguments to slots ala boost::bind?

Here's a for-instance. I have a window with a tree view, and I want to allow the user to hide a column from a context menu. I end up doing something like:

void MyWindow::contextMenuEvent (QContextMenuEvent* event) {
   m_column = view->columnAt (event->x());
   QMenu menu;
   menu.addAction (tr ("Hide Column"), this, SLOT (hideColumn ()));
   // .. run the menu, etc
}

I need to capture the index of the column over which the context menu was activated and store it in a member variable that is used by my window's hideColumn slot:

void MyWindow::hideColumn () {
    view->setColumnHidden (m_column, true);
}

What I'd really like is to be able to bind the column number to my slot when I create the menu so I don't need this member variable. Basically the Qt equivalent of:

menu.addAction (tr ("Hide Column"),
                boost::bind (&MyWindow::hideColumn, this,
                             event->columnAt (event->x()));

Or even better yet adapting the QAction::triggered signal and attaching it to the QTreeView::hideColumn slot, which takes the column index as an argument:

menu.addAction (tr ("Hide Column"),
                boost::bind (&QTreeView::hideColumn, view,
                             event->columnAt (event->x())));

Is any of this do-able?

like image 876
Bklyn Avatar asked Dec 02 '22 07:12

Bklyn


1 Answers

LibQxt makes this possible through QxtBoundFunction and QxtMetaObject (the former does not show up in their documentation for some reason). It's an Open Source project, so you can grab the source if you're interested in the implementation. It uses the following syntax:

connect(
    button,
    SIGNAL(clicked()),
    QxtMetaObject::bind(
        lineEdit,
        SLOT(setText(QString)),
        Q_ARG(QString, "Hello World!)
        )
    );

LibQxt has some other very useful features like the ability to emit signals over a QIODevice, such as network connection.

like image 179
Kaleb Pederson Avatar answered Dec 05 '22 18:12

Kaleb Pederson