Results 1 to 4 of 4

Thread: How to pass a Qwidget to a function into a children class ?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to pass a Qwidget to a function into a children class ?

    I know that it is a recurrent theme, but I dont know to do this :
    I want to pass two different widgets to a function into a class I have instantiated from main app.
    The two different widgets can be Qlabel and /or QTextedit.

    So, I'd want to have a function like this
    Myfuntion(Qwidget a_widget) {
    A_widget_to_remember=a_widget

    .... }
    Later in another function I want to do something with 'a_widget_to_remember'

    if A_widget_to_remember=Qlabel ....
    if A_widget_to_remember=QTextedit ....

    1.- How I must to pass the label or Qtextedit to the function ?
    Using pointers ? Can anyonbe give some easy code ?
    2.- How can I identify what kind of Qwidget is 'A_widget_to_remember'

    Thanks.

  2. #2
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to pass a Qwidget to a function into a children class ?

    1. You clearly want to pass a pointer to your object, otherwise how can you keep track of it? Also, you might want to have a look at overloaded functions.
    2. There are different possible approaches, depending exactly on what you want to do, how generic a solution you want it to be. The easiest would be to have two different variables in your class, one for QLabel and another for QTextEdit, and a flag that tells you which should be used.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to pass a Qwidget to a function into a children class ?

    Quote Originally Posted by tonnot View Post
    1.- How I must to pass the label or Qtextedit to the function ?
    Using pointers ? Can anyone give some easy code ?
    Pass the widgets by pointer. You cannot pass them by value, even if that made sense, because they do not have the requisite copy constructor.
    Qt Code:
    1. class MyClass {
    2. ...
    3. private:
    4. QWidget *aWidget;
    5. };
    6.  
    7. MyClass::record(QWidget *widget)
    8. {
    9. aWidget = widget;
    10. }
    To copy to clipboard, switch view to plain text mode 
    2.- How can I identify what kind of Qwidget is 'A_widget_to_remember'
    QObject qobject_cast to the relevant type and check the returned pointer.

  4. The following user says thank you to ChrisW67 for this useful post:

    tonnot (9th February 2011)

  5. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to pass a Qwidget to a function into a children class ?

    Thank you very much !

Similar Threads

  1. Replies: 13
    Last Post: 30th November 2010, 12:47
  2. Replies: 2
    Last Post: 30th September 2010, 09:57
  3. Replies: 14
    Last Post: 1st December 2009, 20:45
  4. How to pass a QComboBox to a function?
    By Ricardo_arg in forum General Programming
    Replies: 4
    Last Post: 9th March 2008, 22:16
  5. How to pass a QString to another class ?
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 9th December 2006, 20:16

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.