Fatihul Ulum Community of Knowledge
Would you like to react to this message? Create an account in a few clicks or log in to continue.

painter...............

Go down

painter............... Empty painter...............

Post  zz Mon Aug 17, 2009 4:28 am

Kode ini aku dapat dari www.zetcode.com
dengan sedikit modifikasi , kalau anda mau silahkan pelajari
dibawah ini adalah file puf.h
Code:

#ifndef PUFF_H
#define PUFF_H

#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <iostream>

class Puff : public QWidget
{
  Q_OBJECT 

  public:
    Puff (QWidget *parent = 0);
    ~Puff ()
    {
       std::cout << "succes" <<std::endl;
   }
  protected:
    void paintEvent(QPaintEvent *event);
    void timerEvent(QTimerEvent *event);
    void bunuh ();
  private:
    int x;
    qreal opacity;
    int timerId;

};
#endif
dan ini adalah file puf.cpp
Code:

#include "puf.h"
#include <QApplication>
#include <QPainter>
#include <QPainterPath>
#include <QTimer>
Puff::Puff(QWidget *parent)
    : QWidget(parent)
{
  x = 1;
  opacity = 1.0;
  timerId = startTimer(40);
   
//   oke ();
}

/*void Puff :: oke()
{
  x = 1;
  opacity = 1.0;
  timerId = startTimer(40);
}   
*/
void Puff :: bunuh ()
{
   this->close();
}
void Puff::paintEvent(QPaintEvent *event)
{
  QPainter painter(this);

  QString text = "Testing";
 

  painter.setPen(QPen(QBrush("#575555"), 1));

  QFont font("serif", x, QFont::DemiBold);
  QFontMetrics fm(font);
  int textWidth = fm.width(text);

  painter.setFont(font);

  QLinearGradient grad2(0, 55, 250, 0);

  grad2.setColorAt(0.2, Qt::black);
  grad2.setColorAt(0.5, Qt::red);
  grad2.setColorAt(0.8, Qt::black);

  painter.fillRect(10, 10, 330, 260, grad2);
   
  if (x > 10)
  {
    opacity -= 0.010;
    painter.setOpacity(opacity);
  }

  if (opacity <= 0)
  {
    killTimer(timerId);
   bunuh ();
  }

  int h = height();
  int w = width();

  painter.translate(QPoint(w/2, h/2));
  painter.drawText(-textWidth/2, 0, text);

}

void Puff::timerEvent(QTimerEvent *event)
{
  x += 1;
  repaint();
}
dan yang terahir adalah file main.cpp
Code:

#include "puf.h"
#include <QDesktopWidget>
#include <QApplication>

void tengah(QWidget &widget)
{
  int x, y;
  int screenWidth;
  int screenHeight;
  int WIDTH = 350;
  int HEIGHT = 280;
  QDesktopWidget *desktop = QApplication::desktop();
  screenWidth = desktop->width();
  screenHeight = desktop->height();
  x = (screenWidth - WIDTH) / 2;
  y = (screenHeight - HEIGHT) / 2;
  widget.setGeometry(x, y, WIDTH, HEIGHT);
}


int main(int argc, char *argv[])
{
  QApplication app(argc, argv);

  Puff *window = new Puff;

  window -> setWindowTitle("Test");
  window -> show();
  tengah(*window);

  return app.exec();
  delete window;
}
goodluck
zz
zz
Panglima
Panglima

Jumlah posting : 121
Age : 78
Lokasi : france
Registration date : 13.02.09

Kembali Ke Atas Go down

Kembali Ke Atas

- Similar topics

 
Permissions in this forum:
Anda tidak dapat menjawab topik