The Creation of Balloon Message Problem
-
Hi all,
I want to make a class of balloon message. I've added the code below. But both the position of balloon within the widget and the size of widget is wrong. The shape of JBalloonTip is always rectangle not a balloon; Where does the problem arise from?
@#define JBALLOON_WIDTH 200
#define JBALLOON_HEIGHT 100class JBalloonTip : public QWidget
{
Q_OBJECTpublic:
JBalloonTip();
~JBalloonTip();void showBalloon(QPoint globalPos, QPoint localPos);
protected:
void timerEvent(QTimerEvent *e);
void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *);
private:
QPainterPath mBalloonPath;
};@@
JBalloonTip::JBalloonTip() :
QWidget(0, Qt::ToolTip | Qt::FramelessWindowHint)
{
}JBalloonTip::~JBalloonTip()
{
}void JBalloonTip::timerEvent(QTimerEvent *e)
{
int timerId = e->timerId();
killTimer(timerId);
close();QWidget::timerEvent(e);
}
void JBalloonTip::showBalloon(QPoint globalPos, QPoint localPos)
{
int leftWidth = (JBALLOON_WIDTH / 5);
int xTopLeft = globalPos.x() - leftWidth;
int yTopLeft = globalPos.y() - JBALLOON_HEIGHT;
int arrowWidth = (leftWidth / 2);
int rightWidth = JBALLOON_WIDTH - (leftWidth + arrowWidth);QPoint topLeftPoint(xTopLeft, yTopLeft); QPoint nextPoint = globalPos; mBalloonPath.moveTo(globalPos); // STEP 1 nextPoint.rx() += arrowWidth; nextPoint.ry() -= arrowWidth; mBalloonPath.lineTo(nextPoint); // STEP 2 nextPoint.rx() += rightWidth; mBalloonPath.lineTo(nextPoint); // STEP 3. int leftHeight = JBALLOON_HEIGHT - arrowWidth; nextPoint.ry() -= leftHeight; mBalloonPath.lineTo(nextPoint); // STEP 4. nextPoint = topLeftPoint; mBalloonPath.lineTo(nextPoint); // STEP 5. nextPoint.ry() += leftHeight; mBalloonPath.lineTo(nextPoint); // STEP 6. nextPoint.rx() += leftWidth; mBalloonPath.lineTo(nextPoint); // STEP 7. nextPoint.setY(globalPos.y()); mBalloonPath.lineTo(nextPoint); mBalloonPath.closeSubpath(); this->move(topLeftPoint); startTimer(1000); this->show();
}
void JBalloonTip::resizeEvent(QResizeEvent *)
{
// Set the mask
QBitmap bitmap = QBitmap(sizeHint());
bitmap.fill(Qt::color0);
QPainter painter1(&bitmap);
painter1.setPen(QPen(Qt::color1, 1));
painter1.setBrush(QBrush(Qt::color1));
painter1.drawPath(mBalloonPath);this->setMask(bitmap);
}
void JBalloonTip::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawPath(mBalloonPath);}
@Thanks advance for your helps and clarifications,