Popup window Mac OS in Qt - c++

Can I make on Qt is popup window?
I do not know exactly how it is called.

You can use a QSystemTrayIcon. That creates a tray icon in the taskbar, which is where the notification will come from. To show the notification, use the showMessage method. This method takes two QString parameters, the first one being the title and the second one being the message. For example, the following code shows a "Hello World!" message with "Title" as title:
QSystemTrayIcon *trayIcon = new QSystemTrayIcon(button);
trayIcon->setIcon(QIcon("image.png"));
trayIcon->show();
trayIcon->showMessage("Title", "Hello World!");
You must set the tray icon's icon by using the setIcon method, otherwise it won't work. You also have to show the tray icon with the show method. You can also do something when the message is clicked on with the messageClicked signal.
Here is a complete example of a program where clicking on a button will show the message, and clicking on the message will show a normal message box:
#include <QApplication>
#include <QtWidgets>
int main(int argc, char **argv){
QApplication app(argc, argv);
QPushButton button("Show message");
QSystemTrayIcon trayIcon;
QObject::connect(&button, &QPushButton::clicked, [&trayIcon](){
trayIcon.showMessage("Title", "Hello World!"); //Show the popup when the button is clicked on
});
QObject::connect(&trayIcon, &QSystemTrayIcon::messageClicked, [](){
QMessageBox::information(nullptr, "", "Message Clicked"); //Show a message box when the popup is clicked on
});
trayIcon.setIcon(QIcon("image.png")); //Set the tray icon icon to image.png
trayIcon.show(); //Show the tray icon
button.show(); //Show the button
return app.exec();
}
Here is the result on Windows:
And here is the result on Mac:
I've circled the tray icon on both screenshots so that you can see what a tray icon looks like. Try clicking on the popups to see what happens. You can play around with the code to change the messages and the image to really understand how the code works.

You can use QMenu, QActions and the TrayIcon to do this.
void Window::createTrayIcon(){
TrayIconMenu = new QMenu(this);
TrayIconMenu->addAction(MinimizeAction);
TrayIconMenu->addAction(MaximizeAction);
TrayIconMenu->addAction(RestoreAction);
TrayIconMenu->addSeparator();
TrayIconMenu->addAction(QuitAction);
TrayIcon = new QSystemTrayIcon(this);
TrayIcon->setContextMenu(TrayIconMenu);
}
I'm not sure how to get the exact format of that rounded bubble though. That might not be a feature Qt has.

Related

How to set an icon to a new button in QDialogButtonBox?

I'm trying to make a custom button with an icon in QDialogButtonBox in a Qt5 program (Qt 5.5.1) on Kubuntu 14.04. Reading this answer, I understand that it's not recommended to simply edit the stock buttons, so I use QDialogButtonBox::addButton with my custom button. But for some reason, although I do succeed in changing icon of the stock button, my custom button still remains without any icon after using QPushButton::setIcon.
Here's the code
#include <QApplication>
#include <QDialogButtonBox>
#include <QPushButton>
int main(int argc, char** argv)
{
QApplication app(argc,argv);
QDialogButtonBox bbox(QDialogButtonBox::Close);
const auto button=new QPushButton(QObject::tr("Convert..."));
bbox.addButton(QObject::tr("Convert..."), QDialogButtonBox::ApplyRole);
const auto icon=QIcon::fromTheme("system-run");
// This doesn't work - the button remains without icon
button->setIcon(icon);
// But this does
bbox.button(QDialogButtonBox::Close)->setIcon(icon);
bbox.show();
return app.exec();
}
What am I doing wrong? How can I have an icon on my custom button without the need to edit stock buttons?
You are creating two buttons. One button which you don't show anywhere, and one that you add in bbox. And you are setting the icon for the button that you don't show anywhere.
Fix your code so you don't create two separate buttons, and set an icon for the button you are putting in bbox:
QPushButton *button = bbox.addButton(QObject::tr("Convert..."), QDialogButtonBox::ApplyRole);
...
button->setIcon(icon);

implementing custom completer, popup window steal the focus

I need to implement custom completer, similar to QCompleter but containing customized widgets. I got stuck at the very beginning, I can't make the popup work as it should work. In the following example I attach a completer (well, just a plain widget) to the first line edit. The second line edit is there just to test the focus in/out behavior. The problem is that when the popup displays, it steals the focus from the line edit. But this is not what I want, I want to be able to keep typing in the line edit. I tried several other options as commented out in the code below, but non of them worked.
#include <QApplication>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget container;
QLineEdit editor;
QLineEdit editor2;
QHBoxLayout layout(&container);
layout.addWidget(&editor);
layout.addWidget(&editor2);
QWidget completer; // or QWidget completer(&editor);?
//completer.setFocusProxy(&editor); // tried this, but it does not help
completer.setWindowFlags(Qt::Popup);
// the following lines are an alternative to Qt::Popup but the window
// does not close automatically, which is a problem
// e.g. when moving or resizing the parent window
//completer.setAttribute(Qt::WA_ShowWithoutActivating);
//completer.setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
QObject::connect(&editor, &QLineEdit::textEdited,
[&] {
completer.resize(editor.width(), 100);
completer.move(editor.mapToGlobal(QPoint(0, editor.height())));
completer.show();
// editor.setFocus(); // does not help either
});
container.show();
return a.exec();
}
How to implement custom completer?
UPDATE: From the docs, I read that "A popup widget is a special top-level widget that sets the Qt::WType_Popup widget flag, e.g. the QMenu widget. When the application opens a popup widget, all events are sent to the popup. Normal widgets and modal widgets cannot be accessed before the popup widget is closed."
So it seems that when I open a popup it automatically receives the events and therefore I have to forward the events to the line edit using QCoreApplication::sendEvent(editor, event); inside my Completer::keyPressEvent. This seems to work fine except the fact that the cursor in the line edit is not visible or not blinking.
Just apply this method to your completer widget:
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
It works for me.

