【问题标题】:How to get list of application icons which are installed in device with Qt如何使用 Qt 获取安装在设备中的应用程序图标列表
【发布时间】:2012-05-13 04:17:56
【问题描述】:

我正在使用 Qt 应用程序,我想在其中创建一个 QListWidget,其中包含安装在设备上的所有应用程序的名称及其图标。 所以我无法从LINK 中的代码中获取每个应用程序的所有应用程序名称和 UID。 但我也无法获取应用程序图标。我尝试了这两个LINK1LINK2,但在这里我遇到了一些问题,比如如何将 CGulIcon 转换为 QIcon 和 CApaMaskedBitmap 转换为 QIcon。

我该怎么做?

【问题讨论】:

  • 请在给出任何答案之前参考所有三个链接,并且该链接中的代码不会编译。

标签: qt symbian qicon


【解决方案1】:

如果您无法得到解决方案或有任何疑问click here进行讨论。

ma​​in.cpp

#include "GetInstalledApps.h"

#include <QtGui>
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    GetInstalledApps w;
    w.showMaximized();
    return a.exec();
}

.pro

TEMPLATE = app
TARGET = GetInstalledApps 

QT        += core \
    gui 

HEADERS   += GetInstalledApps.h
SOURCES   += main.cpp \
    GetInstalledApps.cpp
FORMS     += GetInstalledApps.ui
RESOURCES +=

HEADERS += xqinstaller_p.h \
   xqinstaller.h \

SOURCES += xqinstaller_p.cpp \
   xqinstaller.cpp

symbian:LIBS += -lswinstcli \
   -lcommdb \
   -lapparc \
   -lefsrv \
   -lapgrfx \


symbian:TARGET.CAPABILITY += TrustedUI
symbian:TARGET.UID3 = 0xEF3055F4

GetInstalledApps.h

#ifndef GETINSTALLEDAPPS_H
#define GETINSTALLEDAPPS_H

#include <QtGui/QMainWindow>
#include "ui_GetInstalledApps.h"
#include "xqinstaller.h"

class GetInstalledApps : public QMainWindow
{
    Q_OBJECT

public:
    GetInstalledApps(QWidget *parent = 0);
    ~GetInstalledApps();

private:
    void GetApps();
    
private:
    Ui::GetInstalledAppsClass ui;
    XQInstaller* m_installer;
    QMap<QString, uint> m_applications;
    QList<QString> m_appNames;
    QList<uint> m_appUID;
};

#endif // GETINSTALLEDAPPS_H

GetInstalledApps.cpp

#include "GetInstalledApps.h"
#include <QScrollArea>

GetInstalledApps::GetInstalledApps(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    setWindowTitle("GetAllInstalledApps");
    m_installer = new XQInstaller(this);
    GetApps();
    
}

void GetInstalledApps::GetApps()
{
    /* Get List of applications. 
     * type of m_applications is QMap<QString, uint>.
     * type of  m_installer is XQInstaller
     */
    m_applications = m_installer->applications();
    
    /* Get List of application names
     type of m_appNames is QList<QString> */
    m_appNames = m_applications.keys(); 
    
    /* Get List of application UID in decimal. 
    type of m_appUID is QList<uint> */
    m_appUID = m_applications.values(); 
    
    ui.listWidget->clear();
    for (int i = 0; i < m_appNames.count(); i++) 
    {
        QString str;
        /* convert UID from decimal to hex and then string. */
        str.setNum(m_appUID.at(i),16);
        /* append name and UID to string */     
        QString string(m_appNames.at(i)+"  UID:"+ str);
        /* append string to list widget to display on screen */
        ui.listWidget->addItem(string);
    }
    
    /* Let's make the UI scale so that we can scroll it. */
    QScrollArea* scrollArea = new QScrollArea;
    scrollArea->setWidget(ui.listWidget);
    scrollArea->setWidgetResizable(true);

    setCentralWidget(scrollArea);
}

GetInstalledApps::~GetInstalledApps()
{

}

xqinstaller.h

#ifndef XQINSTALLER_H
#define XQINSTALLER_H

// INCLUDES
#include <QObject>
#include <QMap>

class XQInstallerPrivate;

// CLASS DECLARATION
class XQInstaller : public QObject
{
    Q_OBJECT

public:
    enum Error {
        NoError = 0,
        OutOfMemoryError,
        AlreadyInUseError,
        UserCancelError, 
        PackageNotSupportedError,
        SecurityFailureError,
        MissingDependencyError,
        NoRightsError,
        BusyError,
        AccessDeniedError,
        UpgradeError,
        UnknownError = -1
    };
    
