如何将Button_SetElevationRequiredState与QPushButton一起使用

How to use Button_SetElevationRequiredState with QPushButton?

本文关键字:QPushButton 一起 SetElevationRequiredState Button      更新时间:2023-10-16

我试过这个:

QPushButton* openHostsPushButton = new QPushButton("Open Hosts");
Button_SetElevationRequiredState(openHostsPushButton->winId(), true);
openHostsPushButton->setMaximumSize(aPushButtonMaxSize);
connect(openHostsPushButton, SIGNAL(clicked()),
        this, SLOT(openHostsClicked()));

但是没有起作用。

我不确定这是否可行,因为Qt实际上并不使用本机控件,而是自定义绘制所有内容。Windows将该图标放置在按钮上的唯一方法是,如果您使用具有类Button的窗口,则需要窗口管理器为您创建该类。

还要注意的是,为了实现这一点,您必须在应用程序中包含一个清单,以链接到通用控件的版本6(Comctl32.dll),但如果您支持Windows Vista和7,您可能已经在这样做了。

但使用涉及QStyle:的Qt有一种更简单的方法

QIcon shieldIcon = QApplication::style()->standardIcon(QStyle::SP_VistaShield);
openHostsPushButton->setIcon(shieldIcon);

在不支持UAC的系统上,图标将不会出现。