PDA

View Full Version : ToolTip....



merry
1st August 2007, 06:53
Hi all

Working on Qt4.2 for MAC

Can any body tells me that , How can The tooltip for the button should be shown in some Image say "balloon".



Thanx

merry
1st August 2007, 07:06
Hi....:)

I mean How can I display the tooltip for the QPushbutton in an image

image can be of anytype

1. Circle
2. balloon
3. triangle etc .....


Thanx

marcel
1st August 2007, 07:50
So you want to display an image with a QToolTip.
Should be easy enough if you subclass QToolTip and override its paintEvent.
The image, for example the triangle should contain the triangle in a specific color and the rest has to be a color different from the one the triangle has.
This is to be able to create a heuristic mask (make the image transparent around the triangle ).


QPixmap tipPix;
//.. load the pixmap
tipPix.setMask(tipPix.createHeuristicMask());


Another solution is to use a transparent png.

Regards

merry
1st August 2007, 08:00
Thanx 4 the sol.

But I dont want to display an image with the tooltip . I want to display toolTip (Text) in the balloon image....

for eg.

If I set the tooltip for the button ... It is drawn immediately below the given position in a distinctive black-on-yellow color combination in a rectangle, right , Now Instead of that rectangle I want to use balloon.

Thanx

marcel
1st August 2007, 08:18
Yes, for that you will have to use custom drawing.
Tool tips are platform dependent. So on windows they actually look like a balloon.

If you want them to look like that on Mac, then you have to paint them yourself. Either by using images, or drawing primitives, whatever.
Since you are on a Mac, you could take advantage of the desktop composition features and add some need effects to it, like transparent gradations, etc.

Regards

merry
1st August 2007, 08:21
THANX 4 the soln.

But I dont know how to do custom drawing...

Can u Explain me it with example

thanx

merry
1st August 2007, 08:45
Ok I 'll draw it using images and drawing primitives ..
but how can i set it for the tooltip , so that tooltip should be displyed in that balloon.

marcel
1st August 2007, 08:48
Custom drawing is not that hard in general, but this tooltip problem will be pretty hard to solve.

First of all, you have to subclass the event() function for the widget you want the tip to appear.
In the event function you will look for a QHelpEvent, whose type is QEvent::ToolTip. This means that the widget is requested to show a tool tip.

The second step is to subclass QToolTip and override the paintEvent. In paintEvent you do all the drawing.



bool SomwWidget::event(QEvent* e)
{
QHelpEvent *he = static_cast<QHelpEvent*>(e);
if(he && he->type() == QEvent::ToolTip)
{
if(!mCustomToolTip)
mCustomToolTip = new CustomToolTip(this);
mCustomToolTip->show();
e->accept();
return true;
}
else
{
if(mCustomToolTip)
mCustomToolTip->hide();
}
return QWidget::event(e);
}

mCustomToolTip should be a member of your widget's class, which you initialize to NULL in the constructor.

For the paintEvent:


void CustomToolTip::paintEvent()
{
QPainter p(this);
p.fillRect(rect(), Qt::transparent );
p.setBrush(Qt::red);
p.drawEllipse(rect());
}



This draws a red ellipse on a transparent background.
It is just an example. You could customize the drawing further.

Regards

merry
1st August 2007, 08:53
Thanx 4 the solution

Ok I ' ll try the solution.....

merry
1st August 2007, 12:02
Hi Marcel


mCustomToolTip should be a member of your widget's class, which you initialize to NULL in the constructor.

Pls Explain the above line...

Is it the member of the Subclass QToolTip.

Thanx

marcel
1st August 2007, 12:08
No.
Let's assume that you want the custom tooltip be displayed for a button.
Then you need to subclass QPushButton and override its event() function, where you put the code I've shown you earlier.
The custom tooltip should be a member of this subclass.

BTW, for what widget(s) you want a custom tooltip to be displayed?

Regards

merry
1st August 2007, 12:47
Hi...


if(!mCustomToolTip)
mCustomToolTip = new CustomToolTip(this);
mCustomToolTip->show();

but in the above code u sent to me , Custom ToolTip is the SubClass Of QToolTip ..

but now u said that the custom tooltip should be a member of QPushbutton subclass.

Pls once again u view the code u sent to me and Explain it to me

Thanx

marcel
1st August 2007, 13:08
merry, merry, merry....
First tell me for what widget you want your custom tooltip.

Regards

merry
1st August 2007, 13:10
QPushButton

marcel
1st August 2007, 13:14
OK. So in order to display a custom tooltip for your push button, you have to intercept the QHelpEvent I mentioned earlier. For this you have to subclass QPushButton and override event().

To display a custom tooltip you first need to create a custom tooltip.
After you create it( create a class for it as I have explained ), add it as a member in your custom push button.

Regards

merry
1st August 2007, 13:44
Marcel

I m doing like that only u said but when I Subclass QToolTip I got errors shown below :



