Saturday, June 8, 2013

How to create Custom push button in QT5

To be honest this is not my work its an exact copy from this link.
http://mcmtechtips.blogspot.com/2012/03/shape-qpushbutton-in-your-own-style-qt.html

The only thing that i changed is the pictures, i created three png files

and included this image in the resource file, and the out put is




//custombutton.h
#ifndef CUSTOMBUTTON_H
#define CUSTOMBUTTON_H



#include <QObject>
#include <QPushButton>
#include <QPixmap>
#include <QBitmap>
 
class custombutton: public QPushButton

{
    Q_OBJECT
public:

    explicit custombutton(QObject *parent = 0);

private:
    const QString name;
    QSize imagesize;

};
 

#endif // CUSTOMBUTTON_H

//custombutton.cpp 
#include "custombutton.h"
 
custombutton::custombutton(QObject *parent):
    QPushButton((QPushButton*)parent),
    name("://image/button1.png")
{
    QPixmap pixmap(name);
    imagesize = pixmap.size();
    this->setFixedSize(imagesize);
    this->setMask(pixmap.mask());
} 
 
//dialog.h
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "custombutton.h"


namespace Ui {
class Dialog;
}


class Dialog : public QDialog
{
    Q_OBJECT
    
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    
private:
    Ui::Dialog *ui;
     custombutton *bt;
};


#endif // DIALOG_H 
 
//dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"


Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    bt = new custombutton(this);
    bt->setStyleSheet("QPushButton{border-image: url(://image/button1.png);}\n"
                      "QPushButton:hover{border-image: url(://image/button1_mhover.png);}\n"
                      "QPushButton:pressed{border-image: url(://image/button1_pressed1.png);}");
    bt->setText("ok");
}


Dialog::~Dialog()
{
    delete ui;
}
 
//main.cpp
#include "dialog.h"
#include <QApplication>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    
    return a.exec();
}  

No comments:

Post a Comment

How to create Custom push button in QT5

To be honest this is not my work its an exact copy from this link. http://mcmtechtips.blogspot.com/2012/03/shape-qpushbutton-in-your-own-st...