Alan
Alan

Reputation: 1265

Passing a standard enum value

I'm coding a toast-style messagebox, but I haven't been able to directly pass the standard icons. The switch below works, but is so clunky. What's the proper way to do this so I can get rid of the switch selection?

void MainWindow::mtoast(int msgtime,int level, QString msg)
{
    QMessageBox *mbox = new QMessageBox;
    mbox->setStandardButtons(0);
    mbox->setText(msg);

    switch(level){
        case 0:
            mbox->setIcon(QMessageBox::NoIcon);
            break;
        case 1:
            mbox->setIcon(QMessageBox::Question);
            break;
        case 2:
            mbox->setIcon(QMessageBox::Information);
            break;
        case 3:
            mbox->setIcon(QMessageBox::Warning);
            break;
        case 4:
            mbox->setIcon(QMessageBox::Critical);
            break;
    }


    mbox->setWindowFlags ( Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    mbox->show();
    QTimer::singleShot(msgtime, mbox, SLOT(hide()));

}

Upvotes: 1

Views: 167

Answers (1)

Vada Poché
Vada Poché

Reputation: 780

Change your function signature so that it accepts a reference to enum QMessageBox::Icon as the second parameter instead of int level. Something like this:

void MainWindow::mtoast(int msgtime, const enum QMessageBox::Icon& icon, QString msg)
{
    QMessageBox *mbox = new QMessageBox;
    mbox->setStandardButtons(0);
    mbox->setText(msg);
    mbox->setIcon(icon); //this statement replaces the entire switch
    mbox->setWindowFlags ( Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    mbox->show();
    QTimer::singleShot(msgtime, mbox, SLOT(hide()));

}

Upvotes: 1

Related Questions