    enum Drive {
        DriveA,   DriveB,   DriveC,   DriveD,   DriveE,
        DriveF,   DriveG,   DriveH,   DriveI,   DriveJ,
        DriveK,   DriveL,   DriveM,   DriveN,   DriveO, 
        DriveP,   DriveQ,   DriveR,   DriveS,   DriveT,
        DriveU,   DriveV,   DriveW,   DriveX,   DriveY,
        DriveZ
    };

    XQInstaller(QObject *parent = 0);
    ~XQInstaller();
    
    bool install(const QString& file, XQInstaller::Drive drive = XQInstaller::DriveC);
    QMap<QString, uint> applications() const;
    bool remove(uint uid);

    XQInstaller::Error error() const;
    
Q_SIGNALS:
    void applicationInstalled();
    void applicationRemoved();
    void error(XQInstaller::Error);
    
private:
    friend class XQInstallerPrivate;
    XQInstallerPrivate *d;
};

#endif // XQINSTALLER_H

xqinstaller.cpp

#include "xqinstaller.h"
#include "xqinstaller_p.h"

/*!
    \class XQInstaller
    \brief The XQInstaller class is used to install sis-packages silently. 
    This extension can be used for example to install back-end applications.

    Example:
    \code
    XQInstaller *installer = new XQInstaller(this);
    QMap<QString, uint> applications = installer->applications();
    QListWidget *applicationList = new QListWidget(this);
    QList<QString> appNames = applications.keys();
    for (int i = 0; i < appNames.count(); i++) {
        applicationList->addItem(appNames.at(i));
    }   
    \endcode
*/

/*!
    Constructs a XQInstaller object with the given parent.
    \sa install(), remove()
*/
XQInstaller::XQInstaller(QObject *parent)
    : QObject(parent), d(new XQInstallerPrivate(this))
{
}

/*!
    Destroys the XQInstaller object.
*/
XQInstaller::~XQInstaller()
{
    delete d;
}

/*!
    Installs a sis package silently given as parameter.

    \param file Sis package
    \param drive Drive letter where the sis is installed to. Default value is 'C'.
    \return If false is returned, an error has occurred. Call error() to get a value of 
    XQInstaller::Error that indicates which error occurred
    \sa error()
*/  
bool XQInstaller::install(const QString& file, XQInstaller::Drive drive)
{
    return d->install(file, drive);
}

/*!
    Get list of installed applications
    If an empty QMap is returned, an error has possibly occurred. Call error() to get a value of 
    XQInstaller::Error that indicates which error occurred if any
    
    \return List of installed applications
    \sa error()
*/
QMap<QString, uint> XQInstaller::applications() const
{
    return d->applications();
}

/*!
    Removes application specified by the uid
   
    \param uid of the application
    \return True if removing was successfully started, otherwise false
    \sa error()
*/
bool XQInstaller::remove(uint uid)
{
    return d->remove(uid);
}

/*!
    \enum XQInstaller::Error

    This enum defines the possible errors for a XQInstaller object.
*/
/*! \var XQInstaller::Error XQInstaller::NoError
    No error occured.
*/
/*! \var XQInstaller::Error XQInstaller::OutOfMemoryError
    Not enough memory.
*/
/*! \var XQInstaller::Error XQInstaller::AlreadyInUseError
    Installer is already in used.
*/
/*! \var XQInstaller::Error XQInstaller::UserCancelError
    Installer cancelled by the user.
*/
/*! \var XQInstaller::Error XQInstaller::PackageNotSupportedError
    Package not supported
*/
/*! \var XQInstaller::Error XQInstaller::SecurityFailureError
    Security failure
*/
/*! \var XQInstaller::Error XQInstaller::MissingDependencyError
    Missing dependency
*/
/*! \var XQInstaller::Error XQInstaller::NoRightsError
    No rights
*/
/*! \var XQInstaller::Error XQInstaller::BusyError
    Installer is busy
*/
/*! \var XQInstaller::Error XQInstaller::AccessDeniedError
    Access denied
*/
/*! \var XQInstaller::Error XQInstaller::UpgradeError
    Error while upgrading
*/
/*! \var XQInstaller::Error XQInstaller::UnknownError
    Unknown error.
*/

/*!
    Returns the type of error that occurred if the latest function call failed; otherwise returns NoError
    \return Error code
*/
XQInstaller::Error XQInstaller::error() const
{
    return d->error();
}

/*!
    \fn void XQInstaller::applicationInstalled()

    This signal is emitted when the application has been installed.

    \sa install()
*/

/*!
    \fn void XQInstaller::error(XQInstaller::Error)

    This signal is emitted if error occured during the asynchronous operation

    \sa install()
*/

