Qt - Get QPushButton icon name

大憨熊 提交于 2019-12-24 10:49:10

问题


I have a two state QPushButton. I want to associate an icon to each state.

It is like Play|Pause buttons in music players.

To do so, I would like to get the current icon name in order to know what the next icon to set will be.

I could subclass QPushButton but is it worth it?


回答1:


Use QPushButton::icon() and QIcon::name() to get the icon name.




回答2:


Instead of setting an icon based on the QPushButton's state, set one QIcon that has two states, Qt will select the correct icon if you use it with a checkable QPushButton.

QIcon icon = QIcon();
// 'Off' state corresponds to unchecked state of QPushButton
icon.addPixmap( QPixmap( ":/img/play.png" ), QIcon::Normal, QIcon::Off );
// 'On' state corresponds to checked state of QPushButton
icon.addPixmap( QPixmap( ":/img/pause.png" ), QIcon::Normal, QIcon::On );
QPushButton * button = new QPushButton();
button->setIcon( icon );
button->setCheckable( true );


来源:https://stackoverflow.com/questions/15062776/qt-get-qpushbutton-icon-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!