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. Checking for selecting

Checking for selecting

Scheduled Pinned Locked Moved Solved General and Desktop
qactioncamera
10 Posts 4 Posters 3.5k 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.
  • S Offline
    S Offline
    Suji
    wrote on last edited by Suji
    #1

    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-worxR 1 Reply Last reply
    0
    • S 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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @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

      kshegunovK 1 Reply Last reply
      1
      • raven-worxR 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().

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @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-worxR 1 Reply Last reply
        0
        • kshegunovK kshegunov

          @raven-worx
          Actually,

          Q_DECLARE_METATYPE(QCameraInfo)
          

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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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
          0
          • S Offline
            S Offline
            Suji
            wrote on last edited by
            #5

            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-worxR 1 Reply Last reply
            0
            • S Suji

              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-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @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
              1
              • S Offline
                S Offline
                Suji
                wrote on last edited by
                #7

                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
                0
                • S Offline
                  S Offline
                  Suji
                  wrote on last edited by
                  #8

                  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
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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
                    1
                    • S Offline
                      S Offline
                      Suji
                      wrote on last edited by
                      #10

                      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
                      0

                      • Login

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