/*!
    \fn void XQInstaller::applicationRemoved()

    This signal is emitted when the application has been removed.

    \sa remove()
*/

// End of file

xqinstaller_h.h

#ifndef XQINSTALLER_P_H
#define XQINSTALLER_P_H

// INCLUDES
#include "xqinstaller.h"
#include <SWInstApi.h>
#include <SWInstDefs.h>

// FORWARD DECLARATIONS
class QString;
class QFile;

// CLASS DECLARATION
class XQInstallerPrivate: public CActive
{

public:
    enum State {
        ERemove,
        EInstall
    };
    
    XQInstallerPrivate(XQInstaller *installer);
    ~XQInstallerPrivate();
    
    bool install(const QString& file, XQInstaller::Drive drive);
    bool remove(uint uid);
    QMap<QString, uint> applications() const;

public:
    XQInstaller::Error error();
    
protected:  
    void DoCancel();
    void RunL();

private:
    XQInstaller *q;
    mutable int iError;
    
    SwiUI::RSWInstSilentLauncher iLauncher;
    SwiUI::TInstallOptions iOptions;
    SwiUI::TInstallOptionsPckg iOptionsPckg;  
    
    SwiUI::TUninstallOptions iUninstallOptions;
    SwiUI::TUninstallOptionsPckg iUninstallOptionsPckg;
    XQInstallerPrivate::State iState;
    TFileName iFileName;
    bool iLauncherConnected;
};

#endif /*XQINSTALLER_P_H*/

// End of file

xqinstaller_h.cpp

#include "xqinstaller.h"
#include "xqinstaller_p.h"
#include <f32file.h>
#include <apgcli.h>

XQInstallerPrivate::XQInstallerPrivate(XQInstaller *installer) 
    : CActive(EPriorityNormal), q(installer), iOptionsPckg(iOptions), 
      iUninstallOptionsPckg(iUninstallOptions), iLauncherConnected(false)
{
    CActiveScheduler::Add(this);  
}

XQInstallerPrivate::~XQInstallerPrivate()
{
    Cancel();
    if (iLauncherConnected) {
        iLauncher.Close();
    }
}

bool XQInstallerPrivate::install(const QString& file, XQInstaller::Drive drive)
{
    int asciiValue = 10;  // = 'A'
    TRAP(iError,
        if (!iLauncherConnected) {
            User::LeaveIfError(iLauncher.Connect());
            iLauncherConnected = true;
        }
        if (IsActive()) {
            User::Leave(KErrInUse);
        }
        
        iState = XQInstallerPrivate::EInstall;
        iOptions.iUpgrade = SwiUI::EPolicyAllowed;
        iOptions.iOCSP = SwiUI::EPolicyNotAllowed;
        iOptions.iDrive = TChar(asciiValue+drive);
        iOptions.iUntrusted = SwiUI::EPolicyAllowed; 
        iOptions.iCapabilities = SwiUI::EPolicyAllowed;  
        iOptions.iOptionalItems = SwiUI::EPolicyAllowed;  
        iOptions.iOverwrite = SwiUI::EPolicyAllowed; 
        TPtrC16 fileName(reinterpret_cast<const TUint16*>(file.utf16()));
        iFileName = fileName;
        iLauncher.SilentInstall(iStatus, iFileName, iOptionsPckg);
        SetActive();
    )
    return (iError == KErrNone);
}

bool XQInstallerPrivate::remove(uint uid)
{
    TRAP(iError,
        if (!iLauncherConnected) {
            User::LeaveIfError(iLauncher.Connect());
            iLauncherConnected = true;
        }
        if (IsActive()) {
            User::Leave(KErrInUse);
        }

        iState = XQInstallerPrivate::ERemove;
    
        iLauncher.SilentUninstall(iStatus,TUid::Uid(uid),
            iUninstallOptionsPckg, SwiUI::KSisxMimeType);
        SetActive();
    )
    return (iError == KErrNone);
}

QMap<QString, uint> XQInstallerPrivate::applications() const
{
    RApaLsSession lsSession;
    QMap<QString, uint> applications; 
      
    // Connect to application architecture server
    TRAP(iError,
        User::LeaveIfError(lsSession.Connect());
        CleanupClosePushL(lsSession);
        
        TApaAppInfo appInfo;
        lsSession.GetAllApps();  
        
        while (lsSession.GetNextApp(appInfo) == KErrNone) {
            TApaAppCapabilityBuf capability;
            User::LeaveIfError(lsSession.GetAppCapability(capability,
                appInfo.iUid));
            if (appInfo.iCaption.Length() > 0 && !capability().iAppIsHidden) {
                QString fullName = QString::fromUtf16(
                    appInfo.iCaption.Ptr(), appInfo.iCaption.Length());
                applications.insert(fullName, (TUint)appInfo.iUid.iUid);
            }
        }
        CleanupStack::PopAndDestroy(&lsSession);
    )
      
    return applications; 
}

