2014-07-04 7 views
1

간단한 사각형 인 간단한 QWidget을 만들고 싶습니다. 주된 문제는 페인트 이벤트가 같은 장소에서 페인트가 실제로 반대 효과를 낼 때마다 그 페인트가 색상을 강하게 만든다는 것입니다. 이 기능을 달성 할 수있는 방법이 있습니까? 어쩌면 간단한 예제를 제공해 주시겠습니까?QT C++에서 위젯이 사라짐

내 코드 : `

#ifndef WIDGET_H 
#define WIDGET_H 

#include <QWidget> 
#include <QTimer> 

namespace Ui { 
class Widget; 
} 

class Widget : public QWidget 
{ 
    Q_OBJECT 

public: 
    explicit Widget(QWidget *parent = 0); 
    void paintEvent(QPaintEvent*); 
    ~Widget(); 

private: 
    Ui::Widget *ui; 
    int alfa; 
    int i; 
    QTimer time; 

}; 

#endif // WIDGET_H 

`

과 CPP :

#include "widget.h" 
#include "ui_widget.h" 
#include <QColor> 
#include <QPainter> 
#include <QBrush> 
#include <QTimer> 
#include <QDebug> 

Widget::Widget(QWidget *parent) : 
    QWidget(parent,Qt::Popup | Qt::FramelessWindowHint), 
    ui(new Ui::Widget), 
    time() 
{ 
    ui->setupUi(this); 
    setAttribute(Qt::WA_NoSystemBackground, true); 
    setAttribute(Qt::WA_TranslucentBackground, true); 
    setAttribute(Qt::WA_PaintOnScreen); 
    time.setInterval(500); 
    time.start(); 
    connect(&time,SIGNAL(timeout()),this,SLOT(update())); 
    alfa=100; 
    i=0; 

} 

void Widget::paintEvent(QPaintEvent *) 
{ 
    QPainter painter(this); 

    int rectBase = height(); 

    QColor c(255,0,255); 
    alfa=alfa-(i*10); 
    c.setAlpha(alfa); 
    qDebug()<<c.alpha(); 
    i++; 

    painter.setBrush(QBrush(c)); 
    painter.drawRect(0, 0, width(),height()); 


} 

Widget::~Widget() 
{ 
    delete ui; 
} 

답변

2

당신은, 당신의 알파 레벨을 변경 QWidget::paintEvent()에 의존해서는 안 (여러 update() 전화는 당신이 그것을 기대하지 않을 때 하나의 paintEvent()paintEvent() 호출 할 수 있습니다 될 수 있습니다).

결과를 얻는 더 믿을만한 방법은 알파 레벨을 낮추는 별도의 슬롯이 있고 update()을 호출하는 것입니다. 클래스 정의는 다음과 같습니다

class Widget : public QWidget 
{ 
    Q_OBJECT 
public: 
    Widget(QWidget * inParent); 

private: 
    void paintEvent(QPaintEvent *); 

private slots: 
    void animate(); 

private: 
    QTimer * mTimer; 
    int mAlpha; 
}; 

그리고 선언 : 나는 타이머의 간격을 감소했다

Widget::Widget(QWidget * inParent) 
    : 
     QWidget(inParent), 
     mTimer(new QTimer(this)), 
     mAlpha(255) 
{ 
    setAttribute(Qt::WA_NoSystemBackground, true); 
    setAttribute(Qt::WA_TranslucentBackground, true); 
    setAttribute(Qt::WA_PaintOnScreen); 
    mTimer->setInterval(40); 
    mTimer->setSingleShot(false); 
    connect(mTimer, SIGNAL(timeout()), this, SLOT(animate())); 
    mTimer->start(); 
} 

void 
Widget::paintEvent(QPaintEvent *) 
{ 
    QPainter painter(this); 
    painter.setBrush(QColor(255,0,255,mAlpha)); 
    painter.drawRect(rect()); 
} 

void 
Widget::animate() 
{ 
    if (mAlpha > 0) 
    { 
     mAlpha -= 3; 
    } 
    else 
    { 
     mTimer->stop(); 
    } 
    update(); 
} 

공지 사항. 매분 30 초마다 update()이라고 부릅니다. 일반적으로 부드러운 애니메이션이 아닙니다.

+0

setAttribute (Qt :: WA_PaintOnScreen)에 문제가 있습니다. 그것은 오래된 Fedora가 필요하고 새로운 Ubuntu에 어떤 제안도 할 수없는 것처럼 보입니다. – MarcinG

+0

@MarcinG Linux에서 Qt 사용 경험이 없습니다. 죄송합니다 ... – PrisonMonkeys

+0

나는'update()'문제를 완전히 글로 썼다. 매우주의 깊다. :-) – HostileFork

1

내가 Qt5에서, 쿠분투 리눅스에이 코드 경고를 얻을 :

QWidget :: paintEngine : sh 그래서, 당신은 setAttribute(Qt::WA_PaintOnScreen);을 제거하여 중지 경고를 얻을 수

https://qt.gitorious.org/qt/qtbase-harmattan/commit/3037525

: 울드는 더 이상 src/widgets/kernel/qwidget_qpa.cpp에,

발신 라인 qWarning("QWidget::paintEngine: Should no longer be called");에 전화하지 않으며,이 티켓에 조금 논의 내가 그거 했어. 그 라인을 꺼낸 후에, 그것은 제게는 효과가 있습니다 - 당신의 뺄셈 모델은 이상합니다. 알파를 수정하고 각 반복에서 뺄셈 값을 변경합니다. 당신은 아마 둘 다 의도하지 않았습니다. 그러니로 변경 :

QColor c (255, 0, 255); 
alfa = alfa - 10; 
if (alfa >= 0) { 
    c.setAlpha(alfa); 
} else { 
    time.stop(); 
} 

... 나 :

QColor c(255,0,255); 
if (alfa - i * 10 >= 0) { 
    c.setAlpha(alfa - i * 10); 
    i++; 
} else { 
    time.stop(); 
} 

이 (@PrisonMonkeys이 타이머 반드시 update() 통화의 유일한 원천되지 않는에 유의을 참조하십시오.)에 대해서 이 경고가 더 강렬 해져서 놓치지 않으려면 The Essential Noisy Debug Hook For Qt을보아야합니다.이 부분을 업데이트해야합니다.

변경 사항이있는 경우 알파 블렌딩 창이 플랫폼에서 전혀 작동하지 않는다면 나를 위해 작동하는 상황을 명시해야합니다. 이 작거나 당신이 원하는 것보다 더 호출 할 수 있기 때문에

+0

WA_PaintOnScreen 속성을 그대로 둘 수있는 방법은 무엇입니까? Fedora에서 응용 프로그램을 테스트 할 때 필자가 필요하다고 확신합니다.설정되지 않은 경우 배경으로 검정 사각형을 가져옵니다. – MarcinG

+0

@MarcinG 당신이 리눅스/페도라에 있고 그것이 작동하지 않는다고 말하는 것에 흥미가 있습니다. Qt5와 Qt Creator (https://en.wikipedia.org/wiki/Qt_Creator)를 사용하지 않을 정당한 이유가 없다면 쿠분투에 대한 나의 성공을 고려해 볼 때 이것을 제안 할 수 있습니다. – HostileFork