Checking for selecting
-
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
-
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(). -
@raven-worx
Actually,Q_DECLARE_METATYPE(QCameraInfo)
is enough for
QVariant
,qRegisterMetaType<QCameraInfo>()
one needs for queued connections. -
@kshegunov
yes right.
It's a personal habit of mine to also call qRegisterMetaType() along with Q_DECLARE_METATYPE -
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 !
-
@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.
-
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 !
-
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(); }
- "CamChoosed"
-
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.
-
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 !