User Tools

Site Tools


qt:mainwindow-buildicon

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
qt:mainwindow-buildicon [2011/05/12 16:00] mithatqt:mainwindow-buildicon [2011/05/13 12:24] mithat
Line 2: Line 2:
  
 <code cpp-qt MainWindow::buildIcon> <code cpp-qt MainWindow::buildIcon>
-#include "mainwindow.h" 
-#include "ui_mainwindow.h" 
- 
-#include <QDebug> 
-#include <QIcon> 
- 
-MainWindow::MainWindow(QWidget *parent) : 
-    QMainWindow(parent), 
-    ui(new Ui::MainWindow) 
-{ 
-    ui->setupUi(this); 
- 
-//    QIcon::setThemeName("Gion"); 
-//    ui->action_Open->setIcon(QIcon::fromTheme("document-open")); 
-//    ui->actionSave_as->setIcon(QIcon::fromTheme("document-save-as")); 
-//    ui->action_Quit->setIcon(QIcon::fromTheme("exit")); 
- 
-    // we need a function that takes QIcon ands the sub-icons automatically: 
-    // theQIcon, formatString (with placeholder for size, int array of sizes) 
- 
-    QIcon documentOpenIcon; 
-    documentOpenIcon.addFile( 
-                ":myIcons/16x16/document-open.png",QSize(16, 16)); 
-    documentOpenIcon.addFile( 
-                ":myIcons/24x24/document-open.png", QSize(24, 24)); 
-    documentOpenIcon.addFile( 
-                ":myIcons/24x24/document-open.png", QSize(32, 32)); 
-    ui->action_Open->setIcon(documentOpenIcon); 
- 
-    QIcon saveAsIcon; 
-    saveAsIcon.addFile( 
-                ":myIcons/16x16/document-save-as.png", QSize(16, 16)); 
-    saveAsIcon.addFile( 
-                ":myIcons/24x24/document-save-as.png", QSize(24, 24)); 
-    saveAsIcon.addFile( 
-                ":myIcons/24x24/document-save-as.png", QSize(32, 32)); 
-    ui->actionSave_as->setIcon(saveAsIcon); 
- 
-    QIcon exitIcon; 
-    exitIcon.addFile( 
-                ":myIcons/16x16/application-exit.png", QSize(16, 16)); 
-    exitIcon.addFile( 
-                ":myIcons/24x24/application-exit.png", QSize(24, 24)); 
-    exitIcon.addFile( 
-                ":myIcons/24x24/application-exit.png", QSize(32, 32)); 
-    ui->action_Quit->setIcon(exitIcon); 
- 
-//    This mess doesn't work! 
-//    setActionIcon(ui->action_Open, "document-open", "Gion"); 
-//    setActionIcon(ui->actionSave_as, "document-save-as", "Gion"); 
-//    setActionIcon(ui->action_Quit, "exit", "Gion"); 
- 
-} 
- 
-MainWindow::~MainWindow() 
-{ 
-    delete ui; 
-} 
- 
-bool MainWindow::setActionIcon(QAction *theAction, 
-                               QString const &iconName, 
-                               QString const &backupTheme) 
-// Try to set theAction's icon from the iconName in the system icon theme. 
-// If the iconName isn't found in the system theme, try the backupTheme. 
-// Return true iff the icon was set. 
-// Ha! This doesn't work because setting the theme name is dynamic! 
-// trying to see what happens when we make a local QIcon to store the icon ... 
-// that doesn't work either! 
-{ 
-    QString systemTheme = QIcon::themeName(); 
-    qDebug() << "iconName: " << iconName << 
-                "systemTheme: " << systemTheme << 
-                "backupTheme: " << backupTheme; 
- 
-    if ( !systemTheme.isEmpty() && QIcon::hasThemeIcon(iconName) ) 
-    { 
-        QIcon theIcon = QIcon::fromTheme(iconName); 
-        theAction->setIcon(theIcon); 
-        qDebug() << "set system theme icon for " << iconName; 
-        return true; 
-    } 
- 
-    if (!backupTheme.isEmpty()) 
-    { 
-        QIcon::setThemeName(backupTheme); 
-        if (QIcon::hasThemeIcon(iconName)) 
-        { 
-            QIcon theIcon = QIcon::fromTheme(iconName); 
-            theAction->setIcon(theIcon); 
-            qDebug() << "Haz backup icon"; 
-        } 
-        QIcon::setThemeName(systemTheme); 
- 
-        qDebug() << "set backup theme icon for " << iconName; 
-        return true; 
-    } 
-    qDebug() << "did not set icon for " << iconName; 
-    return false; 
-} 
- 
-void MainWindow::on_action_Quit_triggered() 
-{ 
-    this->close(); 
-} 
- 
 /** /**
- * Append a regularly defined set of different sized icon images to a QIcon.+ @brief Append a regularly defined set of sized icon images to a QIcon.
  *  *
  * The following usage example assumes the resources ":myIcons/16x16/exit.png",  * The following usage example assumes the resources ":myIcons/16x16/exit.png",
  * ":myIcons/24x24/exit.png", and ":myIcons/32x32/exit.png" are available and  * ":myIcons/24x24/exit.png", and ":myIcons/32x32/exit.png" are available and
  * that we want to bundle them together into exitIcon.  * that we want to bundle them together into exitIcon.
- + *
  * \code  * \code
    QList<int> sizes; sizes << 16 << 24 << 32;    QList<int> sizes; sizes << 16 << 24 << 32;
Line 119: Line 14:
    buildIcon(&exitIcon, ":myIcons/%size%/exit.png", sizes);    buildIcon(&exitIcon, ":myIcons/%size%/exit.png", sizes);
  * \endcode  * \endcode
- + *
  * Does NOT check whether the referenced resources actually exist.  * Does NOT check whether the referenced resources actually exist.
  *  *
Line 126: Line 21:
 void MainWindow::buildIcon(QIcon* const theIcon,           ///< Pointer to the icon that will be built/appened to. void MainWindow::buildIcon(QIcon* const theIcon,           ///< Pointer to the icon that will be built/appened to.
                            QString const &iconRscTemplate, ///< A tag-based template string for the icon resource. Two tags are recognized in iconRscTemplate: \%size\% and \%dim\%. When the iconRscTemplate is expanded, the tag \%dim\% will be replaced by {iconSize} and \%size\% will be replaced by {iconSize}x{iconSize}, where {iconSize} is a string cast of an element in sizeList.                            QString const &iconRscTemplate, ///< A tag-based template string for the icon resource. Two tags are recognized in iconRscTemplate: \%size\% and \%dim\%. When the iconRscTemplate is expanded, the tag \%dim\% will be replaced by {iconSize} and \%size\% will be replaced by {iconSize}x{iconSize}, where {iconSize} is a string cast of an element in sizeList.
-                           QList const &sizeList           ///< A list of single-dimension sizes to be added to the icon.+                           QList<int> const &sizeList      ///< A list of single-dimension sizes to be added to the icon.
                           )                           )
 { {
-    foreach (const int iconSize in sizeList)+    foreach (const int iconSizesizeList)
     {     {
         QString theResource = iconRscTemplate;         QString theResource = iconRscTemplate;
-        theResource.replace(QString("%size%"), iconSize+"x"+iconSize);  // does this work? +        QString dim; dim.setNum(iconSize); 
-        theResource.replace(QString("%dim%"), iconSize+"");             // does this work?+        theResource.replace(QString("%dim%"), dim); 
 +        theResource.replace(QString("%size%"), dim +"x"+dim);
  
-        theIcon->addFile(theResource, QSize(iconSize, iconSize))+        theIcon->addFile(theResource, QSize(iconSize, iconSize));
     }     }
 }</code> }</code>
qt/mainwindow-buildicon.txt · Last modified: 2011/05/13 12:32 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki