Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with inheritance and QObject/QDialog
Forum Updated to NodeBB v4.3 + New Features

Problem with inheritance and QObject/QDialog

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 559 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by Perdrix
    #1

    A colleague implemented an ABC (with some none abstract mfs) for a progress indicator base class. The idea being to implement a Qt subclass for AppA, a command line text based subclass for AppB and a different Qt subclass for AppC.

    class ProgressDlg : public ProgressBase,
    					public QDialog				
    {
    	Q_OBJECT
    private:
    	Ui::ProgressDlg* ui;
    

    This sort of worked until we wanted to change the code to have some base class function as slots at which point it all fell apart (because the Qt subclass was a QDialog (and therefore a QObject) and the base class was also a QObject (OOPS).

    Is it as simple as removing "public QDialog" from the class definition and adding :

    QDialog* theDialog as a private member, and then invoking:

    ui->SetupUi(theDialog);

    Or is there more I need to worry about?

    Thanks
    David

    SGaistS S 2 Replies Last reply
    0
    • PerdrixP Perdrix

      A colleague implemented an ABC (with some none abstract mfs) for a progress indicator base class. The idea being to implement a Qt subclass for AppA, a command line text based subclass for AppB and a different Qt subclass for AppC.

      class ProgressDlg : public ProgressBase,
      					public QDialog				
      {
      	Q_OBJECT
      private:
      	Ui::ProgressDlg* ui;
      

      This sort of worked until we wanted to change the code to have some base class function as slots at which point it all fell apart (because the Qt subclass was a QDialog (and therefore a QObject) and the base class was also a QObject (OOPS).

      Is it as simple as removing "public QDialog" from the class definition and adding :

      QDialog* theDialog as a private member, and then invoking:

      ui->SetupUi(theDialog);

      Or is there more I need to worry about?

      Thanks
      David

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It looks like a case of trying to inherit every time (is a VS has a) rather than compose.

      Rather than subclass that progress indicator class, why not compose a class with it and whatever you need to output the information ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • PerdrixP Perdrix

        A colleague implemented an ABC (with some none abstract mfs) for a progress indicator base class. The idea being to implement a Qt subclass for AppA, a command line text based subclass for AppB and a different Qt subclass for AppC.

        class ProgressDlg : public ProgressBase,
        					public QDialog				
        {
        	Q_OBJECT
        private:
        	Ui::ProgressDlg* ui;
        

        This sort of worked until we wanted to change the code to have some base class function as slots at which point it all fell apart (because the Qt subclass was a QDialog (and therefore a QObject) and the base class was also a QObject (OOPS).

        Is it as simple as removing "public QDialog" from the class definition and adding :

        QDialog* theDialog as a private member, and then invoking:

        ui->SetupUi(theDialog);

        Or is there more I need to worry about?

        Thanks
        David

        S Offline
        S Offline
        SimonSchroeder
        wrote on last edited by
        #3

        @Perdrix said in Problem with inheritance and QObject/QDialog:

        This sort of worked until we wanted to change the code to have some base class function as slots at which point it all fell apart (because the Qt subclass was a QDialog (and therefore a QObject) and the base class was also a QObject (OOPS).

        I am not entirely sure, but it might be that the Qt class needs to be the first class in the inheritance hierarchy. Try swapping ProgressBase and QDialog.

        Having QDialog as a member instead is also a solid way to go. That should work perfectly fine without any further problems.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimonSchroeder
          wrote on last edited by
          #4

          Maybe one more thought: You mentioned that one of the apps is command line based. For the most general case of a progress indicator I'd usually use a callback function. Then I could get rid of the ProgressBase base class altogether. Callback functions should have a void pointer to carry any user-defined load in addition to the progress itself. In your example the void pointer could point to your progress dialog and trigger according calls.

          PerdrixP 1 Reply Last reply
          0
          • S SimonSchroeder

            Maybe one more thought: You mentioned that one of the apps is command line based. For the most general case of a progress indicator I'd usually use a callback function. Then I could get rid of the ProgressBase base class altogether. Callback functions should have a void pointer to carry any user-defined load in addition to the progress itself. In your example the void pointer could point to your progress dialog and trigger according calls.

            PerdrixP Offline
            PerdrixP Offline
            Perdrix
            wrote on last edited by
            #5

            @SimonSchroeder A very short example of what you have in mind would be most helpful

            S 1 Reply Last reply
            0
            • PerdrixP Perdrix

              @SimonSchroeder A very short example of what you have in mind would be most helpful

              S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

              @Perdrix said in Problem with inheritance and QObject/QDialog:

              A very short example of what you have in mind would be most helpful

              I assume that this is in response to callback functions.

              Basically, you would have two functions: one for registering a callback with its associated data and the other to call the callback. For simplicity I'll assume there will only be one single callback at a time and I'll use global data. It is easy to make this a little nicer.

              void (*progressCallback)(int progress, void *data) = nullptr;
              void *progressCallbackData = nullptr;
              
              void registerProgressCallback(void (*callback)(int progress, void *data), void *data)
              {
                progressCallback = callback;
                progressCallbackData = data;
              }
              
              void updateProgress(int progress)
              {
                if(progressCallback)
                {
                  progressCallback(progress, progressCallbackData);
                }
              }
              
              class MyProgressClass
              {
              public:
                MyProgressClass()
                {
                  registerProgressCallback(std::mem_fun(&MyProgressClass::updateProgress), static_cast<void*>(this));
                }
              
                void updateProgress(int progress)
                {
                  //...
                }
              };
              

              This would be the rough outline for using callbacks. I am not entirely sure about std::mem_fun (or is it std::mem_fn now?). Maybe you need an additional wrapper function to cast the void pointer back to a pointer to MyProgressClass. Maybe lambdas would work as well. Things look a little easier if you don't use a function pointer but std::function<void(int,void*)> instead. I am also not entirely sure if I got the function pointer syntax right.

              registerProgressCallback() and updateProgress() could also be static functions of a class together with class variables storing all callbacks and their associated data in vectors.

              The advantage of the void pointer for additional data is that you can just declare a struct and put all function parameters into this. Then write a wrapper function that will unwrap the data from the struct and call you progress function with an arbitrary amount of (fixed) parameters.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved