Results 1 to 8 of 8

Thread: having access to members and functions of a class from its subclass

  1. #1
    Join Date
    Dec 2016
    Posts
    30
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows Android

    Default having access to members and functions of a class from its subclass

    hi. i have 2 classes A and B. B is subclass into the A. B needs to access to the A members and functions. how could i do this in Qt?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: having access to members and functions of a class from its subclass

    This has nothing to do with Qt. If you are writing in C++, it is a C++ question and you should look up C++ inheritance.

    If what you are asking is this kind of subclassing:

    Qt Code:
    1. class B
    2. {
    3. // ...
    4. };
    5.  
    6. class A : public B
    7. {
    8. // ...
    9. };
    To copy to clipboard, switch view to plain text mode 

    and you mean B needs to know about methods that are defined only in A, then how is this supposed to work? If you create an instance of class B and try to cast it to "A" and use A methods, your code will probably crash when you try to call A methods because an instance of B is not an instance of A even if you force the code to treat it that way.

    Maybe you should show some of your code where you are trying to do what you want, because the way I understand your question it doesn't make any sense.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    QJak (2nd May 2018)

  4. #3
    Join Date
    Dec 2016
    Posts
    30
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: having access to members and functions of a class from its subclass

    Quote Originally Posted by d_stranz View Post
    This has nothing to do with Qt. If you are writing in C++, it is a C++ question and you should look up C++ inheritance.

    If what you are asking is this kind of subclassing:

    Qt Code:
    1. class B
    2. {
    3. // ...
    4. };
    5.  
    6. class A : public B
    7. {
    8. // ...
    9. };
    To copy to clipboard, switch view to plain text mode 

    and you mean B needs to know about methods that are defined only in A, then how is this supposed to work? If you create an instance of class B and try to cast it to "A" and use A methods, your code will probably crash when you try to call A methods because an instance of B is not an instance of A even if you force the code to treat it that way.

    Maybe you should show some of your code where you are trying to do what you want, because the way I understand your question it doesn't make any sense.
    thanks so much d_stranz, class A is like this
    class A : public QThread
    if A can be a QWidget and a QThread too (class A : public QWidget and QThread), the problem can be solved.
    thanks a lot help me

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: having access to members and functions of a class from its subclass

    Quote Originally Posted by QJak View Post
    thanks so much d_stranz, class A is like this
    class A : public QThread
    if A can be a QWidget and a QThread too (class A : public QWidget and QThread), the problem can be solved.
    thanks a lot help me
    Error. Qt GUI objects can only live in the main thread. You have a conceptual problem.

  6. The following user says thank you to Lesiok for this useful post:

    QJak (2nd May 2018)

  7. #5
    Join Date
    Dec 2016
    Posts
    30
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: having access to members and functions of a class from its subclass

    thanks
    I need to do 3 works toghether in the same time:
    1- first thread does processing on gived data and showing results with GUI
    2- second thread does logging (saving above data into a file)
    3- third thread does waiting every 10 second and then sending a byte to GUI of the first thread for showing

    please help me to do this work
    thanks

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: having access to members and functions of a class from its subclass

    I think you only need two threads: main (with GUI) and worker. Worker process data, logs them to the file and sends data to GUI with signals and slots. Sending data to GUI is activated with QTimer::timeout signal with 10s period.

  9. The following user says thank you to Lesiok for this useful post:

    QJak (2nd May 2018)

  10. #7
    Join Date
    Dec 2016
    Posts
    30
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: having access to members and functions of a class from its subclass

    thanks
    before i did this by using signal and slot. but when i send a signal to two slots, the slots not working in paralell procces and first one of these slots works and after finishing this slot, the other slot starts. but i need the two slots work paralel (like thread)

  11. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: having access to members and functions of a class from its subclass

    How do slots work in parallel if they are in one thread? If they must work in parallel then they must be in separate threads.

  12. The following user says thank you to Lesiok for this useful post:

    QJak (2nd May 2018)

Similar Threads

  1. Why operator overloading functions can't be members of class
    By rawfool in forum General Programming
    Replies: 1
    Last Post: 31st October 2017, 16:53
  2. Replies: 2
    Last Post: 27th October 2013, 18:23
  3. Replies: 4
    Last Post: 17th January 2011, 14:05
  4. Replies: 1
    Last Post: 9th January 2011, 07:20
  5. Static functions and class members
    By Raistlin in forum General Programming
    Replies: 5
    Last Post: 22nd December 2006, 10:00

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.