How to set focus to a QWidget, on a different window - c++

I have created two QPushButton on two different QMainWindow. I am assigning focus to them randomly at a specific interval.Here is the code.
int main(int argc, char **argv){
QApplication a(argc, argv);
QMainWindow *win1= new QMainWindow();
win1->resize(567,578);
win1->move(67,30);
win1->show();
QMainWindow *win2= new QMainWindow();
win2->resize(567,578);
win2->move(97,580);
win2->show();
win1->show();
//win2->setModal(true);
QPushButton *but1 = new QPushButton(win1);
but1->resize(80,20);
but1->move(100,100);
but1->setText("1");
but1->show();
QPushButton *but2 = new QPushButton(win2);
but2->resize(80,20);
but2->move(100,300);
but2->setText("2");
but2->show();
while(1){
if((rand()%2) == 1){
//win2->lower();
win1->raise();
win1->activateWindow();
win1->setWindowState(Qt::WindowActive);
win1->setFocus(Qt::ActiveWindowFocusReason);
but1->setFocus(Qt::ActiveWindowFocusReason);
}
else{
//win1->lower();
win2->raise();
win2->activateWindow();
win2->setFocus(Qt::ActiveWindowFocusReason);
but2->setFocus(Qt::ActiveWindowFocusReason);
}
qApp->processEvents(0x00);
sleep(2);
}
But the problem is the title bar of the first window is not changing color(usually putting a window back-n-forth through the visual stack changes the color of the title-bar), even when it has become the top window visually

You will obtain the desired behavious if you change your last loop to something similar:
while (1) {
// Exits if both windows are closed
if (!win1->isVisible() && (!win2->isVisible())) {
return 0;
}
// Eventually changes the focus, if the desired window is still visible
if((rand() % 2) == 1) {
if (win1->isVisible()) {
QApplication::setActiveWindow(win1);
}
}
else {
if (win2->isVisible()) {
QApplication::setActiveWindow(win2);
}
}
QTime now;
now.start();
do {
qApp->processEvents(0x00);
} while (now.elapsed() < 2000);
}
Anyway, if you put your program to sleep, it will not respond to user input during that interval, so be careful.
The implementation is quite ugly, but it checks if the windows to be focused is still visible (i.e. the user has not closed it) and eventually exits if both have been closed.
Of course I suppose that you were only interested in the setActiveWindow() thing, so I've not spent much time in writing something beautiful!

Related

QT doesn't properly processes events in loop

I want to repeat the action performed in a SLOT until a qpushbutton is down.So I've done the following connections:
connect(ui->button,SIGNAL(pressed()),this,SLOT(button_hold()));
connect(ui->button,SIGNAL(released()),this,SLOT(button_released()));
and I've implemented the SLOTS in the following way
void My_class::button_hold(){
//CLASS ATTRIBUTE key_is_released , i
QThread::msleep(200);
int wait_lock = 500;
i++; //GOAL OF THE SLOT
QCoreApplication::processEvents(QEventLoop::AllEvents);
while(!key_is_released){
QThread::msleep(wait_lock);
i++;
cout<<i<<endl;
if(wait_lock > 50) wait_lock -= 50;
QCoreApplication::processEvents(QEventLoop::AllEvents);
}
key_is_released = false;
}
void My_class::button_released(){
key_is_released = true;
}
The goal is to repeat the action in the button_hod() Slot (In the example it's to increase i) even more quickly decreasing wait_lock and keeping the button down.The issue is that if i click button two times or more in a quick way the loop never ends,like if processEvents() would not work.The firts msleep is used in order to do not enter the loop (i is increased just once)if the button is just clicked and it is not keep down too long.If i do not click quickly but I keep down the button the method works well.
What am I doing wrong?
Reentering the event loop is a bad idea, and leads to spaghetti code.
Qt provides a wonderful state machine system with UML semantics. It's often best to model the system you're designing as a state machine. Here, there are two states: an idle state, and an active state: the value is incremented periodically while active.
The state machine decouples the Ui particulars (a button) from the core functionality: that of a periodically incrementing value.
Ideally, you would also factor out the state machine and the value to a controller class, and only connect the controller and the Ui from main() or a similar function.
// https://github.com/KubaO/stackoverflown/tree/master/questions/48165864
#include <QtWidgets>
#include <type_traits>
class Ui : public QWidget {
Q_OBJECT
int m_value = -1;
QStateMachine m_machine{this};
QState m_idle{&m_machine}, m_active{&m_machine};
QVBoxLayout m_layout{this};
QPushButton m_button{"Hold Me"};
QLabel m_indicator;
QTimer m_timer;
void setValue(int val) {
if (m_value == val) return;
m_value = val;
m_indicator.setNum(m_value);
}
void step() {
if (m_value < std::numeric_limits<decltype(m_value)>::max())
setValue(m_value + 1);
}
public:
Ui(QWidget * parent = {}) : QWidget(parent) {
m_layout.addWidget(&m_button);
m_layout.addWidget(&m_indicator);
m_machine.setInitialState(&m_idle);
m_idle.addTransition(&m_button, &QPushButton::pressed, &m_active);
m_active.addTransition(&m_button, &QPushButton::released, &m_idle);
m_machine.start();
m_timer.setInterval(200);
connect(&m_timer, &QTimer::timeout, this, &Ui::step);
connect(&m_active, &QState::entered, [this]{
step();
m_timer.start();
});
connect(&m_active, &QState::exited, &m_timer, &QTimer::stop);
setValue(0);
}
};
int main(int argc, char ** argv) {
QApplication app{argc, argv};
Ui ui;
ui.show();
return app.exec();
}
#include "main.moc"
The problem was that clicking fast let say 10 times ,the release slot was called yet 10 times before entering in the loop, so key_is_released would be never updated again to true.Instead the button_hold slot was not yet been called for ten times.So adding a counter, increasing it in the button_hold slot and decreasing it in the release slot and adding a second condition counter>0 besides !key_is_released in the while loop fixes all.(It enters the loop only if not all the release slot have been run)

C++ Break a loop from another function

I'm developing a video recording software but I am stuck on an issue.
I want to stop the recording when the button Stop is clicked but nothing happens when I click it...
Here is my core (simplified):
MySoftware.hpp:
bool b_Stop = false;
MySoftware.cpp
MainWindow::MainWindow() : QWidget() {
qpb_StartCapture = new QPushButton("Start Capture", this);
QObject::connect(qpb_StartCapture, SIGNAL(clicked()), this, SLOT(startCapture()));
qpb_StopCapture = new QPushButton("Stop Capture", this);
QObject::connect(qpb_StopCapture, SIGNAL(clicked()), this, SLOT(stopCapture()));
}
void MainWindow::startCapture() {
b_Stop = false;
// CAMERAS INITIALIZATION
while (!b_Stop) {
for (int i = 0; i < v_cp_Cameras.size(); i++) {
// IMAGE CAPTURE
}
}
// IMAGES PROCESSING
}
void MainWindow::stopCapture() {
b_Stop = true;
}
The way I see it is that startCapture is probably called from your event loop. This blocks any other events from being processed. Try putting your loop into a separate thread and see if that works.
You're having the image capture inside of a for loop that doesn't check whether or not the stop button is activated. You're checking the while loop, which probably never starts over. put if (b_stop) {break;} inside the for loop and it might work.

QProgresdialog: busy waiting bar is not moving while function is executing

I'm trying to show a busy waiting bar when one function is executing, my problem is it stop moving once the function starts.
MyProgressDialog *progBar= new MyProgressDialog();
QProgressBar* bar = new QProgressBar(progBar);
bar->setRange(0, 0);
bar->setValue(0);
progBar->setBar(bar);
QString labeltext=QString("<qt> <center><big><b>%1</b></big></center> <br><b>%2</b><br> %3 <br><b>%4</b><br> %5</qt>")
.arg(progBar->labeltext)
.arg("File in :")
.arg(FileI)
.arg("File out :")
.arg(FileO);
progBar->label->setText(labeltext);
progBar->setValue(10);
progBar->show();
progBar->setValue(20);
Sleep(500);
progBar->setValue(50);
Sleep(500);
MyFunction(FileI,FileO,mode,key);
Sleep(500);
progBar->setValue(80);
Sleep(500);
progBar->setValue(100);
progBar->close();
delete bar;
delete progBar;
I warpped my function with a sleep and set value in purpose to let it moving but in vain, when I remove them MyProgressdialog didn't show its contents, am I need to lunch my function in a separate thread ?
I tried to use QFutureWatcher:
QFutureWatcher<void> futureWatcher;
QFuture<void> f1 = run(
MyFunction,
filePath,
file.absolutePath()+"/OUT_"+fileN,
1,
key
);
QObject::connect(&futureWatcher, SIGNAL(finished()), progBar, SLOT(reset()));
QObject::connect(progBar, SIGNAL(canceled()), &futureWatcher, SLOT(cancel()));
QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int,int)), progBar, SLOT(setRange(int,int)));
QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), progBar, SLOT(setValue(int)));
// Start the computation.
futureWatcher.setFuture(f1);
// Display the dialog and start the event loop.
progBar->exec();
futureWatcher.waitForFinished();
delete progBar;
It works fine and my bar is moving when I call MyFunction just one time (for one file) but the problem occurs when I call MyFunction more than one time (for many files successively): it works but I get the same OUT_file for all files treated, I think it is multithreading issue.
EDIT: class MyProgressDialog
class MyProgressDialog: public QProgressDialog
{
Q_OBJECT
public:
MyProgressDialog()
{
qDebug()<<"MyProgressDialog constructor";
label=new QLabel(this);
QPalette* palette = new QPalette();
palette->setColor(QPalette::Window,"#F8F8FF");
setPalette(*palette);
QFont* font = new QFont("Courier New");
font->setItalic(true);
font->setPixelSize(15);
setFont(*font);
adjustSize();
setWindowIcon(QIcon(QApplication::applicationDirPath()+"/icons/icon1.png"));
setWindowFlags(Qt::WindowStaysOnTopHint);
setMinimumWidth(500);
setMinimumHeight(200);
labeltext=QString("Please wait until Encryption/Decryption was done");
label->setText(labeltext);
label->adjustSize();
label->setWordWrap (true);
setLabel(label);
setRange(0,100);
setWindowTitle("MyFunction progress");
setModal(true);
}
~MyProgressDialog()
{
qDebug()<<"MyProgressDialog destructor";
delete label;
}
public:
int value;
QString labeltext;
QLabel* label;
};
UI in Qt is event driven. So executing your code in same thread as ui will block every ui event untill your function is finished. There are two aproaches to show progress.
Use thread for computing and send update events to UI
Easier way: after each setValue and show calls call QApplication::processEvents(); static method. Calling QApplication::processEvents() will dispatch events currently queued on event loop. Those events include all ui related events
Here is code sample for gcc/mingw gcc
#include <QApplication>
#include <QProgressBar>
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QProgressBar bar;
bar.setRange(0, 100);
bar.show();
app.processEvents();
usleep(250000);
for(int i = 1; i <= 10; ++i)
{
bar.setValue(i * 10);
app.processEvents();
usleep(250000);
}
return 0;
}
It shows progress bar and steps it by 10 every 0,25s
Your code should look something like this:
MyProgressDialog *progBar= new MyProgressDialog();
QProgressBar* bar = new QProgressBar(progBar);
bar->setRange(0, 100); // note your "busy state won't be shown as you're changing value right after show
bar->setValue(0);
progBar->setBar(bar);
QString labeltext=QString("<qt> <center><big><b>%1</b></big></center> <br><b>%2</b><br> %3 <br><b>%4</b><br> %5</qt>")
.arg(progBar->labeltext)
.arg("File in :")
.arg(FileI)
.arg("File out :")
.arg(FileO);
progBar->label->setText(labeltext);
progBar->setValue(10);
progBar->show();
QApplication::processEvents(); // HERE
progBar->setValue(20);
QApplication::processEvents(); // HERE
Sleep(500);
progBar->setValue(50);
QApplication::processEvents(); // HERE
Sleep(500);
MyFunction(FileI,FileO,mode,key);
Sleep(500);
progBar->setValue(80);
QApplication::processEvents(); // HERE
Sleep(500);
progBar->setValue(100);
progBar->close();
QApplication::processEvents(); // HERE
delete bar;
delete progBar;

