Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Checking for selecting

    General and Desktop
    qaction camera
    4
    10
    3205
    Loading More Posts
    • 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.
    • S
      Suji last edited by Suji

      Hi there !

      I'm new in the programing world. I'm trying to do a simple QActionGroup which allows me to check for selecting between multiple availables cameras. I'm creating the QActiongroup menu like this (thanks QT examples) :

       camerasMenu = menuBar()->addMenu(tr("&Cameras"));
      
          QActionGroup *groupeCameras = new QActionGroup(this);
          groupeCameras->setExclusive(true);
          foreach (const QCameraInfo &cameraInfo, QCameraInfo::availableCameras())
          {
              QAction *groupeCamerasAction = new QAction(cameraInfo.description(), groupeCameras);
              groupeCamerasAction->setCheckable(true);
              groupeCamerasAction->setData(QVariant::fromValue(cameraInfo));
              if (cameraInfo == QCameraInfo::defaultCamera())
                  groupeCamerasAction->setChecked(true);
      
              camerasMenu->addAction(groupeCamerasAction);
      
                  qDebug() << cameraInfo.description();
          }
      

      Actually, i setup the viewfinder like this, manualy :

          QCameraViewfinder *viewfinder = new QCameraViewfinder(this);
          setCentralWidget(viewfinder);
      
      //Display Video stream
      
          foreach (const QCameraInfo &cameraInfo, cameras)
              {
                  qDebug() << cameraInfo.description();
      
                  if (cameraInfo.description() == "cameraname")
                      camera = new QCamera(cameraInfo);
              }
          camera->setViewfinder(viewfinder);
      

      Then i just connect "camera" to a simple pushbutton "on" that activate the stream.

      I want to be able to change the default camera by checking the camera desired on this QActiongroup, then when i click on my "on" button, that start the stream of the camera checked, and not the camera that i'm actually manualy choose.

      Am I clear ? :s

      Have a good day !

      Suji

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @Suji last edited by raven-worx

        @Suji

        the line QVariant::fromValue(cameraInfo) only works if the type is already registered as a meta type to Qt. I am not sure if QCameraInfo is registered as such (you will receive an error if not).

        If not you need to add it (somewhere before using it):

        Q_DECLARE_METATYPE(QCameraInfo) // put this in a header file
        qRegisterMetaType<QCameraInfo>(); // maybe in the constrcutor, or a static initializer
        

        Or simply add the description string to avoid this at all.

        Either way connect to the QActionGroup triggered(QAction)* signal in case you want to immediately react on the changes or use QActionGroup::checkedAction() to get the currently selected action.
        From the action then retrieve the data you already set using QAction::setData().

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        kshegunov 1 Reply Last reply Reply Quote 1
        • kshegunov
          kshegunov Moderators @raven-worx last edited by

          @raven-worx
          Actually,

          Q_DECLARE_METATYPE(QCameraInfo)
          

          is enough for QVariant, qRegisterMetaType<QCameraInfo>() one needs for queued connections.

          Read and abide by the Qt Code of Conduct

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @kshegunov last edited by

            @kshegunov
            yes right.
            It's a personal habit of mine to also call qRegisterMetaType() along with Q_DECLARE_METATYPE

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • S
              Suji last edited by

              hi ! Thanks for your answers!

              I've already fixed the meta type problem by the past by adding Q_DECLARE_METATYPE(QCameraInfo) in the .cpp file, seems to work like this, but is it better to put it into the header even if Qt dont send any error ?

              I started to use the QActionGroup triggered(QAction)* signal like this :

              QAction *checkedButton = groupeCameras->checkedAction();
              groupeCameras->triggered(checkedButton);
              

              (As reminder : groupeCameras is the QActionGroup.)

              I dont know how to do after this. I'm thinking about setting the checked camera as "defaultcamera" and construt the QCamera on it like this for example :

              QCameraInfo camdefault = QCameraInfo::defaultCamera();
              camera = new QCamera(camdefault);
              

              But i dont know how to link QAction to a QCamera in fact...

              Thanks a lot again for your help !

              raven-worx 1 Reply Last reply Reply Quote 0
              • raven-worx
                raven-worx Moderators @Suji last edited by

                @Suji
                the triggered() is a signal not a method you should call.

                connect( actionGroup, SIGNAL(triggered(QAction*)), this, SLOT(onActionSelected(QAction*)) );
                ....
                
                // must to be declared as slot
                void onActionSelected(QAction* action)
                {
                       QCameraInfo info = action->data().value<QCameraInfo>();
                       ....
                }
                

                Checks left out for this example.

                Or if it's easier for your code design use

                QAction* action = actionGroup->checkedAction();
                QCameraInfo info = action->data().value<QCameraInfo>();
                

                at a appropriate place.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply Reply Quote 1
                • S
                  Suji last edited by

                  Thank you,

                  Yes, my coding method is not clean, i'm learning alone, so it's harder to have clean habits. I will use your first example, it's seems to be a good way for resolving my problem + learning how to code properly.

                  Now i'll try to go ahead alone, i hope it will be good !

                  Thanks again @raven-worx, wish you a good day !

                  1 Reply Last reply Reply Quote 0
                  • S
                    Suji last edited by

                    Hi again,

                    Your solution worked well, now when i check on the QActionGroup groupeCameras, that selects the camera as i wanted.
                    But now, i want to display the camera's stream i choosed, but i got this answer back in the command window :

                    • "CamChoosed"
                      Failed to get the video control

                    Did i miss something obvious ?

                    Here is the code :

                    connect(groupeCameras, SIGNAL(triggered(QAction*)), this, SLOT(onActionSelected(QAction*)) );
                    
                    void MaFenetre::onActionSelected(QAction* action)
                    {
                           QCameraInfo info = action->data().value<QCameraInfo>();
                    
                                   qDebug() << info.description();
                                    camera = new QCamera(info);
                                    camera->setViewfinder(viewfinder);
                                    camera->start();
                    }
                    
                    
                    1 Reply Last reply Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      Hi and welcome to devnet,

                      You don't seem to delete the original camera before creating a new one thus I'm not sure that you can change your viewfinder like that.

                      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 Reply Quote 1
                      • S
                        Suji last edited by

                        Hi @SGaist !

                        I moved the 2 lines :

                        QCameraViewfinder *viewfinder = new QCameraViewfinder(this);
                         setCentralWidget(viewfinder);
                        

                        from my principale function to the MaFenetre::onActionSelected function, and that work !

                        Thanks a lot guys, you're magic here ! I was searching for days !

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post