Results 1 to 2 of 2

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

  1. #1
    Join Date
    Apr 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Hi,

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

    Qt Code:
    1. class MyIconText : public QWidget
    2. {
    3. public:
    4. MyIconText(const QString& text, const QPixmap& pixmap, QWidget* parent = 0)
    5. :QWidget(parent)
    6. {
    7. QVBoxLayout* layout = new QVBoxLayout;
    8. setLayout(layout);
    9.  
    10. QLabel *icon = new QLabel(this);
    11. icon->setPixmap(pixmap);
    12.  
    13. layout->addWidget(icon);
    14. layout->addWidget(new QLabel(text, this));
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. class MyFrame : public QFrame
    2. {
    3. ........
    4.  
    5. void MyFrame::mousePressEvent(QMouseEvent* event)
    6. {
    7. QWidget* widget = childAt(event->pos());
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by silence; 12th April 2010 at 05:52.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to let QWidget::childAt() return only one when the target include many QWidge

    You are trying to handle the mousepress for an icon / text widget. Sounds to me like a job for a QPushButton 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

Similar Threads

  1. Promoting the parent QWidget of a QWidget form
    By extrakun in forum Qt Tools
    Replies: 6
    Last Post: 16th April 2010, 14:19
  2. Replies: 3
    Last Post: 1st April 2010, 23:56
  3. Focus on a QWidget item in a QWidget
    By vycke in forum Qt Programming
    Replies: 10
    Last Post: 7th January 2008, 21:00
  4. QWidget::exec() and QWidget::show()
    By MarkoSan in forum Qt Programming
    Replies: 6
    Last Post: 18th October 2007, 21:39
  5. Replies: 1
    Last Post: 2nd May 2006, 21:11

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.