void XQInstallerPrivate::DoCancel()
{
    if (iState == XQInstallerPrivate::EInstall) {
        iLauncher.CancelAsyncRequest(SwiUI::ERequestSilentInstall);
    } else if (iState == XQInstallerPrivate::ERemove) {
        iLauncher.CancelAsyncRequest(SwiUI::ERequestSilentUninstall);
    }
}
 
void XQInstallerPrivate::RunL()
{
    if (iStatus.Int() == KErrNone) {
        if (iState == XQInstallerPrivate::EInstall) {
            emit q->applicationInstalled();
        } else if (iState == XQInstallerPrivate::ERemove) {
            emit q->applicationRemoved();
        }
    } else {
        iError = iStatus.Int();
        emit q->error(error());
    }
}

XQInstaller::Error XQInstallerPrivate::error()
{
    switch (iError) {
    case KErrNone:
        return XQInstaller::NoError;
    case SwiUI::KSWInstErrInsufficientMemory:
    case KErrNoMemory:
        return XQInstaller::OutOfMemoryError;
    case SwiUI::KSWInstErrFileInUse:
    case KErrInUse:
        return XQInstaller::AlreadyInUseError;
    case SwiUI::KSWInstErrUserCancel:
        return XQInstaller::UserCancelError;  
    case SwiUI::KSWInstErrPackageNotSupported:
        return XQInstaller::PackageNotSupportedError; 
    case SwiUI::KSWInstErrSecurityFailure:
        return XQInstaller::SecurityFailureError; 
    case SwiUI::KSWInstErrMissingDependency:
        return XQInstaller::MissingDependencyError; 
    case SwiUI::KSWInstErrNoRights:
        return XQInstaller::NoRightsError;
    case SwiUI::KSWInstErrBusy:
        return XQInstaller::BusyError;
    case SwiUI::KSWInstErrAccessDenied:
        return XQInstaller::AccessDeniedError;
    case SwiUI::KSWInstUpgradeError:
        return XQInstaller::UpgradeError;
    case SwiUI::KSWInstErrGeneralError:
    default:
        return XQInstaller::UnknownError;
    }    
}
 
// End of file

对于getting QIcon::

需要听众:

#include <fbs.h> //CFbsBitmap
#include <aknsskininstance.h> //MAknsSkinInstance 
#include <aknsutils.h> //AknsUtils

需要库:

LIBRARY   fbscli.lib ///CFbsBitmap
LIBRARY   aknskins.lib aknskinsrv.lib aknswallpaperutils.lib  //MAknsSkinInstance ,AknsUtils

源代码:

CGulIcon* CMyClass::GetApplicationIconL(const TUid& aAppUID)
{
    CFbsBitmap* AppIcon(NULL);
    CFbsBitmap* AppIconMsk(NULL);
 
    MAknsSkinInstance* skin = AknsUtils::SkinInstance();
 
    AknsUtils::CreateAppIconLC(skin,aAppUID,  EAknsAppIconTypeContext,AppIcon,AppIconMsk);
    CleanupStack::Pop(2);
 
    return CGulIcon::NewL(AppIcon,AppIconMsk);
}

【讨论】:

    【解决方案2】:

    好吧,对于图标的事情,看看这个:CGullIcon 有一个函数可以返回位图,一个 CFbsBitmap。一旦你完成了,看看这个:http://qt-project.org/doc/qt-4.8/qpixmap.html#fromSymbianCFbsBitmap 然后你创建一个新的 QIcon(QPixmap) (QPixmap = 你已经转换的图标)。所以很可能您首先将其传递给 CFbsBitmap,然后使用 QPixmap 使用 fromSymibnaCFbsBitmap()。

    如果我没记错的话,CApaMaskedBitmap 是 CFbsBitmap 的子类,所以它应该是一样的。

    要获取应用程序及其 UID,请尝试查看:http://www.developer.nokia.com/Community/Wiki/Get_list_of_installed_applications_and_its_UID_in_Qt

    【讨论】:

    • 对不起,不能添加太多超链接,因为我的代表仍然很低。 ://
    • -1 获取应用程序名称和 uid 的链接与问题中提到的相同,甚至无法编译。
    • 您尝试过使用 TApaAppInfo 吗?
    • 是的,我试过了,但什么都听不懂。如果您有任何示例/工作代码,请更新它。
    猜你喜欢
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多