PolkitQt-1

Polkit-qt-1 usage example

You can find an example usage of Polkit-qt-1 in the examples/ dir. You can build it by passing -DBUILD_EXAMPLES=TRUE to your cmake line. The structure consists of a .ui file and a main class, to demonstrate how easy it is to integrate polkit support in an existing application. Let's see some details about it:

bt = new ActionButton(kickPB, "org.qt.policykit.examples.kick", this);
bt->setText("Kick... (long)");
// here we set the behavior of PolKitResul = No
bt->setVisible(true, Action::No);
bt->setEnabled(true, Action::No);
bt->setText("Kick (long)", Action::No);
bt->setIcon(QPixmap(":/Icons/custom-no.png"), Action::No);
bt->setToolTip("If your admin wasn't annoying, you could do this", Action::No);
// here we set the behavior of PolKitResul = Auth
bt->setVisible(true, Action::Auth);
bt->setEnabled(true, Action::Auth);
bt->setText("Kick... (long)", Action::Auth);
bt->setIcon(QPixmap(":/Icons/action-locked-default.png"), Action::Auth);
bt->setToolTip("Only card carrying tweakers can do this!", Action::Auth);
// here we set the behavior of PolKitResul = Yes
bt->setVisible(true, Action::Yes);
bt->setEnabled(true, Action::Yes);
bt->setText("Kick! (long)", Action::Yes);
bt->setIcon(QPixmap(":/Icons/custom-yes.png"), Action::Yes);
bt->setToolTip("Go ahead, kick kick kick!", Action::Yes);

This small paragraph sets up an action button using an existing button defined in the UI file, kickPB . As you can see, you can set custom properties on your button depending on the action status/result. The code is mostly self-explainatory

bt = new ActionButtons(QList<QAbstractButton*>() << listenPB << listenCB,
"org.qt.policykit.examples.listen", this);
bt->setIcon(QPixmap(":/Icons/action-locked.png"));
bt->setIcon(QPixmap(":/Icons/action-unlocked.png"), Action::Yes);
bt->setText("Click to make changes...");

This demonstrates the use of ActionButtons, that lets you associate multiple buttons with a single action with extreme ease. listenPB and listenCB, both defined in the ui file, are kept in sync with the action.

connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction()));
connect(bt, SIGNAL(clicked(QAbstractButton*,bool)), bt, SLOT(activate()));
connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated()));

Those three signals are all you need to control the action and the activation. Action::triggered() lets you start the activation/revoke when needed, ActionButton::clicked() lets you do the same thing with even more ease, just by manually connecting the signal to ActionButton::activate() (see the docs to understand why this connection doesn't happen automatically), and Action::authorized() signal notifies you when polkit has authorized you to perform the action.

As you can see, usage of polkit-qt-1 is extremely simple. Have a look at the complete example and to the API Docs for more details.

This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:56 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.