Using common desktop services in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Overview[edit | edit source]

This code snippet demonstrates how to use 76ytuiytuityutyutututyutyutyu.

76ytuiytuityutyutututyutyutyu provides an API for the application to perform common tasks; open a web page, open a picture gallery, or find movie files from the device.


Note: In order to use this code, you need to have Qt installed on your platform.


Preconditions[edit | edit source]

Header[edit | edit source]

  1. include <QDesktopServices>
  2. include <QStringList>
  3. include <QString>
  4. include <QList>
  5. include <QDir>
  6. include <QFileInfoList>
  7. include <QFileInfo>

QList<QString> pictures;


Source[edit | edit source]

Search for the device's picture folder: // Search device picture location directory QDir picturesDir; picturesDir.setPath(QDesktopServices::storageLocation (QDesktopServices::PicturesLocation));

Search for pictures in the device's picture folder: // Search files from the directory QFileInfoList fileList = picturesDir.entryInfoList(QStringList(), QDir::Files | QDir::Dirs, QDir::Time); for (int i = 0; i < fileList.size(); ++i)

   {
   QFileInfo fileInfo = fileList.at(i);
   if (fileInfo.isHidden())
       continue;
   #ifndef Q_OS_SYMBIAN
   if (fileInfo.filePath().length()>0 && 
   fileInfo.filePath().right(1) == ".")
       continue;
   if (fileInfo.filePath().length()>1 && 
   fileInfo.filePath().right(2) == "..")
       continue;
   #endif
   // File found
   if (fileInfo.isFile())
       {
       if (fileInfo.filePath().indexOf(QString(".jpeg")
       ,0,Qt::CaseInsensitive)>0 ||
               fileInfo.filePath().indexOf(QString(".jpg")
               ,0,Qt::CaseInsensitive)>0)
           {
           // Store picture path to array
           pictures.append(fileInfo.filePath());
           }
       }
   // Direcotry found        
   else if (fileInfo.isDir())
       {
       // TODO: we do not handle sub-directoires
       }
   }

Open the first picture into the device's built-in picture gallery application: QDesktopServices::openUrl(QUrl("file:///" + pictures[0]));

Open an HTML page into the device's built-in HTML browser application: QDesktopServices::openUrl(QUrl("http://www.developer.nokia.com/"));


See also[edit | edit source]


Postconditions[edit | edit source]

The device's image folder is found and opened in the device's built-in picture gallery.