Results 1 to 5 of 5

Thread: The Creation of Balloon Message Problem

  1. #1
    Join Date
    Dec 2009
    Posts
    15
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default 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 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:aintEvent(QPaintEvent *)
    {
    QPainter p(this);
    p.drawPath(mBalloonPath);

    }
    I've attached the view of JBalloonTip.

    jballoontip.png

    Thanks advance for your helps and clarifications,
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: The Creation of Balloon Message Problem

    Reimplement sizeHint() to provide a reasonable default size for the widget (baloon) and to set the correct size policy with setSizePolicy().
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Dec 2009
    Posts
    15
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: The Creation of Balloon Message Problem

    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?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: The Creation of Balloon Message Problem

    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.

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: The Creation of Balloon Message Problem

    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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 3
    Last Post: 18th July 2010, 13:53
  2. Table Creation Problem.
    By damodharan in forum Qt Programming
    Replies: 4
    Last Post: 3rd June 2010, 12:11
  3. balloon message in Qt
    By wagmare in forum Qt Programming
    Replies: 5
    Last Post: 26th March 2009, 16:37
  4. how can I make a balloon to wrap the editable text?
    By learning_qt in forum Qt Programming
    Replies: 9
    Last Post: 20th January 2009, 15:09
  5. QSystemTrayIcon: Overridden balloon msg
    By further in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2008, 10:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.