PDA

View Full Version : How to find in which cell mouse press event occured in QGridLayout



LiCodeX
24th October 2007, 18:25
Hi,

I've a QGridLayout consisting of QLabel widgets in it. I want to know how to find out in which cell particularly the mouse Press even occured, as this is necessary for me to know which label widget has been clicked.
Any suggestions, help will be appreciated

Thanks and Regards

marcel
24th October 2007, 18:34
Layouts are strictly for arrangement. You must not use them for user interaction. Create you own label class instead:


//header
class MyLabel : public QLabel
{
Q_OBJECT

public:
MyLabel(const QString & text, QWidget * parent = 0, Qt::WindowFlags f = 0);
~MyLabel();

signals:
void clicked();
protected:
void mousePressEvent(QMouseEvent*);
}

//cpp
MyLabel::MyLabel(const QString & text, QWidget * parent = 0, Qt::WindowFlags f = 0)
:QLabel(text, parent, f)
{
}

MyLabel::~MyLabel()
{
}

void MyLabel::mousePressEvent(QMouseEvent*)
{
emit clicked();
}



All you have to do is to connect the clicked signal of each label to some slot(s).