Created
July 14, 2013 01:23
-
-
Save alexreinking/5992821 to your computer and use it in GitHub Desktop.
A complete example of a button that toggles between two icons in Qt 5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DIALOG_H | |
#define DIALOG_H | |
#include <QtWidgets> | |
#include <QtGui> | |
class Dialog : public QDialog | |
{ | |
Q_OBJECT | |
public: | |
Dialog(QWidget *parent = 0); | |
~Dialog(); | |
public slots: | |
void toggleIcon(); | |
private: | |
QPushButton* iconBtn; | |
int icon; | |
}; | |
#endif // DIALOG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "dialog.h" | |
#include <QApplication> | |
int main(int argc, char *argv[]) | |
{ | |
Q_INIT_RESOURCE(res); //Required for MSVC 2012. | |
QApplication a(argc, argv); | |
Dialog w; | |
w.show(); | |
return a.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RCC> | |
<qresource prefix="/"> | |
<file>icon1.png</file> | |
<file>icon2.png</file> | |
</qresource> | |
</RCC> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------- | |
# | |
# 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