How to create custom shortcut with Qt? - qt

In my app there is button that when you click on it the main window will be hid then if you type a shortcut the main window will show again.
But the shortcut doesn't work.
Here's my code for the shortcut:
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_K), this);
QObject::connect(shortcut, &QShortcut::activated, this, &MainWindow::openWindow);

For adding shortcut you set its parent this.
If you close or hide MainWindow means that the shortcut couldn't work because its parent was not called or found.
When you close or hide the window, another screen like your desktop is displayed, so the shortcut does not work.
For example if you write this code :
shortcut = new QShortcut(QKeySequence(tr("Ctrl+k", "Open/Close window")), this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::hide);
when you press Ctrl+k you see that your main window will hide and the shortcut works correctly.
but after that, your desktop will appear and its shortcut will work.

Related

How to enable the menubar in a QUndoView window in Qt?

I use a QUndoStack and I want to show its content using a QUndoView. I create the view as follows:
undoView = new QUndoView(&_undoStack);
undoView->setWindowTitle(tr("Undo Stack"));
undoView->show();
The view is created in a separate window (which is what I want), but that window has a disabled menu bar, so I cannot move it at all, or close it. Is there a way to enable it?
Ok so the issue was that the main window had a window modality of Qt::WindowModality::ApplicationModal which blocks all input to any other top-level window. Simply changing the main window to non-modal with SetWindowModality fixed this.
hide();
setWindowModality(Qt::WindowModality::NonModal);
show();
Alternatively change the modality directly in Qt creator if the main window was created from there.

How can I tell QWidget to show in the same place that it was before it was hidden?

If I create and show a top-level QWidget, drag it to a new position on the desktop, then call widget->hide() followed by widget::show(), it generally reappears in a different place from where it was previously.
I can add code to the subclass which saves and restores its geometry, but I'm wondering if there's an in-Qt system for giving hints to the window manager as to where the widget should appear when shown.
Is there a nice way to do this?
I create a "Qt Widgets Application" and drag in a QPushButton, then add the following code:
void Widget::on_pushButton_clicked()
{
hide();
show();
}
Run the program, drag the widget to a new position, and then click on the button and I find that the widget is still in the same place, not like you said

How to remove bar from QMainWindow

I'm trying to delete this bar, butI can't get rid of it (it's locate just under the toolbar):
What is the name of that bar,how can I access it?
Thank you.
What you are calling the toolbar is actually the menu bar and what you are calling the other bar is actually an emtpy toolbar.
The most likely reason you have an empty toolbar is because you created your window using QtDesigner. If you choose a QMainWindow as your starting point, it automatically adds an empty menubar and an empty toolbar to the window. If you don't want the toolbar, find it in the Object Inspector on the right-hand side, right-click and select Remove Toolbar 'mainToolbar' (or whatever other name is the default).
If you added that tool bar you probably have a pointer to it? If yes, you can simply call:
removeToolBar(toolbar);
in your QMainWindow class. Otherwise you can remove all tool bars from the main window as:
QList<QToolBar *> allToolBars = mainWindow->findChildren<QToolBar *>();
foreach(QToolBar *tb, allToolBars) {
// This does not delete the tool bar.
mainWindow->removeToolBar(tb);
}
Below adds a little to #RobbieE's answer.
When creating a QMainWindow form, it creates mainToolBar for the user.
If you right click on it and select Remove Toolbar 'mainToolBar' it will be gone.
Or in code in the top of your constructor:
ui->setupUi(this);
delete ui->mainToolBar; // add this line
Hope that helps.

How can I create a home button?

I am pretty new in the programming world. I am using Titanium, and I want a button that when I clicked on it, it will go back to the home window. Obviously, the button will be in another window.
I tried win2.open() and win2.show(), but it is not working. Any ideas?
Use titanium navigational controller
https://github.com/vuinguyen/Ti-Navigation-Controller
it would give you button for previous and home window
Jacl,
If you are in second window, just close that window on the click event of your button in the second window.
Try the following in second window
//I'm assuming that you have came from window1 to window2
//and you have a button btnHome at window 2 used to navigate back to home page
var thisWindow = Ti.UI.currentWindow;
btnHome.addEventListener('click', function(e){
thisWindow.close(); //Will close the current window and display the previous one
});
There is a similar question which is used to navigate between windows. Go through Titanium Mobile: cant navigate between pages.
You can use the following links also for references
http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.Window-property-url
http://docs.appcelerator.com/titanium/latest/#!/api/Global-method-require

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.

Resources