Qt How to update GUI in the loop - qt

I need to update screen to show how button is moving.
Here is my code:
void mouseReleaseEvent(QMouseEvent *event){
double b=(button->x()*event->y())/(button->x()-1);
double k=(button->y()-b)/button->x();
int time=0;
fnl=false;
if(event->button()==Qt::LeftButton)
{
while(!fnl)
{
int mX=button->x()-1;
int mY=k*(button->x()-1)+b;
button->setText(QString::number(b));
button->move(mX,mY);
QThread::sleep(1);
//here I need to update screen and show button
}
}
}
But it does not update GUI. It simply plays inside the loop.

Never use QThread::sleep() in the GUI thread, It will block the GUI thread from doing anything. Instead, You can use QTimer to schedule something to run at a later point in time. Also, Your slots/functions should be as short and optimized as possible in order to return control to the event loop and be able to handle other events that may happen.
You may want to have a look at this question for a similar problem.
The same technique can be applied here to solve the problem by replacing your while loop with a slot and a QTimer whose interval is set to 0. But Qt can do all that job using the animation framework, Here is an example of a button that gets moved when clicked:
#include <QtWidgets>
int main(int argc, char* argv[]){
QApplication a(argc, argv);
//create and show button
QPushButton button("Animated Button");
button.move(QPoint(100, 100));
button.show();
//create property animator object that works on the position of the button
QPropertyAnimation animation(&button, "pos");
//set duration for the animation process to 500ms
animation.setDuration(500);
//when the button is clicked. . .
QObject::connect(&button, &QPushButton::clicked, [&]{
//set the starting point of the animation to the current position
animation.setStartValue(button.pos());
//set the ending point to (250, 250)
animation.setEndValue(QPoint(250, 250));
//start animation
animation.start();
});
return a.exec();
}
Qt also provides many examples for using the animation framework. . .

A timer is the best option. If you want to use brute force you can call
qApp->processEvents();
inside your loop. Ugly but gets the job done.

Related

How to close the qt application when setQuitOnLastWindowClosed(false)

I wish to create a project including 3 windows: mainWindow, childWindow1, childWindow2, and only one window should show at a time. And I can switch between these windows.
So I have three tasks:
I place two buttons in the mainWindow and want to use them to make one of the child windows showing and the main window hiding.
And when I close the child window, I wish to show the main window.
When I close the main window, terminate the whole application.
I first had a problem:
If I close the child window, the application exit.
So I use the qApp.setQuitOnLastWindowClosed(false), and I got task 2 done.
But another problem occured:
If I close the main window, the program is still running.
Last problem:
How to show the child window in the task bar? It looks wired to run a program which can't be found in the taskbar.
I search everywhere I could, any help would be really appreciated!
main.cpp:
int main()
{
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
MainWindow mainWindow;
mainWindow.show();
return a.exec();
}
mainWindow.cpp:
void mainWindow::button1Clicked()
{
this->hide();
childWindow1 = new ChildWindow1(this);
connect(childWindow1, &QMainWindow::destroyed, this, &QMainWindow::show);
childWindow1->setWindowModality(Qt::WindowModal);
childWindow1->show();
}
childWindow1.cpp
ChildWindow1::ChildWindow1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
}
To achieve this you need to do the following:
In your main.cpp:
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
MainWindow w;
QObject::connect(&w, &MainWindow::exited, &a, &QApplication::quit);
w.show();
return a.exec();
exited signal would be used to exit the application after closing the main window.
In your MainWindow you should reimplement closeEvent like this:
void MainWindow::closeEvent(QCloseEvent *event)
{
QMainWindow::closeEvent(event);
emit exited();
}
Don't forget to add exited() to MainWindow signals section;
Finally, code for creating new windows should be the following:
child1 = new ChildWindow;
child1->setAttribute(Qt::WA_DeleteOnClose);
connect(child1, &QObject::destroyed, this, &QWidget::show);
child1->show();
hide();
Note that I don't pass this as a parent for a ChildWindow, this allows it to appear in the task bar.
So, creating a new ChildWindow will hide the main window, closing the ChildWindow will delete it automatically (that's another reason why you don't need to pass parent to ChildWindow constructor) and closing MainWindow will close the whole application thanks to our exited signal.

QT 5.6 QVBoxLayout removeWidget then addWidget not working as expected

