Qt signals and slots working - qt

I have created a Main Window which consists of menu bar with the first menu being "file". Now I am trying to open another window if I click on the "file" menu item. But I am not getting the receiver object of the other window while designing. How can I establish a link?
I am using Qt 4.7

I don't think you can do it by manual linking - this time you will have to write it yourself :P
Simply add a new slot to your window's class, add include for second's window header. Then in slot implementation create new window if necesary and simply call show on it.
Secondly you have to connect file meny to your signal - best is to add connect call in your window constructor AFTER intializing ui.
Also note that if your file option contains actually any submenu, then it's signal will never be emmited, so you need to think about something else in such case.

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

Qt Popup as a completer window

I need to make some kind of popup window that contains propositions to complete sentences in text editor (QTextPlainEdit). This window needs to be on top of all windows of this application. Also this popup mustn't interrupt typing in the text editor when it appears. I tried different types of flags for QWidget that implements this completer but all I have got is that this completer window is placed above all windows of OS (even if this application is not active) or it interrupts typing in the text editor and makes main window not active any time it appears.
What flags should I use for completer widget?
You could try to use QWidget::setWindowFlags(Qt::Window | Qt::FramelessWindowHint).
Otherwise you could use a customized version of Qt::Popup, by overriding the automatic closing behavior.
You could also try this: if you set the QTextPlainEdit's parent as the completer's parent it should do what you want, provided that the parent does not have a layout (otherwise it will not "float").
The Qt docs contain an example that implements a google-based auto-completer widget, here: http://qt-project.org/doc/qt-4.8/network-googlesuggest.html.
As far as I can tell, they do two things that might be relevant to your situation. One is the flags they set on the popup widget:
popup = new QTreeWidget;
popup->setWindowFlags(Qt::Popup);
popup->setFocusPolicy(Qt::NoFocus);
popup->setFocusProxy(parent);
The other is a custom event filter on the popup widget, which forwards most keypress-events to the editor widget, and closes the auto-completer on Enter or Escape.

qt create dialog

I've made my first qt window. Now I'd like to make my first dialog, using qt. I have just finished creating the dialog, which is basically made of a QDialogButtonBox, and now I'd like to connect it to the window. I have two beginner's questions:
How can I retrieve how the dialog was closed (ok pressed or cancel pressed) from the window.cpp, which creates a new dialog, and then calls dialog->show() ?
Where and how to destroy the dialog pointer ?
If you use dialog->show() then I assume it's non-modal dialog.
If you have created QDialogButtonBox and connected its signals with accept() and reject() slots of your dialog as documentation shows, then your dialog will emit finished(int) and additionally accepted() or rejected() signals by which you can determine how it was closed.
If you need more customized behavior, then you can reimplement closeEvent(QCloseEvent *event) or create your own singnals.
If you need to delete your dialog you can use setAttribute(Qt::WA_DeleteOnClose, true);, which will delete instance on close.
you can use one flag, and signal and slot.
when put OK flag=1 , and when put cancel then flag=-1; and then use signal.
in in the window.h write code how to handle that flags with 1 simple slot.
for destroying the pointer you can use signal and slot in your Dialog and tell when user push
Ok, or Cancel , or exit (up- right (red cross)) go to slot in call the Destructer of dialog
and also you that you better set parent of dialog to window.
First Question:
When you want to show the dialog,just construct it,using myDialog *d = new myDialog(this)(the this pointer will make sure that you havn't to delete the pointer you created 'cause Qt will handle this if you specified the dialog's parent). And use d->exec() if you need a modal dialog, or d->show() to make it non-modal;
Second Question:
Once your specified the dialog's parent object, all u need is just use it and leave alone the memory managent,Qt will do this for you. Also you can use d->setAttribute(Qt::WA_DeleteOnClose,true) to make it destroy itself when it is closed.
Remember to link the QDialogButtonBox to your dialog's actions.

Destroy QWidget returned from Plugin in Qt

My app loads a plugin which creates a window (QWidget), but I can not destroy it when I exit from QMainWindow, obviously because widget returned from plugin is not a child of QMainWindow. The issue is that if I make that window to be a child of mainwindow, I get a window on another window. But I need them both to be separated. I did them separated (but main window has no control over window from plugin), in my case I do not know how to close window from plugin when app quits. How can I achieve that?
Just delete it. If you don't assign it a parent, no other widget has ownership. So you should simply be able to destroy it yourself on exit.

QSystemTray problems

I am trying to use QSystemTrayIcon for my application and i am facing some problems.
It is the first time i use qt so i am not really used to it.
I followed this tutorial to make a system tray icon but i fail to customize it.
I want to have a button show/hide and not 3 show, hide, restore. These actions are really confusing for a newbie and i dont know what to do and what to connect.
I tried some things but with no luck.
Also when the system tray menu appears if you click somewhere else, the menu stays open.
Any way to solve this thing too?
If you want to remove one of the menu items, modify the createTrayIcon function so that it only adds the actions you need (and clean up the unused members once you get it to work). It's that simple.
If you want a single menu item or button to toggle between visible and hidden, you'll need to create a custom slot that calls show() or hide() (or setVisible(bool)) depending on whether the widget is hidden or not (use isVisible() for that for example). Then connect your action to that slot.
Read the Signals and Slots documentation and examples for information about how to create a new slot.

Resources