Skip to content

Instantly share code, notes, and snippets.

@elpuri
Created December 28, 2010 19:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpuri/757597 to your computer and use it in GitHub Desktop.
Save elpuri/757597 to your computer and use it in GitHub Desktop.
A button component in QML
import Qt 4.7
BorderImage {
id: buttonbase
signal clicked
property int hitboxExtension : 20
property alias text : label.text
property bool pressed: hitbox.pressed && hitbox.containsMouse
source: pressed ? "button_down.png" : "button_up.png"
border.left: 50;
border.right: 50;
MouseArea {
id: hitbox
anchors.fill: parent
anchors.topMargin: -hitboxExtension
anchors.bottomMargin: -hitboxExtension
anchors.leftMargin: -hitboxExtension
anchors.rightMargin: -hitboxExtension
onClicked: { haptics.buttonClick(); parent.clicked() }
onPressedChanged: { if (hitbox.pressed) haptics.buttonClick(); }
}
Text {
id: label
anchors.centerIn: parent
anchors.verticalCenterOffset: pressed ? -2 : -3
font.family: "Lato Black"
font.pixelSize: 26
color: "#ffffff"
styleColor: "#4f000000"
style: Text.Sunken
}
}
import Qt 4.7
BorderImage {
id: buttonbase
signal clicked
property bool pressed: hitbox.pressed && hitbox.containsMouse
source: pressed ? "button_down.png" : "button_up.png"
border.left: 50;
border.right: 50;
MouseArea {
id: hitbox
anchors.fill: parent
onClicked: parent.clicked()
}
}
import Qt 4.7
BorderImage {
id: buttonbase
signal clicked
property alias text : label.text
property bool pressed: hitbox.pressed && hitbox.containsMouse
source: pressed ? "button_down.png" : "button_up.png"
border.left: 50;
border.right: 50;
MouseArea {
id: hitbox
anchors.fill: parent
onClicked: parent.clicked()
}
Text {
id: label
anchors.centerIn: parent
anchors.verticalCenterOffset: pressed ? -2 : -3
font.family: "Lato Black"
font.pixelSize: 26
color: "#ffffff"
styleColor: "#4f000000"
style: Text.Sunken
}
}
#include "haptics.h"
#ifdef Q_OS_SYMBIAN
#include <QFeedbackEffect>
QTM_USE_NAMESPACE
#endif
Haptics::Haptics(QObject *parent) :
QObject(parent)
{
}
void Haptics::buttonClick() {
#ifdef Q_OS_SYMBIAN
QFeedbackEffect::playThemeEffect(QFeedbackEffect::ThemeBasicButton);
#endif
}
#ifndef HAPTICS_H
#define HAPTICS_H
#include <QObject>
class Haptics : public QObject
{
Q_OBJECT
public:
explicit Haptics(QObject *parent = 0);
signals:
public slots:
void buttonClick();
};
#endif // HAPTICS_H
property int hitboxExtension : 10
// Yadda yadda
MouseArea {
id: hitbox
anchors.fill: parent
anchors.topMargin: -hitboxExtension
anchors.bottomMargin: -hitboxExtension
anchors.leftMargin: -hitboxExtension
anchors.rightMargin: -hitboxExtension
Haptics haptics;
QDeclarativeView w;
QDeclarativeEngine* engine = w.engine();
engine->rootContext()->setContextProperty("haptics", &haptics);
import Qt 4.7
Rectangle {
width: 360
height: 640
color: "#444444"
Button {
id: selectButton
anchors.left: parent.left
anchors.leftMargin: 30
anchors.right: parent.right
anchors.rightMargin: 30
anchors.bottom: quitButton.top
anchors.bottomMargin: 20
text: "Select person"
onClicked: { proxyItem.sourceItem = picker; proxyItem.grabImage(); }
}
Button {
id: quitButton
anchors.left: parent.left
anchors.leftMargin: 30
anchors.right: parent.right
anchors.rightMargin: 30
anchors.bottom: parent.bottom
anchors.bottomMargin: 30
text: "Exit"
onClicked: Qt.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment