Jiawei Lu
Jiawei Lu

Reputation: 577

Show the status of icons in toolbar using qt in C++

I would like to show the status of icons in toolbar (whether they are activated). For example, When I click Bold, Italic or Underline icon in Microsoft word, it will be shaded, and switch to normal status when I click it again. It's not necessary to be shaded. I just need to distinguish whether it is activated.

word

Upvotes: 1

Views: 406

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

You have to checkable the QAction, or use a QWidget that is checkable like QToolButton:

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow w;
    QToolBar *toolbar = w.addToolBar("Toolbar");

    QAction *bold_action = toolbar->addAction("B");
    QFont bold_fn(bold_action->font());
    bold_fn.setBold(true);
    bold_action->setFont(bold_fn);
    bold_action->setCheckable(true);

    QAction *italic_action = toolbar->addAction("I");
    QFont fn_cursive(italic_action->font());
    fn_cursive.setItalic(true);
    italic_action->setFont(fn_cursive);
    italic_action->setCheckable(true);

    QAction *underline_action = toolbar->addAction("U");
    QFont fn_underline(underline_action->font());
    fn_underline.setUnderline(true);
    underline_action->setFont(fn_underline);
    underline_action->setCheckable(true);

    QAction* subscript_action = new QAction;
    subscript_action->setIcon(QIcon(":/subscript.png"));
    subscript_action->setCheckable(true); // <---
    toolbar->addAction(subscript_action);

    w.setCentralWidget(new QTextEdit);
    w.resize(320, 240);
    w.show();
    return a.exec();
}

Output:

enter image description here

Upvotes: 1

Related Questions