Yes/No message box using QMessageBox

How do I show a message box with Yes/No buttons in Qt, and how do I check which of them was pressed?
I.e. a message box that looks like this:
You would use QMessageBox::question for that.
Example in a hypothetical widget's slot:
#include <QApplication>
#include <QMessageBox>
#include <QDebug>
// ...
void MyWidget::someSlot() {
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Test", "Quit?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
qDebug() << "Yes was clicked";
QApplication::quit();
} else {
qDebug() << "Yes was *not* clicked";
}
}
Should work on Qt 4 and 5, requires QT += widgets on Qt 5, and CONFIG += console on Win32 to see qDebug() output.
See the StandardButton enum to get a list of buttons you can use; the function returns the button that was clicked. You can set a default button with an extra argument (Qt "chooses a suitable default automatically" if you don't or specify QMessageBox::NoButton).
You can use the QMessage object to create a Message Box then add buttons :
QMessageBox msgBox;
msgBox.setWindowTitle("title");
msgBox.setText("Question");
msgBox.setStandardButtons(QMessageBox::Yes);
msgBox.addButton(QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
if(msgBox.exec() == QMessageBox::Yes){
// do something
}else {
// do something else
}
QT can be as simple as that of Windows. The equivalent code is
if (QMessageBox::Yes == QMessageBox(QMessageBox::Information, "title", "Question", QMessageBox::Yes|QMessageBox::No).exec())
{
}
I'm missing the translation call tr in the answers.
One of the simplest solutions, which allows for later internationalization:
if (QMessageBox::Yes == QMessageBox::question(this,
tr("title"),
tr("Message/Question")))
{
// do stuff
}
It is generally a good Qt habit to put code-level Strings within a tr("Your String") call.
(QMessagebox as above works within any QWidget method)
EDIT:
you can use QMesssageBox outside a QWidget context, see #TobySpeight's answer.
If you're even outside a QObject context, replace tr with qApp->translate("context", "String") - you'll need to #include <QApplication>
QMessageBox includes static methods to quickly ask such questions:
#include <QApplication>
#include <QMessageBox>
int main(int argc, char **argv)
{
QApplication app{argc, argv};
while (QMessageBox::question(nullptr,
qApp->translate("my_app", "Test"),
qApp->translate("my_app", "Are you sure you want to quit?"),
QMessageBox::Yes|QMessageBox::No)
!= QMessageBox::Yes)
// ask again
;
}
If your needs are more complex than provided for by the static methods, you should construct a new QMessageBox object, and call its exec() method to show it in its own event loop and obtain the pressed button identifier. For example, we might want to make "No" be the default answer:
#include <QApplication>
#include <QMessageBox>
int main(int argc, char **argv)
{
QApplication app{argc, argv};
auto question = new QMessageBox(QMessageBox::Question,
qApp->translate("my_app", "Test"),
qApp->translate("my_app", "Are you sure you want to quit?"),
QMessageBox::Yes|QMessageBox::No,
nullptr);
question->setDefaultButton(QMessageBox::No);
while (question->exec() != QMessageBox::Yes)
// ask again
;
}
If you need asynchronous call you should use open and result methods instead of question or exec. Sample code inside a QWidget method:
QMessageBox* const message = new QMessageBox(QMessageBox::Icon::Question, tr("Test"),
tr("Quit?"), QMessageBox::Button::Yes | QMessageBox::Button::No, this);
message->setDefaultButton(QMessageBox::Button::No);
message->open();
connect(message, &QDialog::finished, this, [message] {
message->deleteLater();
if (message->result() == QMessageBox::Button::Yes) {
QApplication::quit();
}
});
It should not be usefull just for a quit dialog but for other confirmation dialogs where parent widget might be destroyed by external events it is the main way to avoid a crash.
Python equivalent code for a QMessageBox which consist of a question in it and Yes and No button. When Yes Button is clicked it will pop up another message box saying yes is clicked and same for No button also. You can push your own code after if block.
button_reply = QMessageBox.question(self,"Test", "Are you sure want to quit??", QMessageBox.Yes,QMessageBox.No,)
if button_reply == QMessageBox.Yes:
QMessageBox.information(self, "Test", "Yes Button Was Clicked")
else :
QMessageBox.information(self, "Test", "No Button Was Clicked")
If you want to make it in python you need check this code in your workbench.
also write like this.
we created a popup box with python.
msgBox = QMessageBox()
msgBox.setText("The document has been modified.")
msgBox.setInformativeText("Do you want to save your changes?")
msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
msgBox.setDefaultButton(QMessageBox.Save)
ret = msgBox.exec_()

How to change a QMenu text from english to russian by clicking a button

Please consider we have a menu which has text set "MyMenu" and I want to change the menu text by clicking a button in the same widget from "MyMenu" to "МойМеню". Could you bring a code snippet please for that operation?
Have a look at "Dynamic Translation", http://doc.qt.io/qt-5/internationalization.html
void MyWidget::changeEvent(QEvent *event)
{
if (e->type() == QEvent::LanguageChange)
{
titleLabel->setText(tr("Document Title"));
...
okPushButton->setText(tr("&OK"));
// You could also use : retranslateUi(QWidget*);
}
else
{
QWidget::changeEvent(event);
}
}
This will be helpful to you as well : http://doc.qt.io/qt-5/qcoreapplication.html#installTranslator
Basically, when you call : qApp->installTranslator(MyAppTranslator) it will create a QEvent::LanguageChange.
So, provide a simple QComboBox with English/Russian, and when the selected language changes, call qApp->installTranslator(MyAppTranslator);. Then make sure your buttons are properly set up in changeEvent, and that's it !
Hope it helps a bit !
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));//this is the solution
.............
}
And in the code you can dynamically change the strings if you set them from the begining by using tr() function [tr("your text")].