moc_CustomToolTip.cpp:37: error: 'staticMetaObject' is not a member of 'QToolTip'
moc_CustomToolTip.cpp: In member function 'virtual void* CustomToolTip::qt_metacast(const char*)':
moc_CustomToolTip.cpp:53: error: 'qt_metacast' is not a member of 'QToolTip'
moc_CustomToolTip.cpp: In member function 'virtual int CustomToolTip::qt_metacall(QMetaObject::Call, int, void**)':
moc_CustomToolTip.cpp:58: error: 'qt_metacall' is not a member of 'QToolTip'


Thanx

marcel
1st August 2007, 13:53
Can I see the code?

merry
1st August 2007, 14:05
Yaa Sure...

QButton.cpp //Subclass of QPushButton


#include "QButton.h"
#include <QEvent>
#include <QHelpEvent>


MyToolTipButton::MyToolTipButton(QWidget *parent ): QPushButton(parent)
{

MyToolTipButton1 = new QPushButton();
MyToolTipButton1->setText("abc");
}



bool MyToolTipButton::event(QEvent* e)
{
QHelpEvent *he = static_cast<QHelpEvent*>(e);
if(he && he->type() == QEvent::ToolTip)
{
if(!mCustomToolTip)
//mCustomToolTip = new CustomToolTip();
MyToolTipButton1->show();
e->accept();
return true;
}
else
{
if(mCustomToolTip)
MyToolTipButton1->hide();
}

return QWidget::event(e);

}


QButton.h


#ifndef QBUTTON_H
#define QBUTTON_H

#include <QPushButton>
#include <QEvent>
#include "CustomToolTip.h"

class CustomToolTip;
class MyToolTipButton : public QPushButton
{
Q_OBJECT
public:
MyToolTipButton(QWidget *parent =0);

public:
QPushButton *MyToolTipButton1;

protected:
bool event(QEvent* e);


public:
CustomToolTip *mCustomToolTip;

};


#endif

CustomToolTip.h


#ifndef CustomToolTip_H
#define CustomToolTip_H
#include <QToolTip>

class CustomToolTip : public QToolTip
{
Q_OBJECT
public:
CustomToolTip();
public:
void paintEvent() ;

};

#endif


CustomToolTip.cpp


#include "CustomToolTip.h"
#include <QPainter>

void CustomToolTip::paintEvent()
{
QPainter p(this);
p.fillRect(rect(), Qt::transparent );
p.setBrush(Qt::red);
p.drawEllipse(rect());
}

and also there is an error
CustomToolTip.cpp:15: error: no matching function for call to 'QPainter::QPainter(CustomToolTip* const)'

marcel
1st August 2007, 14:13
You have a lot of errors there!
Most important:
1. Remove the QPushButton member from the QPushButton subclass.
2. paintEvent is virtual protected. Define it as such.
Also, it has a QPaintEvent* parameter.

Regards

merry
1st August 2007, 14:24
After doing all the things u said , I Still got these three errors


moc_CustomToolTip.cpp:37: error: 'staticMetaObject' is not a member of 'QToolTip'
moc_CustomToolTip.cpp: In member function 'virtual void* CustomToolTip::qt_metacast(const char*)':
moc_CustomToolTip.cpp:53: error: 'qt_metacast' is not a member of 'QToolTip'
moc_CustomToolTip.cpp: In member function 'virtual int CustomToolTip::qt_metacall(QMetaObject::Call, int, void**)':
moc_CustomToolTip.cpp:58: error: 'qt_metacall' is not a member of 'QToolTip'

fullmetalcoder
1st August 2007, 14:26
QToolTip is not a QObject... remove the Q_OBJECT macro...:rolleyes:

marcel
1st August 2007, 14:29
In this case change the inheritance from QToolTip to QWidget.
And in the constructor of CustomToolTip:


setWindowFlags(Qt::ToolTip);


Regards

merry
2nd August 2007, 08:17
Hi Marcel


I had done all that what u said , It gives no error but ,only showing a Main window with a button and toolTip of that button in a rectangle but not showing the ToolTip in an elliplse.

I debug the code , And on debugging I found that Its not entering in this function


if(he && he->type() == QEvent::ToolTip)
{
if(!mCustomToolTip)
mCustomToolTip = new CustomToolTip(this);
mCustomToolTip->show();
e->accept();
return true;
}



Pls help What can I do now.

Thanx

merry
2nd August 2007, 10:47
Pls...Pls............help

vermarajeev
3rd August 2007, 07:21
Pls...Pls............help

Firstly, in the CustomToolTip class do this

CustomToolTip::CustomToolTip( QWidget * parent )
:QWidget(parent)
{
setWindowFlags(Qt::ToolTip);
resize( 50, 50 );
}

Keep the mouse cursor stable on the PushButton and then move, a red ecllipse will appear on top left corner.

Works fine for me.

merry
3rd August 2007, 07:36
Thanx Mr Rajeev For helping me ..

I do the same as u said but still not showing tooltip..


Pls review my code once , and Also tells me Is am doin somthin Wrong...


CustomToolTip.cpp


#include "CustomToolTip.h"
#include <QPainter>

CustomToolTip::CustomToolTip( QWidget * parent )
:QWidget(parent)
{
setWindowFlags(Qt::ToolTip);
resize(50,50);
}


