0

An existing code implements some QActions within a QToolbar as in picture. The purpose is to make the selected Qaction's icon blinking.

Therefore, according to Wizard_Step - a class member- the dedicated QAction has its icon changed alternatively each time this timer handler is called.

While this is 100% working, this code is really hugly and I'd like to improve by factorize this code in an object manner. I'm thinking about something like using QAction pointers, arrays for example.

QActions form toolbar

void MainWindow::on_Timer_Update()
{
  Wizard_hide = !Wizard_hide ;

  switch (Wizard_Step)
  {
  case 1 :
      if ( Wizard_hide )
       ui->actionOpen_Dir->setIcon(QIcon(QPixmap(":/Icons/dir_selected" ) ));
      else
        ui->actionOpen_Dir->setIcon(QIcon(QPixmap(":/Icons/dir") )) ;
      break;
  case 2 :
      if ( Wizard_hide )
       ui->actionAdd_Selected_Item->setIcon(QIcon(QPixmap(":/Icons/Selected_row_selected" ) ));
      else
        ui->actionAdd_Selected_Item->setIcon(QIcon(QPixmap(":/Icons/Selected_row") )) ;
      break;
  case 3 :
      if ( Wizard_hide )
       ui->actionClean_Filename->setIcon(QIcon(QPixmap(":/Icons/clean_selected" ) ));
      else
        ui->actionClean_Filename->setIcon(QIcon(QPixmap(":/Icons/clean") )) ;
      break;
  case 4 :
      if ( Wizard_hide )
       ui->actionApply_changes->setIcon(QIcon(QPixmap(":/Icons/Apply_selected" ) ));
      else
        ui->actionApply_changes->setIcon(QIcon(QPixmap(":/Icons/Apply") )) ;
      break;
      default: qDebug() << "error"; 
 }


}
dlewin
  • 1,673
  • 1
  • 19
  • 37

2 Answers2

1

If you need to make a wizard, I suggest to use QWizard.

If you need to make your icons animated, I suggest to use a QMovie.

UPDATE


You can use a QList<QAction*> to keep you actions, and save the last triggered action. Doing so, when your Wizard_Step changes, you need to reset the last action, and set the current action.

QIcons can show multiple images according to internal to the icon state. So you don't need to change the icon, but simply tell the the icon state changed.

This snippet is a working example and should clarify this. I used a QComboBox to simulate changes to wizard step. You just need to connect to the slot onWizardStepChanged to update the icons. You don't need a timer to check if the wizard step changed, but emit a signal that wizard step has changed (here is done by the combo box).

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QList>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

public slots:
    void onWizardStepChanged(int wizardStep);

private:

    int m_lastWizardStep;
    QList<QAction*> m_actionList;
};

#endif // MAINWINDOW_H

MainWindow.cpp

#include "mainwindow.h"

#include <QLabel>
#include <QDebug>
#include <QAction>
#include <QToolBar>
#include <QComboBox>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{

    QComboBox* combo = new QComboBox(this);
    QStringList wizardSteps;
    wizardSteps << "Action0" << "Action1";
    combo->addItems(wizardSteps);

    setCentralWidget(combo);

    // Create Icons, with different images per state
    QIcon iconAction0;
    iconAction0.addFile(":/images/images/unchecked.png", QSize(), QIcon::Normal, QIcon::Off);
    iconAction0.addFile(":/images/images/checked.png", QSize(), QIcon::Normal, QIcon::On);

    QIcon iconAction1;
    iconAction1.addFile(":/images/images/unchecked.png", QSize(), QIcon::Normal, QIcon::Off);
    iconAction1.addFile(":/images/images/checked.png", QSize(), QIcon::Normal, QIcon::On);


    // Create Actions
    QAction* action0 = new QAction(iconAction0, "Action0", this);
    action0->setCheckable(true);

    QAction* action1 = new QAction(iconAction1, "Action1", this);
    action1->setCheckable(true);

    // Store actions in list
    m_actionList.append(action0);
    m_actionList.append(action1);

    // Add actions to toolabar
    QToolBar* toolBar = addToolBar("Actions");
    toolBar->addAction(action0);
    toolBar->addAction(action1);


    // Connect ComboBox
    connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(onWizardStepChanged(int)));

    // Set action0 as default
    m_lastWizardStep = 0;
    action0->setChecked(true);
    combo->setCurrentIndex(m_lastWizardStep); // combo->currentIndex = 0 by default
}

void MainWindow::onWizardStepChanged(int wizardStep)
{
    if(m_lastWizardStep != wizardStep)
    {
        m_actionList.at(m_lastWizardStep)->setChecked(false);
        m_actionList.at(wizardStep)->setChecked(true);
        m_lastWizardStep = wizardStep;
    }
}


MainWindow::~MainWindow(){}
Miki
  • 40,887
  • 13
  • 123
  • 202
0

You can use QStateMachine for binding yours property. As documentation say QAction class has icon property.

So you need to create two QState, binding neccessary properties, add these states in your QStateMachine and start it.

Some examples of using State Machine Framework

t3ft3l--i
  • 1,372
  • 1
  • 14
  • 21