Turning on android flashlight in Qt6 - c++

Currently I'm trying to make an mobile app and I need to turn on the flash light of a mobile device. Actually I want to use it as a torch(On/off/blink etc). I searched in the qt docs and found QCamera class. But I'm unable to turn the light on. I'm using Qt 6.3.1. Can anyone help?
I'm doing something like this:
// In constructor of a widget class,
cam = new QCamera(QCameraDevice::BackFace, this); //cam is declared in the header file
// In a function, called after a button click,
// cam->setFlashMode(QCamera::FlashOn);
cam->setTorchMode(QCamera::TorchOn);
cam->start();
I added this code inside a function and called it after a button-click event. But when I click the button, nothing happens.
UPD:
What I have found interesting is, I've tried printing the return value of isFlashModeSupported() and it returns false!
QString str = cam->isFlashModeSupported(QCamera::FlashOn) ? "Flash: Yes" : "Flash: No";
ui->Flash->setText(str); // str = "Flash: No"
I'm using a phone which has controllable flash light. So what can be the reason for this kind of behaviour?

Related

Making a simple message box using C++/WinRT

I'm having trouble making a simple message dialog in C++/WinRT. Something as simple as "You clicked this: press ok to continue" nothing fancy. In the standard Windows API you can simply write MessageBox() and new popup will show up where you can click ok, and you can do somthing similiar in C++/CX with
auto messageDialog = ref new Windows::UI::Popups::MessageDialog("You clicked on the button!"); messageDialog->ShowAsync();
I've tried using IMessageDialog and ContentDialog classes with no success and can't seem to find a single example out there on how to do it without getting into writing Xaml code which for my perticular project seems unnecisary for something as simple as a message box. Maybe I'm just not setting them up right? But then again there are no examples on how to set it up in the first place (in C++). What am I missing?
Thanks!
I was able to form a simple message dialog using this:
winrt::hstring Title = L"Hello";
winrt::hstring Content = L"Some cool content!";
winrt::Windows::UI::Popups::MessageDialog dialog(Content, Title);
dialog.ShowAsync();
Make sure to also include <winrt/Windows.UI.Core.h> so you can get access to the UI library.
And here is the result:

Problem with Keypress simulation on Linux (Ubuntu 18)

I'm making a hobby project that is basically a bot for a very old flash game, the mouse move and click works fine, but all key presses make the operating system lag/stutter and sometimes stop listening to all keyboard inputs, real or fake.
I started using just XLib with XTests but didn't work, so I tried XSendEvent instead of XTests, but all symptoms stayed the same, so the last attempt was with XDO, which gave better results, but still freezes the OS.
this is the current snippet that I'm trying to use to simulate a keypress:
//Constructor
CheatCore::CheatCore() {
xdo_t x = xdo_new(NULL);
Window *list;
xdo_search_t search;
unsigned int nwindows;
memset(&search, 0, sizeof(xdo_search_t));
search.max_depth = -1;
search.require = xdo_search::SEARCH_ANY;
search.searchmask = SEARCH_CLASS | SEARCH_ONLYVISIBLE;
search.winclass = "Chrome";
int id = xdo_search_windows(x, &search, &list, &nwindows);
qDebug() << nwindows;
if(!nwindows){
qDebug() << "Chrome not found";
return;
}
w = list[0];
//I have to call activate twice to really bring it forward, I suspect that its
//because I use a transparent "overlay" that show stats for the cheat and it is set as Aways on top
//(i used Qt to set it to not get any Events)
xdo_activate_window(x,w);
xdo_activate_window(x,w);
}
//there is a function that executes every second to check if a pixel color has changed,
//if so, then the SendKey is called to Reload weapon magazine pressing the "space" key
void CheatCore::SendKey(){
xdo_activate_window(x,w);
xdo_activate_window(x,w);
xdo_send_keysequence_window(x, w, "space", 500);
}
I'm using a transparent overlay to show the bot status, with just some numbers appearing, it is a widget created using Qt that is AlwaysOnTop and the paint event draws the desired information's, it is another object and don't have direct impact in the CheatCore, but this is the window flags used to draw over a transparent window and ignore events.
setWindowFlags(Qt::WindowTransparentForInput | Qt::FramelessWindowHint |
Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);
I didn't manage to understand what could be provoking this weird behavior, could it be the windowing system?
Also, I tried to find a Qt way of simulating mouse/keyboard inputs, but i didn't manage to find any solution to send events to other windows if there is a way possible of achieving this would be great!
The game i'm trying to automate is called "Storm the House"
If interested this is the link to the online repo : link
Can you help me make this work? Thank you!
Context about the setup:
Ubuntu 18.10 using VGA and Nvidia drivers (if it may influence the xserver)
Did you ever try to use xdotool from command line. To use xdotool you need to install package first.
To simualte a key press, you can use.
xdotool key <key>
For example if you want to simulate key press for X you can use this code
xdotool key x
Or any other combination like
xdotool key ctrl+f
Also you can replace key press with another one, for example if you want to replace pressing D with Backspace you can try this one
xdotool key D BackSpace
You can read complete guid online, also you can write script with this tool and use it in many different situations.
Also you can use it for remote connection too.
I hope this helps you with your little problem.
Using evdev is a linux specific option.
It's a simpler solution as you just need to open the correct file and write to it.
Take a look at this similar question to see how to get started.