void CustomToolTip::paintEvent(QPaintEvent *e)
{
QPainter p(this);
p.fillRect(rect(), Qt::transparent );
p.setBrush(Qt::red);
p.drawEllipse(rect());
}



QButton.cpp




#include "QButton.h"
#include <QEvent>
#include <QHelpEvent>

MyToolTipButton::MyToolTipButton(QWidget *parent=0)
{
setMouseTracking(true);
FileRecovery=new QPushButton(0);
RawRecovery=new QPushButton(0);
FileRecovery->setFocusPolicy(Qt::StrongFocus);
}

bool MyToolTipButton::event(QEvent* e)
{
QHelpEvent *he = static_cast<QHelpEvent*>(e);
if(he && he->type() == QEvent::ToolTip)
{
if(!mCustomToolTip)
mCustomToolTip = new CustomToolTip(0);
mCustomToolTip->show();
e->accept();
return true;
}
else
{
if(mCustomToolTip)
mCustomToolTip->hide();
}
return QWidget::event(e);
}



MainWindow.cpp


#include "MainWindow.h"
#include "QButton.h"
#include <QtGui>


MainWindow::MainWindow()
{
QWidget *centralWidget = new QWidget;
button=new MyToolTipButton(0);
centralWidget->setFixedSize(500,300);
button->FileRecovery = new QPushButton();
button->FileRecovery->setGeometry(20,20,121,121);
button->FileRecovery->setFixedSize(121,121);
button->FileRecovery->setToolTip("Hello");
button->FileRecovery->setIcon(QIcon("/design/images/open drive.png"));
button->FileRecovery->setIconSize(QSize(64,64));
button->RawRecovery = new QPushButton();
button->RawRecovery->setGeometry(140,20,121,121);
button->RawRecovery->setFixedSize(121,121);
button->RawRecovery->setIcon(QIcon("/design/images/save& repair -6 copy.png"));
button->RawRecovery->setIconSize(QSize(64,64));
clipboard = QApplication::clipboard();
QHBoxLayout *controlsLayout = new QHBoxLayout;
controlsLayout->addStretch(1);
QHBoxLayout *lineLayout = new QHBoxLayout;
lineLayout->addSpacing(1);
QVBoxLayout *centralLayout = new QVBoxLayout;
centralLayout->addLayout(controlsLayout);
centralLayout->addWidget(button->FileRecovery, 1);
centralLayout->addWidget(button->RawRecovery, 0);
centralLayout->addSpacing(1);
centralLayout->addLayout(lineLayout);
centralWidget->setLayout(centralLayout);
setCentralWidget(centralWidget);
button->FileRecovery->installEventFilter(this);
}


Regards

vermarajeev
3rd August 2007, 07:56
Ok, no time to review.
What you can do is take the below code, cut and paste into a new project and then try.
NOTE: you have to place the mouse cursor on the button stable for a while, then move slightly to see the eclipse.

//.h

class PushButton : public QPushButton
{
Q_OBJECT
public:
PushButton( const QString & text, QWidget * parent = 0 );
~PushButton(){}

protected:
bool event ( QEvent * );
void mouseMoveEvent( QMouseEvent* );

private:
CustomToolTip *mCustomToolTip;
QPoint _mousePoint;
};
//.cpp


PushButton::PushButton( const QString & text, QWidget * parent )
: QPushButton(parent)
{
setText(text);
mCustomToolTip = 0;
setMouseTracking(true);
}

bool PushButton::event( QEvent * e )
{
QHelpEvent *he = static_cast<QHelpEvent*>(e);
if(he && he->type() == QEvent::ToolTip)
{
if(!mCustomToolTip)
mCustomToolTip = new CustomToolTip( this);
mCustomToolTip->show();
e->accept();
return true;
}
else
{
if(mCustomToolTip)
mCustomToolTip->hide();
}
return QWidget::event(e);
}

void PushButton::mouseMoveEvent( QMouseEvent* e)
{
_mousePoint = e->pos() ;
}
//.h

class CustomToolTip : public QWidget
{
Q_OBJECT
public:
CustomToolTip( QWidget * parent = 0 );
~CustomToolTip(){}
protected:
void paintEvent ( QPaintEvent * );
private:

};

//.cpp

CustomToolTip::CustomToolTip( QWidget * parent )
:QWidget(parent)
{
setWindowFlags(Qt::ToolTip);
resize( 50, 50 );
}

void CustomToolTip::paintEvent ( QPaintEvent * )
{
QPainter p(this);
p.fillRect( rect(), Qt::transparent );
p.setBrush(Qt::red);
p.drawEllipse(rect());
}
//main.cpp

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
PushButton* pBtn = new PushButton("OK", &w);
w.resize(400, 500);
pBtn->setGeometry( w.width()/2, w.height()/2, 80, 30 );
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}

merry
3rd August 2007, 10:42
Hi..

I am using 5 buttons on my main window, and for every button it is showing the same tooltip(ellipse) , Is there a way that for every button it shows different tooltip.

Thanx