PDA

View Full Version : Custom Widget - clicked() signal



ak
11th November 2006, 02:26
What shoud i do to create clicked() signal in my custom widget?

munna
11th November 2006, 04:06
You can have a boolean variable which will become true in mousePressEvent. In mouseReleaseEvent you can emit the signal clicked() if the boolean is true and then reset it again for the next click.

sarode
12th November 2006, 12:22
Hello.

Here is the sample code to do that..




class Button : public QWidget
{
Q_OBJECT

public:
Button(....);

protected:
void mousePressEvent(QMouseEvent*);
void mouseReleaseEvent(QMouseEvent*);
void paintEvent(QPaintEvent*);

private:
bool state;

signals:
void clicked();
void release();
void pressed();
};

Button::Button(............)
{
state = 0;
}

void Button::mousePressEvent(....)
{
state = 1;
repaint();
emit pressed();
}

void Button::mouseReleaseEvent(....)
{
state = 0;
repaint();
emit released();
emit clicked();
}

void Button::paintEvent(....)
{
if(state)
...............
else
.............;
}

.

Based on the state value repaint the button as you need and call the signal with any object like this




Button *b = new Button(0);
connect(b, SIGNAL(clicked()), qApp, SLOT(quit()));


You can try this options here

Cheers

wysota
13th November 2006, 08:35
void Button::mouseReleaseEvent(....)
{
state = 0;
repaint();
emit released();
emit clicked();
}.

Based on the state value repaint the button as you need

It's better to update() than to repaint(), repaint() calls are not merged which may slow down the application a bit (or much, depending on how complex is the painting routine).