JUCE - Making a New Window

Coming from making single-page applications with the visual WYSISWYG editor in JUCE, I'm having a bit of trouble figuring out how to invoke new windows (outside of the main GUI window). I made a test application that just has a small minimal main GUI that I created with the visual editor. It has a button "Make New Window." My goal is to be able to click that button and have a new window pop up and that this new window is a JUCE "GUI component," (AKA, the graphical / visual GUI editor file). Now, I actually have sort of achieved this, however, its throwing errors and assertions, so it would be great to get a very simple, step-by-step tutorial.
I studied the main.cpp file that the Projucer automatically created in order to get a feel for how they are creating a window. Here's what I did.
1) In my project, I added a new GUI Component (which becomes a class) and called it "InvokedWindow."
2) In my main GUI component class header, I added a new scoped pointer of type InvokedWindow: ScopedPointer<InvokedWindow> invokedWindow;
3) I created a new button in the main GUI editor called "Make New Window" and added this to the handler code:
newWindowPtr = new InvokedWindow; so that any time the button is hit, a new object of type InvokedWindow is created.
4) In the InvokedWindow class, in the constructor, on top of the automatically generated code, I added:
setUsingNativeTitleBar (true);
setCentrePosition(400, 400);
setVisible (true);
setResizable(false, false);
Which I sort of got from the main file of the JUCE application.
I also added a slider to this new window just to add functionality to it.
5) I added an overloaded function to let me close the window:
void InvokedWindow::closeButtonPressed()
{
delete this;
}
So, now when I run the app and click the make new window button, a new window does pop up, but I get an assertion:
/* Agh! You shouldn't add components directly to a ResizableWindow - this class
manages its child components automatically, and if you add your own it'll cause
trouble. Instead, use setContentComponent() to give it a component which
will be automatically resized and kept in the right place - then you can add
subcomponents to the content comp. See the notes for the ResizableWindow class
for more info.
If you really know what you're doing and want to avoid this assertion, just call
Component::addAndMakeVisible directly.
*/
Also, I'm able to close the window once and hit the button in the main GUI to create another instance of a newWindow, but closing it a second time leads to an error:
template <typename ObjectType>
struct ContainerDeletePolicy
{
static void destroy (ObjectType* object)
{
// If the line below triggers a compiler error, it means that you are using
// an incomplete type for ObjectType (for example, a type that is declared
// but not defined). This is a problem because then the following delete is
// undefined behaviour. The purpose of the sizeof is to capture this situation.
// If this was caused by a ScopedPointer to a forward-declared type, move the
// implementation of all methods trying to use the ScopedPointer (e.g. the destructor
// of the class owning it) into cpp files where they can see to the definition
// of ObjectType. This should fix the error.
ignoreUnused (sizeof (ObjectType));
delete object;
}
};
This is all a bit over my head. I was figuring it wouldn't be too bad to be able to create a new window, via a button. A new window that I could edit with the graphical GUI editor, but I'm not able to fully figure it out all on my own, through I did try. Could anyone post a step-by-step guide to doing this the correct way? I did post this at the JUCE forums, but due to my lack of GUI programming, I was unable to understand the solutions posted (my own fault), so I'm hoping to get a very simple guide to this. It would be very much appreciated. Thank you.
I figured it out. I needed to create:
A new GUI component (Remember, this is the visual editor in JUCE)
A class (I called it BasicWindow, based on the JUCE demo code) that acts as a shell to run this new window and holds the GUI component.
A JUCE SafePointer that makes a new object of type BasicWindow whenever the button is clicked and sets the attributes to that window.
Here is my code:
Referring to line 3) Inside the handler section of the button to create the new window:
basicWindow = new BasicWindow("Information", Colours::grey, DocumentWindow::allButtons);
basicWindow->setUsingNativeTitleBar(true);
basicWindow->setContentOwned(new InformationComponent(), true);// InformationComponent is my GUI editor component (the visual editor of JUCE)
basicWindow->centreWithSize(basicWindow->getWidth(), basicWindow->getHeight());
basicWindow->setVisible(true);
Referring to line 2) A .cpp file that defines what the BasicWindow is:
#include "../JuceLibraryCode/JuceHeader.h"
class BasicWindow : public DocumentWindow
{
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BasicWindow)
public:
BasicWindow (const String& name, Colour backgroundColour, int buttonsNeeded)
: DocumentWindow (name, backgroundColour, buttonsNeeded)
{
}
void closeButtonPressed() override
{
delete this;
}
};
And referring to line 1) Make the GUI editor component, which this is easy to do. You just right add a new file in the JUCE file manager. "Add New GUI Component," then visually add all your elements and handler code.
My biggest issue was that I was using a JUCE ScopedPointer, so after deleting the object, the pointer pointing to it wasn't being set back to NULL. The SafePointer does this. If any more explanation is needed, I'm happy to help, as this was terrible for someone with not much GUI development "under his belt."