Non-modal QWidget dialog that stays on top of the window

I want a dialog which stays on top of my main window and not other windows. I derived a class and added some flags. If I call the dialog now with show() the dialog appears and is staying on top as long as I don't press a button or whatever. Then the dialog goes to background again.
Dial::Dial(QWidget *parent) : QWidget(parent)
{
this->setWindowFlags(Qt::Tool | Qt::Dialog);
// ...
Consequently, I looked into the docu and found this:
Indicates that the widget is a tool window. A tool window is often a
small window with a smaller than usual title bar and decoration,
typically used for collections of tool buttons. If there is a parent,
the tool window will always be kept on top of it.
Happily, I added this line into my singleton creating the dialog.
d->mainWindow = new Foo();
d->dial->setParent(d->mainWindow);
Now the dialog is just embedded into my central widget (QOpenGlWidget) and is not a dialog anymore. Somehow, I seem to lack understanding what the docu is telling me? How can I get the dialog stay on top of my application and what does the docu mean?
I'm not able to reproduce your problem. The following code will generate a QWidget that will allways stay on top of the QMainWindow:
#include "QApplication"
#include "QMainWindow"
#include "QLineEdit"
int main(int argc, char * argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.show ();
QWidget *pLineEdit = new QWidget(&w);
pLineEdit->setWindowFlags(Qt::Tool | Qt::Dialog);
pLineEdit->show ();
a.exec ();
}
Tested with Qt 5.9.
Not sure if you've already solved this by now but you can try the WindowStaysOnTopHint flag when you construct the dialog:
Qt::WindowFlags flags = this->windowFlags();
flags |= Qt::WindowStaysOnTopHint;
this->setWindowFlags(flags);
Then use show() instead of exec() to make it non-modal:
dlg->show();
You need to set the modality (documentation) of the widget, like this:
QWidget *dialog = new QWidget(window, Qt::Dialog);
dialog->setWindowModality(Qt::ApplicationModal);
dialog->show();
However, I'd recommend to use the pre-configured QDialog class, which handles all that stuff for you:
QDialog *dialog = new QDialog(window);
dialog->exec();
Use QDialog instead of QWidget, and pass the parent widget in its constructor function.
QDialog* pDlg = new QDialog(this);
pDlg->show();

Qt4 login window

I am writing a login window in Qt.
When the users clicks on OK, it should close the login window, show a "Connecting to server..." Widget, and open the main window once the connecttoserver method has done its job.
However, the widget appears only when the main window is shown, and disappears immediately (it shouldn't even close!)
How do I solve this issue ?
void LoginWindow::blah()
{
close();
QWidget widget;
widget.show();
//calls to the "connecttoserver method"
Main *main = new Main(student->getInfo()[0], student->getInfo()[1], student->getInfo()[2], view);
main->show();
}
}
QWidget is declared as an automatic on the stack so it is destroyed when the method returns. You want to declare it on the heap instead:
QWidget *widget = new QWidget();
widget->show();
In addition to atomice's answer, make sure you have set your application's quitOnLastWindowClosed to false, or else your application will terminate between closing your login window and opening your main window.

Set QLineEdit focus in Qt

I am having a qt question. I want the QLineEdit widget to have the focus at application startup. Take the following code for example:
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QFont>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
window->setWindowIcon(QIcon("qtest16.ico"));
window->setWindowTitle("QtTest");
QHBoxLayout *layout = new QHBoxLayout(window);
// Add some widgets.
QLineEdit *line = new QLineEdit();
QPushButton *hello = new QPushButton(window);
hello->setText("Select all");
hello->resize(150, 25);
hello->setFont(QFont("Droid Sans Mono", 12, QFont::Normal));
// Add the widgets to the layout.
layout->addWidget(line);
layout->addWidget(hello);
line->setFocus();
QObject::connect(hello, SIGNAL(clicked()), line, SLOT(selectAll()));
QObject::connect(line, SIGNAL(returnPressed()), line, SLOT(selectAll()));
window->show();
return app.exec();
}
Why does line->setFocus() sets the focus on the line widget #app startup only if it is placed after laying out the widgets and if used before it's not working?
Keyboard focus is related to widget tab order, and the default tab order is based on the order in which widgets are constructed. Therefore, creating more widgets changes the keyboard focus. That is why you must make the QWidget::setFocus call last.
I would consider using a sub-class of QWidget for your main window that overrides the showEvent virtual function and then sets keyboard focus to the lineEdit. This will have the effect of always giving the lineEdit focus when the window is shown.
Another trick that might work is by using the singleshot timer:
QTimer::singleShot(0, line, SLOT(setFocus()));
Effectively, this invokes the setFocus() slot of the QLineEdit instance right after the event system is "free" to do so, i.e. sometime after the widget is completely constructed.
Perhaps this is an update as the last answer was in 2012 and the OP last edited the question in 2014. They way I got this to work was to change the policy and then set the focus.
line->setFocusPolicy(Qt::StrongFocus);
line->setFocus();
In Qt setFocus() is a slot, you can try other overloaded method which takes a Qt::FocusReason parameter like the line shown below:
line->setFocus(Qt::OtherFocusReason);
You can read about focus reason options in the following link:
http://doc.trolltech.com/4.4/qt.html#FocusReason-enum