PDA

View Full Version : The Creation of Balloon Message Problem



yaseminyilmaz
30th January 2013, 12:15
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 100

class JBalloonTip : public QWidget
{
Q_OBJECT

public:
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);

}


I've attached the view of JBalloonTip.

8656

Thanks advance for your helps and clarifications,

Santosh Reddy
30th January 2013, 14:02
Reimplement sizeHint() to provide a reasonable default size for the widget (baloon) and to set the correct size policy with setSizePolicy().

yaseminyilmaz
30th January 2013, 14:21
Thanks a lot for your helps. I've reimplemented sizeHint function. Now, the size of JBalloonTip is correct but it's shape is the rectangle. It is not shown balloon shape. It seems like the call of QPainter::drawPath is ineffective on paintEvent function?

Is there anyone created a widget shaped like attached image, is it a doable work with Qt?

d_stranz
30th January 2013, 14:53
Is there anyone created a widget shaped like attached image, is it a doable work with Qt?

What attached image? From your original post?

As far as I can tell, the code you have implemented to create the path will give you exactly the shape you see on screen. (In other words, a "cartoon bubble" like your screenshot).

If you want it to look like something else, draw it on a piece of graph paper, calculate the relative coordinates corresponding to the vertices of that shape, and implement that path. There is nothing wrong with QPainter::drawPath(). The problem is your code that creates the path.

Santosh Reddy
30th January 2013, 14:58
Now, the size of JBalloonTip is correct but it's shape is the rectangle. It is not shown balloon shape.
It is rectangle because the QPainterPath contais path to draw a rectangle. You need to correct the QPainterPath (mBalloonPath)


Is there anyone created a widget shaped like attached image, is it a doable work with Qt?
Yes it is very much do-able.