A complete example of a button that toggles between two icons in Qt 5.
#include "dialog.h" | |
Dialog::Dialog(QWidget *parent): QDialog(parent) | |
{ | |
QVBoxLayout *mainLayout = new QVBoxLayout(this); | |
iconBtn = new QPushButton(QIcon(":/icon1.png"), "", this); | |
icon = 0; | |
mainLayout->addWidget(iconBtn); | |
connect(iconBtn, SIGNAL(clicked()), this, SLOT(toggleIcon())); | |
setLayout(mainLayout); | |
} | |
void Dialog::toggleIcon() | |
{ | |
icon = (icon) ? 0 : 1; | |
QString fname = QString(":/icon%1.png").arg(icon+1); | |
iconBtn->setIcon(QIcon(fname)); //This will set the size automatically | |
} | |
Dialog::~Dialog() | |
{ | |
delete iconBtn; | |
} |
#------------------------------------------------- | |
# | |
# Project created by QtCreator 2013-07-13T19:42:21 | |
# | |
#------------------------------------------------- | |
QT += core gui | |
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | |
TARGET = TestSO | |
TEMPLATE = app | |
SOURCES += main.cpp\ | |
dialog.cpp | |
HEADERS += dialog.h | |
RESOURCES += \ | |
res.qrc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment