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. open dialog in friend class

open dialog in friend class

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.1k Views
  • 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on last edited by hobbyProgrammer
    #1

    trying to create a dialog in a class from a different class.

    My first class had become too big, so I decided to create a new friend class and transfer a few methods to that friend class, so the first class could still call those methods.
    One of those methods creates a QInputDialog, but it gives an error over:

        GraphicsView *gv;
        
        bool ok;
        QInputDialog *dialog = new QInputDialog();
        QString text = dialog->getText(this, tr("Save As"), tr("Please enter a name: "), QLineEdit::Normal, "", &ok);
    

    Error:

    cannot initialize a parameter of type QWidget* with an.....
    

    I also tried:

        GraphicsView *gv;
        
        bool ok;
        QInputDialog *dialog = new QInputDialog();
        QString text = dialog->getText(gv, tr("Save As"), tr("Please enter a name: "), QLineEdit::Normal, "", &ok);
    

    But that gave me this error:

    no viable conversion from 'class 1' to 'QWidget*'
    

    I am curious what the best solution is in this case. I've rarely ever used friend classes and never used them in Qt.

    1 Reply Last reply
    0
    • H hobbyProgrammer

      @JonB Hi thank you so much.

      I've already tried multiple inheritance once, but it was a mess as both classed (QWidget and QGraphcisView contained some of the same functions and I've overwritten some of them and it's doesn't know which one to override, so that might not be the best solution here).
      But I've never heard of a helper class (I'm quite new to c++), but that might be the way to go.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #12

      @hobbyProgrammer
      Yes, I meant to write, multiple-inheritance can be a bit tricky if you're not used to it. You have to explicitly indicate which class's method you are trying to override/invoke if there is ambiguity.

      You can Google for helper class C++ for examples. For a start any methods in your current derivation which do not use this (for the QGraphicsView), or can be re-factored not to use it, are candidates for moving out. Related is also encapsulation, where you define a class which does not derive from QGraphicsView but instead has the QGraphicsView as a member, and operates on that as needed. It's a fine line to decide when best to use what in your design, but that's programming for you :)

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        gatText is static, you don't need an instance:
        QString text = QInputDialog::getText(this, tr("Save As"), tr("Please enter a name: "), QLineEdit::Normal, QString(), &ok);

        To solve your error you need to tell us what type this is

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        H 1 Reply Last reply
        0
        • VRoninV VRonin

          gatText is static, you don't need an instance:
          QString text = QInputDialog::getText(this, tr("Save As"), tr("Please enter a name: "), QLineEdit::Normal, QString(), &ok);

          To solve your error you need to tell us what type this is

          H Offline
          H Offline
          hobbyProgrammer
          wrote on last edited by hobbyProgrammer
          #3

          @VRonin it's a QGraphicsView. The first class is a customized QGraphicsView class to override some of the methods of QGraphicsView and create a custom QGraphicsView.

          So this function used to be inside the GraphicsView class and now I've transfered it to the friend class, but I still need to create a dialog.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #4

            so the above should work

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            H 1 Reply Last reply
            0
            • VRoninV VRonin

              so the above should work

              H Offline
              H Offline
              hobbyProgrammer
              wrote on last edited by hobbyProgrammer
              #5

              @VRonin said in open dialog in friend class:

              so the above should work

              I thought that this should work, but apparently it doesn't.

              GraphicsView *gv;
                  
                  bool ok;
                  QInputDialog *dialog = new QInputDialog();
                  QString text = dialog->getText(gv, tr("Save As"), tr("Please enter a name: "), QLineEdit::Normal, "", &ok);
              

              This seems to generate an error when I include Q_OBJECT in the class declaration.

              Class contains Q_OBJECT macro but does not inherit from QObject
              

              But if I don't include Q_OBJECT I get this error:

              use of undeclared identifier 'tr'
              
              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @hobbyProgrammer said in open dialog in friend class:

                GraphicsView

                You need to pass a QWidget pointer, so if GraphicsView doesn't derive from QWidget (directly or indirectly) you won't be able to pass it to this function - C++ basics.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                H 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @hobbyProgrammer said in open dialog in friend class:

                  GraphicsView

                  You need to pass a QWidget pointer, so if GraphicsView doesn't derive from QWidget (directly or indirectly) you won't be able to pass it to this function - C++ basics.

                  H Offline
                  H Offline
                  hobbyProgrammer
                  wrote on last edited by
                  #7

                  @Christian-Ehrlicher GraphicsView derives from QGraphicsView, not QWidget. However, the constructor of GraphicsView looks like this:

                  GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent)
                  {
                      scene = new QGraphicsScene();
                      this->setScene(scene);
                      this->setRenderHints(QPainter::Antialiasing);
                      this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
                  }
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • H hobbyProgrammer

                    @Christian-Ehrlicher GraphicsView derives from QGraphicsView, not QWidget. However, the constructor of GraphicsView looks like this:

                    GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent)
                    {
                        scene = new QGraphicsScene();
                        this->setScene(scene);
                        this->setRenderHints(QPainter::Antialiasing);
                        this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
                    }
                    
                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @hobbyProgrammer said in open dialog in friend class:

                    However, the constructor of GraphicsView looks like this

                    This doesn't change anything: this is just a parameter for the constructor (parent) it does not make QGraphicsView a QWidget.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    H 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @hobbyProgrammer said in open dialog in friend class:

                      However, the constructor of GraphicsView looks like this

                      This doesn't change anything: this is just a parameter for the constructor (parent) it does not make QGraphicsView a QWidget.

                      H Offline
                      H Offline
                      hobbyProgrammer
                      wrote on last edited by
                      #9

                      @jsulm okay but what do I need to do? I don't really get how I need to pass a QWidget pointer.
                      If there is a different solution to shrink the size of my GraphicsView.cpp file, I am more than happy to hear it. (Most preferably devide methods to another source file if possible)

                      JonBJ 1 Reply Last reply
                      0
                      • H hobbyProgrammer

                        @jsulm okay but what do I need to do? I don't really get how I need to pass a QWidget pointer.
                        If there is a different solution to shrink the size of my GraphicsView.cpp file, I am more than happy to hear it. (Most preferably devide methods to another source file if possible)

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #10

                        @hobbyProgrammer
                        The QWidget *parent is just an optional parameter for a QWidget to be the parent of a QGraphicsView. You cannot make a QGraphicsView be a QWidget, it isn't one, not use QWidget methods on a QGraphicsView. Nothing to do with source files.

                        You'll have to find some other way to "shrink the size of my GraphicsView.cpp file,". Without knowing what your code looks like it's difficult to say.

                        • For example, and it's only an example, you might sub-class to multiple depths, QGraphicsView <- MySimpleGraphicsView <- MyMoreAdvancedGraphicsView <- ..., where each level of sub-class has a certain amount of related functionality but not too much, so that you can spread across multiple sub-classes/files.
                        • Or, you might make your sub-class from QGraphicsView use C++ multiple inheritance, where it would derive from QGraphicsView plus some other class(es) of yours which implement additional functionality, which again could be in separate files.
                        • Or, you might write a helper class which holds some of the functionality so it's not all in one file.
                        • If you are saying you have some QWidget-y things you need to do around the QGraphicsView, perhaps you should write that in a QWidget-derived class which you then pass as the parent of the QGraphicsView.
                        H 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @hobbyProgrammer
                          The QWidget *parent is just an optional parameter for a QWidget to be the parent of a QGraphicsView. You cannot make a QGraphicsView be a QWidget, it isn't one, not use QWidget methods on a QGraphicsView. Nothing to do with source files.

                          You'll have to find some other way to "shrink the size of my GraphicsView.cpp file,". Without knowing what your code looks like it's difficult to say.

                          • For example, and it's only an example, you might sub-class to multiple depths, QGraphicsView <- MySimpleGraphicsView <- MyMoreAdvancedGraphicsView <- ..., where each level of sub-class has a certain amount of related functionality but not too much, so that you can spread across multiple sub-classes/files.
                          • Or, you might make your sub-class from QGraphicsView use C++ multiple inheritance, where it would derive from QGraphicsView plus some other class(es) of yours which implement additional functionality, which again could be in separate files.
                          • Or, you might write a helper class which holds some of the functionality so it's not all in one file.
                          • If you are saying you have some QWidget-y things you need to do around the QGraphicsView, perhaps you should write that in a QWidget-derived class which you then pass as the parent of the QGraphicsView.
                          H Offline
                          H Offline
                          hobbyProgrammer
                          wrote on last edited by
                          #11

                          @JonB Hi thank you so much.

                          I've already tried multiple inheritance once, but it was a mess as both classed (QWidget and QGraphcisView contained some of the same functions and I've overwritten some of them and it's doesn't know which one to override, so that might not be the best solution here).
                          But I've never heard of a helper class (I'm quite new to c++), but that might be the way to go.

                          JonBJ 1 Reply Last reply
                          0
                          • H hobbyProgrammer

                            @JonB Hi thank you so much.

                            I've already tried multiple inheritance once, but it was a mess as both classed (QWidget and QGraphcisView contained some of the same functions and I've overwritten some of them and it's doesn't know which one to override, so that might not be the best solution here).
                            But I've never heard of a helper class (I'm quite new to c++), but that might be the way to go.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #12

                            @hobbyProgrammer
                            Yes, I meant to write, multiple-inheritance can be a bit tricky if you're not used to it. You have to explicitly indicate which class's method you are trying to override/invoke if there is ambiguity.

                            You can Google for helper class C++ for examples. For a start any methods in your current derivation which do not use this (for the QGraphicsView), or can be re-factored not to use it, are candidates for moving out. Related is also encapsulation, where you define a class which does not derive from QGraphicsView but instead has the QGraphicsView as a member, and operates on that as needed. It's a fine line to decide when best to use what in your design, but that's programming for you :)

                            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