I searched everywhere and found nothing that solved this. I make a QVBoxLayout and then make a web call for data. When the data comes back I add 4 custom widgets to this QVBoxLayout
verticalLayout->addWidget(nsd);
For the first four this works great. Everything appears as needed. However, I want to delete any one of the four widgets then add a widget at the bottom. Deleting works fine
verticalLayout->removeWidget(nsd);
delete nsd;
I know it works fine because then that widget not longer draws to my screen. The problem is that adding the widget is not working entirely. I call the same code
verticalLayout->addWidget(nsd);
and checking verticalLayout->count() tells me there are 4 items. The widget is created with the same parent widget as the ones added before. The paint event of the new widget never gets called. Furthermore the 3 that show on the screen show spaced for 3 items. It's not like there's a hole anywhere. I also tried adding then deleting but it's the same problem. The new item never gets drawn and its size never factored in.
If you want to get rid of a widget completely, you only need to destruct it. You don't have to worry if it was in a layout. If the widget is dynamically allocated, then delete nsd is all you need, the layout->removeWidget call is not needed. You also don't have to give widgets any explicit parents - insertion into the layout will set proper parent.
The following works and is safe no matter what is the type of the widget being added/removed. If the deletion had target widget on the call stack, you should use deleteLater instead of plain delete. But this can never be the case when you delete it in response to a signal from an unrelated widget, unless the target widget re-enters the event loop (aargh! it shouldn't).
// https://github.com/KubaO/stackoverflown/tree/master/questions/layout-addremove-37814292
#include <QtWidgets>
int main(int argc, char ** argv) {
QApplication app(argc, argv);
QWidget widget;
QVBoxLayout layout(&widget);
QPushButton button;
QLabel label("Hello");
layout.addWidget(&button);
layout.addWidget(&label);
auto onClick = [&]{
if (layout.count() == 3) {
delete layout.itemAt(2)->widget();
button.setText("Add");
} else {
layout.addWidget(new QLabel("Hello too!"));
button.setText("Remove");
}
};
QObject::connect(&button, &QPushButton::clicked, onClick);
onClick();
widget.show();
return app.exec();
}

When I send or post a QMouseEvent, at the position of a QPushbutton, its clicked() signal is not emitted

I try to design a gui for an augmented reality application using the kinect. The idea is, to use the hands detected by the kinect skeleton tracking to control an application via gestures.
This question is not about gestures, as this part of my appilcation works fine.
In my application i want to simulate a mouse click whenever a click gesture is performed. To do this, i am sending two events one, mousebuttonpressed, and one mousebuttonreleased, as a normal click is also a series of press and release.
The whole thing works fine on a QWebView. In the browser window, i can "click" on links.
But for some reason i cannot "click" on a QPushButton. The clicked() signal is not emitted.
I have a short example to illustrate my problem:
First the main function:
int main(int argc, char **argv){
QApplication app( argc, argv );
QWebViewButtons mainapp( 0, Qt::Window );
app.setActiveWindow( &mainapp );
mainapp.show();
mainapp.setApplication( &app ); //this is a setter to also get access to the main application in the widget
return app.exec();
}
This is my own widget:
QWebViewButtons::QWebViewButtons(QWidget* parent, Qt::WindowFlags flags ): QWidget(parent, flags ){
this->m_ui.setupUi( this );
QObject::connect( this->m_ui.pushButton, SIGNAL(clicked(bool)), this, SLOT( buttonClicked(bool) ) );
}
void QWebViewButtons::mousePressEvent( QMouseEvent* event ){
printf("mouse click, %d, %d\n", event->pos().x(), event->pos().y() );
}
void QWebViewButtons::buttonClicked( bool clicked ){
printf("slot button clicked\n");
}
void QWebViewButtons::keyPressEvent( QKeyEvent* event ){
printf("Key pressed\n");
QPoint pos( this->width()/2, this->height()/2 );
QPoint global = this->mapToGlobal(pos);
QWidget *w = this->m_app->widgetAt(global);
//printf("widget under point of click: %s", w);
QApplication::sendEvent( w, new QMouseEvent( QEvent::MouseButtonPress, pos, Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier ) );
QApplication::sendEvent( w, new QMouseEvent( QEvent::MouseButtonRelease, pos, Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier ) );
}
I followed the suggestion here:
Qt Artificial mouse clicks doesnt work properly
and send to send my mouse events directly to the QPushButton. But that didn't help.
I also tried to send the mouse events to "this", or the main app.
Now I am running out of ideas.
What I want to have is, that the buttonClicked() slot is called if i press any key. But i only is called if i click the button with my mouse.
How can i accomplish this? Or is my basic idea completely false?
Thanks for your ideas.
So, do I get that right? When doing your "click-gesture" you come into keyPressEvent? If so, you can check whether the "click" has been done above the button and explicitly call the button's clicked() signal. But that's not what you want?
And what exactly is QWebViewButtons? The area the user does his gestures in?
Have you debugged into the keyPressEvent to see if your sendEvent has the correct widget (w)? I can't see why the widget should not recieve the event...
And remember that when new'ing a QMouseEvent and sending it via sendEvent is is never deleted. When using sendEvent you should create your event on the stack.
Maybe you should have a look at this thread, where a QTestEventList is recommended: Mimicking/faking a Mouse Click and Mouse Wheel using Qt. But I can imagine that you don't want test functions do the trick ;)
Ok, the trick was to really send the click to the EXACT widget at the desired click position. In my application i had some trouble because of semi transparent widgets lying over each other.
So the widgetAt(global) command wouldnt help me.
But you can use childAt(global) instead. Of course you need to know from which widget you want to find the child.
That did the trick for me. :-)

