PDA

View Full Version : how to let QWidget::childAt() return only one when the target include many QWidget



silence
12th April 2010, 06:23
Hi,

I have a class named MyIconText that derived from QWidget, and it has two QLabel in it.



class MyIconText : public QWidget
{
public:
MyIconText(const QString& text, const QPixmap& pixmap, QWidget* parent = 0)
:QWidget(parent)
{
QVBoxLayout* layout = new QVBoxLayout;
setLayout(layout);

QLabel *icon = new QLabel(this);
icon->setPixmap(pixmap);

layout->addWidget(icon);
layout->addWidget(new QLabel(text, this));
}
};


When I add MyIconText into QFrame and try to fetch it's location by childAt() when user click mouse on it.
by the childAt() function will not return the pointer of MyIconText, but it return the pointer of QLabel in MyIconText.



class MyFrame : public QFrame
{
........

void MyFrame::mousePressEvent(QMouseEvent* event)
{
QWidget* widget = childAt(event->pos());
}
}


quetions:
1) How do I let the childAt() return the pointer of MyIconText?
2) is there any better way to contain a icon and label together? because QLabel can not show icon and text at the same time.

thanks!

JohannesMunk
12th April 2010, 13:55
You are trying to handle the mousepress for an icon / text widget. Sounds to me like a job for a QPushButton (http://doc.trolltech.com/latest/qpushbutton.html) with icon and text set accordingly.

If that doesn't work for you, implement your MyIconsText-widgets-paintEvent and draw the pixmap and the text yourself. That way you will only have one widget, which is correctly returned by childat.

HIH

Johannes