Qt negative button not working - qt

In my Qt symbian app, I have over ride the negative exit button with back to come back on main screen and then again over ride it with exit to close the app, my app is working fine on emulator but when I test it on device it shows exit button instead of back, some time it shows back also but if I go to the same page twice then again it start showing exit button, frustrating part is that app is working fine on Qt emulator but not on device. Does somebody knows whats the problem is. I am using
back->setSoftKeyRole(QAction::NegativeSoftKey);
this->addAction(back);
to over ride the exit button before loading the screen and
back->setSoftKeyRole(QAction::NegativeSoftKey);
this->removeAction(back);
to removing back button when coming back to mainWindow.

Create vertical layout and Widget which You will add on scrollarea with parent as that class this e.g
QVBoxLayout *vlay = new QVBoxLayout(this);
QWidget *area = new QWidget(this)
And add widget to it
This will make it child of parent class.
works fine for me.

I think adding and removing QAction objects here and there is asking for problems.
You should try to redesign your application to use a QStateMachine to handle transitions between states.
Take a look at the introductory documentation here.

Refer this LINK for custom softkeys..
QAction* myAction= new QAction(tr("My Action"), this);
myAction->setSoftKeyRole(QAction::NegativeSoftKey);
addAction(myAction);

Related

Qt is activating window without my consent after using QFileDialog

Qt 5.12.6 MSVC2017 64-bit on Windows 10, Qt Creator 4.10.1.
I have a top level window derived from QMainWindow, which has a QMdiArea.
From the first main window I use a file open dialog to open a new document in a QMdiSubWindow and place it in the QMdiArea. After the file dialog returns but before I do the logic of opening the document, I have to call QApplication::setActiveWindow(this), to set it back to the main window, because the opening logic depends on the active window. The need for this somewhat appears related to https://www.qtcentre.org/threads/2950-ActiveWindow-changes-after-closing-QFileDialog in that the activeWindow becomes null, regardless of how I parent the file dialog.
I then open a second identical top level window and open another document using the same method.
What's very weird is that after the second QMdiSubWindow in the second QMainWindow is activated, the first QMainWindow gets activated again, and thus various follow-on operations affect the document in the first window rather than the second window which is the intended one. If I click back and forth on the main windows, the activation is correct.
If I create a new document directly from the second QMainWindow instead of using the file open dialog, then it works fine. The new QMdiSubWindow is created and shown in the second QMainWindow, without the first one getting activated. So this implicates the file dialog somehow.
When I put a breakpoint on a function triggered by the QMainWindow::activated signal, the stack trace shows this signal is coming from within Qt on its own accord, not from someplace in my code directly.
How can I find out why Qt is activating the first window when using the file dialog from the second window? At this point the code is too large to try to fit a minimal example here on SO.
--EDIT--
I added a one-shot QTimer at the end of the function that creates the QMdiSubWindow. Zero milliseconds after in creates the QMdiSubWindow it activates the first QMainWindow, then activates the current QMainWindow. That workaround seems to work, and the second QMainWindow becomes the active one. But only if I select away from it first. The fact that the hack works with 0 ms timer is also interesting. I'll be posting minimal example when I can.
thanks

PyQt5 QMenu no focus on exec_()

I have an already fully functioning app, with a custom widget that opens a custom QMenu when clicked, by calling exec_():
menu.exec_(QPoint)
I recently migrated from PyQt4->PyQt5 and noticed that the focusOutEvent method I was overriding was no longer responsive. I already have a fix, which works just fine:
menu.setFocus() #calling this prior to exec_()
Nevertheless, I would like to understand what exactly is different after the Qt migration, that I had to manually set the focus in order to keep desired functionality.
The menu is set to have StrongFocus.
Another apparent issue that might be related is that the setting of the cursor to hand fails to change back to regular cursor when the cursor leaves the menu rect.
Thank you! =)

Detecting click outside QWidget

My application has non-rectangular popup widgets.
their class defines the following to achieve that transparency:
setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_TranslucentBackground, true);
I also use:
this->setWindowFlags(Qt::Popup| Qt::FramelessWindowHint);
The problem is that on windows 7, an automatic "shadow" is being drawn on the bottom and right sides of my window. This is highly undesirable.
So, I tried using Qt::Tool instead of Qt::Popup
this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
This works visually. No shadow, but now a mouse click outside my widget window will not automatically close/hide it as it would have done with a Qt::Popup.
So, I need one of these two solutions:
A way to have Qt::Popup and get rid of that automatic windows shadow decoration
A way to let the Qt::Tool window a mouse click occurred outside of it.
One note: My application is built for Windows XP and up. I cannot use a Vista/Win7 only runtime DLLs, nor can i have a "Windows XP" and "Vista and up" separate builds.
Any advice will be welcome.
You could manually watch for when the focus changes from your Qt::Tool window. So basically you watch for when the focus goes onto another window of your process or when your application loses focus.
How to detect that my application lost focus in Qt?
Hope that helps.
Finally after realizing that no amount of "SetFocusPolicy" calls will allow me to receive those events for a Qt::Tool window, I've resorted to something else to fix my problem:
I kept the Qt:tool as Qt::Popup caused an undesired shadowing effect, tarnishing my owner draw frame. Removing this style cannot be done in Qt and I didn't want to mess with platform specific conditional code.
I installed an event filter with my Qt::tool window and I began receiving events that assisted me in understanding when other parts of my application were clicked, or if the application itself lost focus to another application. This was what I needed, functionality wise. I could also get an event when users click the non-client areas of the application's main window, such as the windows caption so that I can close it when dragging begins etc.
My solution for Windows 7:
QDialog *d = new QDialog;
d->setStyleSheet("background:transparent;");
d->setAttribute(Qt::WA_DeleteOnClose, true);
d->setAttribute(Qt::WA_TranslucentBackground, true);
#ifdef Q_OS_WIN
d->createWinId();
#endif
d->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
d->show();
I set my QListView
d->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
Install eventfilter and used MousePressEvent to hide qlistview widget.
MousePressEvent on list never comes to filter they produce other events which I didn't debug.
So if you want to design auto completer this will be perfect. Tested in Qt5.3.1.

Another Window over QMainWindow in Qt

Can i have another window over main window in Qt , and how can i implement it? I have a plugin which must return another window. I created in plugin a QWidget and set it as centralWidget but my app crashes.Anyway this will not show two windows at same time . Could someone explain how to do that ?
Any new widget created without a parent will show up as a new window. Don't try to reset 'centralWidget' unless you don't actually want the old one anymore.
If all you want is a main window whose contents change, take a look at StackedWidget.

Launching new form by a button click in qt gui application?

I am trying upon some simple applications. I would like to know how to launch a new form by clicking a button from main window. Please explain this to me with a simple example.
QT already comes with a simple example on how to create and show different types of windows and dialogs. Take a look here: Window Flags Example
Also if you already have a main widow in your application, you can take a look how it's shown (main function in your main.cpp) for your project and use the same technique:
MainWindowTest *testWindow = new MainWindowTest();
testWindow->show();
If you need to show a modal dialog, use your dialog's exec() method:
Dialog *dialog = new Dialog();
dialog->exec();
hope this helps, regards

Resources