Qt: How to change QWizard default buttons

I'm making a program with Qt 4.8.5 on a Fedora system (unix). It's a QWizard structure with its QWizardPages.
I needed to change the default QWizard buttons (back, next, finish...) and customize it. I found I could do it putting the next lines on the constructor of my QWizard class called BaseWizard (class BaseWizard : public QWizard)
QList<QWizard::WizardButton> button_layout;
button_layout <<QWizard::Stretch << QWizard::CustomButton1 << QWizard::BackButton << QWizard::NextButton << QWizard::FinishButton;
this->setOptions(QWizard::NoDefaultButton);
With that what I have from left to right is one custom buttons, the back button, the next button and the finish button, and when I want I can show or hide the custom buttons with SetVisible() / SetDisabled() / setEnabled() functions.
That worked perfect for what I wanted until now... I have to make some changes to the program so I need to change those buttons depending on the page the user is. As I said before, I know I can change the visibility of CustomButton 1 for example BUT I can't do the same with back button so... my quesiton is: How can I decide which buttons I show in every QWizardPage (and their text) and which is the best way to do it?
I've tried creating a function on my BaseWizard
// Function to have only 2 custom buttons
void BaseWizard::ChangeButtons()
{
QList<QWizard::WizardButton> button_layout;
button_layout <<QWizard::Stretch << QWizard::CustomButton1 << QWizard::CustomButton2;
setButtonLayout(button_layout);
}
And then in the QWizardPage (lets call it WP) using it like:
BaseWizard *bz;
bz->ChangeButtons();
But when I do that nothing changes.I can still see the NextButon for example. I have tried also using first a button_layout.clear(); to see if clenaing it before adding the buttons works, but not.
I have also tried changing the text of CustomButton1. If I do it in the WP after calling ChangeButtons with
wizard()->button(QWizard::CustomButton1)->setText("TEXT CHANGED");
Then the text changes but if I put it in the ChangeButton() function with this->button(QWizard::CustomButton1)->setText("BBBBB"); it does nothing (But its entering into the funcion). Ofc if I try to change the text of CustomButton2 in WP nothing happens because I can't still see that button... so any idea of what I am doing wrong or how could I get what I try will be very apreciated,
Thank you so much.
Ok, I finally knew why was my program crashing... I put here the solution if anyone needs it in the future:
BaseWizard *bz; // <-- HERE is the problem
bz->ChangeButtons();
I was not initializing the pointer so it has to be changed to:
BaseWizard *bz = dynamic_cast<BaseWizard*>(wizard());

How to set text below the QToolButton in QT not below the icon

I'm using QToolButton and I set the icon.
Now I want Text "below the QToolButton", "Not below the icon".
Is there any way to achieve this in C++,QT in Linux ?
I've found myself in the same position a while back ago while I was making an application for an embedded Linux system.
I haven't found a straight forward solution (I was searching for a way to achieve it with CSS).
What I ended up doing, was creating a new QWidget (using the designer). Then placing the button in it with a QLabel under it.
Then added a simple static function
static void wdgCustomButton::create(const QString iconPath, const QString text)
{
// create a new button here, create some modification functions for
// text, image and optionally QStyleSheets.
// Call those here (pass the arguments)
// Then return the button
// pseudo code, (not tested):
wdgCustomButton button = new wdgCustomButton( /* could pass a parent */ );
button->setIcon( iconPath ); // function simply calls the ui->button->setIcon
button->setText( text ); // function simply calls the ui->label->setText
return button;
}
And then add those new QWidgets to your pannel using code (maybe someone knows how to get it in the default toolbar, but I haven't searched for that myself yet since I never needed it).
this->menuButtons[menuBtnsCount] = wdgCustomButton::create( ":/Images/Warning.png", "Delete everything" );
this->menuButtons[menuBtnsCount]->setGeometry( QRect( /* size and position here */ ) );
this->menuButtons[menuBtnsCount]->show();
I hope this might give you an idea to fix it in an easy way!
Edit:
I'm sorry, I forgot to add something about the click event. The click event was mainly why I made a QWidget out of it!
I just used the connect function [I belive on the whole button like: connect(this->menuButtons[0], ...]