Select text of QLineEdit on focus

I have created a dialog using QtDesigner. There is a QLineEdit object in the dialog with some default content. When the dialog initializes and the focus goes to the QLineEdit, I want the default content to be auto selected, so once the user start writing, the previous content will be overwritten.
EDIT:
In constructor:
dialog->accept();
and
connect( dialog, SIGNAL(accepted()), QlineObj, SLOT( selectAll() ) );
This is an older question, but nevertheless, I ended up here searching for a solution this exact problem. It can be solved in the following way:
Create a class derived from QLineEdit and override the focusInEvent in the header:
void focusInEvent(QFocusEvent *event) override;
Then implement it like so:
void MyLineEdit::focusInEvent(QFocusEvent *event)
{
// First let the base class process the event
QLineEdit::focusInEvent(event);
// Then select the text by a single shot timer, so that everything will
// be processed before (calling selectAll() directly won't work)
QTimer::singleShot(0, this, &QLineEdit::selectAll);
}
Just in case anybody else wonders how this can be done ;-)
Call
lineEdit->selectAll();
after you set the default text. (In the dialog constructor, perhaps.)
There is a simpler method to get almost the same behaviour, which is to set the default content using setPlaceholderText() instead of setText(). This will show the default content grayed out and as soon as the QLineEdit gains focus, it will disappear.
You can use QApplication::focusChanged along with QTimer::singleShot to select all the text on a changed focus.
Normally your QApplication is declared in main, so use QObject::connect there, eg:
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
My_Main_Window w();
QObject::connect(&a, SIGNAL(focusChanged(QWidget*, QWidget*)),
&w, SLOT(my_focus_changed_slot(QWidget*, QWidget*)));
w.show();
return a.exec();
}
Remember to make the public slot in the My_Main_Window class:
public slots:
void my_focus_changed_slot(QWidget*, QWidget*);
Then in your definition of my_focus_changed_slot, check if the second QWidget* (which points to the newly focused widget) is the same as the QLineEdit you wish to select all of, and do so using QTimer::singleShot so that the event loop gets called, then the QLineEdit has the selectAll slot called immediately after, eg
void My_Main_Window::focus_changed(QWidget*, QWidget* new_widget) {
if (new_widget == my_lineedit) {
QTimer::singleShot(0, my_lineedit, &QLineEdit::selectAll);
}
}
where my_lineedit is a pointer to a QLineEdit and is part of the My_Main_Window class.

Please let me know its an functionality behaviour or not

I have a listview filled with items. By default, the 0th item will be selected.
If I try to navigate the list using the mobile keypad, it's not gaining focus - instead I need to use my mobile select key for focus. In this process my mobile left soft key gets changed to “Done”. Why is the "Done" menu appearing?
How do I provide default focus to the listview? And how do I avoid the display of “Done” at left soft key?
Here is my code:
#include "Test_Doco.h"
#include <QtGui>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QListView *listui = new QListView();
listui->setSelectionMode(QAbstractItemView::SingleSelection);
listui->viewport()->setFocusPolicy(Qt::WheelFocus);
listui->viewport()->setFocus();
QStandardItemModel* listModel = new QStandardItemModel();
for(int i =0; i<10;i++)
{
QStandardItem *item1 = new QStandardItem("AOL");
listModel->appendRow(item1);
}
QModelIndex index = listModel->index(0,0);
listui->setCurrentIndex(index);
listui->setModel(listModel);
listui->showMaximized();
return a.exec();
}
Edit: I have updated the code. Please check it.
For the default focus, stop calling listui->viewport()->setFocus(); and call listui->setFocus() to give it focus when it is created.
As for the display of "Done", I'm not too sure, but you might need to post more code to show the dialog you are creating. Most have a set of default buttons or a button set to default. The "Done" key might be related to that. As seen here "Exit" is the softkey shown.
The issue is w.r.t Qt 4.6.2 and the issue is fixed in Qt 4.6.3

Resources