I believe that there is no native functions in QtWebKit to use SpellChecker.
Is there any way to get the fields (<textarea>, <input> and <tagName contenteditable=true>) of the current page and highlight specific words (add underline only words possibly wrong)?
[edit]
I need to add "visual effect" (underline) to the Words and not the dom elements, for example if I have a html like this:
<textarea>Helllo world!</textarea>
only the word "Helllo" will be underlined, example:
Thank you.
UPDATE:
No, as far as I can tell from poking and prodding at QtWebKit, you can't format the contents of a textarea.
Format text in a <textarea>?
You could maybe replace it with a div that looks and acts like a textarea, and then insert some tags around specific words.
But near as I can tell from working on this question, it isn't possible with QWebElement.
You could go and ask the trolls and see if they have any suggestions.
Here is the code that got the closest. When the webpage comes up, right click on different places on the page.
main.cpp
#include <QApplication>
#include "webview.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
WebView w;
w.show();
return a.exec();
}
webview.h
#ifndef WEBVIEW_H
#define WEBVIEW_H
#include <QWebView>
#include <QContextMenuEvent>
class WebView : public QWebView
{
Q_OBJECT
public:
explicit WebView(QWidget *parent = 0);
~WebView();
public slots:
void contextMenuEvent(QContextMenuEvent *);
private:
};
#endif // WEBVIEW_H
webview.cpp
#include "webview.h"
#include <QWebFrame>
#include <QWebElement>
#include <QWebElementCollection>
#include <QDebug>
WebView::WebView(QWidget *parent) :
QWebView(parent)
{
this->setUrl(QUrl("http://www.w3schools.com/tags/tag_textarea.asp"));
// right click on different parts of the web page
}
WebView::~WebView(){ }
void WebView::contextMenuEvent(QContextMenuEvent * cme)
{
QPoint pos = cme->pos();
QWebHitTestResult whtr = this->page()->frameAt(pos)->hitTestContent(pos);
QWebElement we = whtr.element();
if(we.isNull())
{
qDebug() << "WebElement is null.";
}
else
{
qDebug() << we.tagName() << "OuterXML <<<<<" << we.toOuterXml() << ">>>>>";
qDebug() << we.tagName() << "InnerXML <<<<<" << we.toInnerXml() << ">>>>>";
qDebug() << we.tagName() << "PlainText <<<<<" << we.toPlainText() << ">>>>>";
// TODO: replace the lines below with a better way of extracting words from the DOM and the HTML tags
// This current method modifies tags instead of just the text inside tags.
QStringList list = we.toPlainText().split(' ');
for(int i = 0; i < list.size(); i++)
{
// TODO: Insert dictionary logic here when examining words
if(list.at(i).size() > 5)
{
list[i] = "<span class=\"sp\" style=\"border-bottom: 1px dotted red;\">" + list.at(i) + "</span>";
}
}
qDebug() << list.join(" ");
we.setInnerXml(list.join(" "));
qDebug() << "-------------------------------------------";
}
}
So after some additional research (after some changes to your question) here is the method I would look into:
When a right click is performed on the page, the QWidget (your QWebView) sends out a QContextMenuEvent.
http://qt-project.org/doc/qt-4.8/qcontextmenuevent.html#details
Get the position out of that event, and then drill down into your web page to find out what it was for:
Get the web frame at the click location...
QWebFrame * QWebPage::frameAt ( const QPoint & pos ) const
Do a hit test content at the location...
QWebHitTestResult QWebFrame::hitTestContent ( const QPoint & pos ) const
Query the element out of the hit test...
QWebElement QWebHitTestResult::element () const
Double check that the content is editable by the user...
bool QWebHitTestResult::isContentEditable () const
But that is about as far as I have gotten on this research project.
If you restyle the webelements on the page to have a span tag of your misspelled class, then you could look for those and then either create your own popup menu, or edit the contextMenu of the QWebView right then.
So, no, there is not a context menu for elements on the page, but you can simulate it, if you find the element that is clicked on, and then change the contextMenu for the QWidget.
http://qt-project.org/doc/qt-4.8/webkit-simpleselector.html
void Window::on_elementLineEdit_returnPressed()
{
QWebFrame *frame = webView->page()->mainFrame();
QWebElement document = frame->documentElement();
QWebElementCollection elements = document.findAll(elementLineEdit->text());
foreach (QWebElement element, elements)
element.setAttribute("style", "background-color: #f0f090");
}
http://qt-project.org/doc/qt-4.8/mainwindows-menus.html
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(cutAct);
menu.addAction(copyAct);
menu.addAction(pasteAct);
menu.exec(event->globalPos());
}
Again, I hope that helps.
As far as awesome examples of using QWebKit, there is this project:
How to create Web History for my Browser
https://code.google.com/p/arora/
I did a search on the repo for "spell" and "spellcheck" and nothing useful came up.
As far as tweaking the html on the fly, the Fancy Browser example shows how it's done:
http://qt-project.org/doc/qt-4.8/webkit-fancybrowser-mainwindow-cpp.html
void MainWindow::rotateImages(bool invert)
{
QString code;
if (invert)
code = "$('img').each( function () { $(this).css('-webkit-transition', '-webkit-transform 2s'); $(this).css('-webkit-transform', 'rotate(180deg)') } )";
else
code = "$('img').each( function () { $(this).css('-webkit-transition', '-webkit-transform 2s'); $(this).css('-webkit-transform', 'rotate(0deg)') } )";
view->page()->mainFrame()->evaluateJavaScript(code);
}
They just use jquery and run additional javascript on a loaded page.
So you could probably substitute "$('img').each( for "$('textarea').each( and act on the text of an area.
You could also run a regex on the page or use an html parser to find all the <textarea> blocks.
Hope that helps.
Related
Ok, so I want to customize the height and the position of icons of the tabs from a QToolBox widget. I am not able to do it using stylesheet (I tried different options, but none worked, see question: Customizing QToolBox: tab height), so I decided to give it a try to using a proxy. I know there are the controlElements:
CE_ToolBar
CE_ToolBoxTabShape
CE_ToolBoxTabLabel
And so I can use them on the drawControl function to get what I want.
void drawControl(ControlElement oCtrElement,
const QStyleOption *poStyleOptionption,
QPainter *poPainter,
const QWidget *poWidget) const
But I am not sure how to implement it... Any help or example?
Thanks,
UPDATE:
I have created the proxy, assigned it to the qtoolbox and added conditions to see if I could do something:
...
ToolBoxProxy* tabStyle = new ToolBoxProxy();
ui->toolBox->setStyle(tabStyle);
...
toolboxproxy.h
#include <QProxyStyle>
#include <QPainter>
class ToolBoxProxy : public QProxyStyle
{
public:
explicit ToolBoxProxy();
void drawControl(ControlElement oCtrElement, const QStyleOption * poStylrOptionption, QPainter * poPainter, const QWidget * poWidget = 0) const;
};
toolboxproxy.cpp
#include "toolboxproxy.h"
#include <QDebug>
ToolBoxProxy::ToolBoxProxy(){
}
void ToolBoxProxy::drawControl(ControlElement oCtrElement, const QStyleOption *poStyleOptionption, QPainter *poPainter, const QWidget *poWidget) const
{
if (oCtrElement == CE_ToolBar) {
qDebug() << "CE_ToolBar";
} else if (oCtrElement == CE_ToolBoxTabShape) {
qDebug() << "CE_TOOLBOXTABSHAPE";
} else if (oCtrElement == CE_ToolBoxTabLabel) {
qDebug() << "CE_ToolBoxTabLabel";
}
QProxyStyle::drawControl(oCtrElement, poStyleOptionption, poPainter, poWidget);
}
I don't know why, but the software never prints out any of the 3 qDebug()... but if I set a breakpoint, it only reaches the function when the control element is any of those:
CE_ItemViewItem
CE_ShapedFrame
CE_ToolButtonLabel
I would like to do certain updates when the text contents change or font or alignment change on QGraphicsTextItem.
So I connected QTextDocument::contentsChanged() to a slot that does the update.
This signal is emitted whenever the document's content changes; for
example, when text is inserted or deleted, or when formatting is
applied.
The signal gets hit when I change the text - but setting text formatting or alignment doesn't seem to affect it.
.h
class MyTextItem : public QGraphicsTextItem
{
Q_OBJECT
public:
MyTextItem();
~MyTextItem() {}
void setItemFont(QFont f);
void setItemAlign(Qt::Alignment a);
private slots:
void updateItemOnContentsChanged();
private:
void updateTextOnPropertyChanges();
};
.cpp
MyTextItem::MyTextItem()
{
setTextInteractionFlags(Qt::TextEditorInteraction);
connect(document(), SIGNAL(contentsChanged()), this, SLOT(updateItemOnContentsChanged()));
}
void MyTextItem::setItemFont(QFont f)
{
setFont(f);
}
void MyTextItem::setItemAlign(Qt::Alignment a)
{
QTextDocument *_document = document();
QTextOption _option = _document->defaultTextOption();
_option.setAlignment(a);
_document->setDefaultTextOption(_option);
setDocument(_document);
}
void MyTextItem::updateItemOnContentsChanged()
{
updateTextOnPropertyChanges();
}
void MyTextItem::updateTextOnPropertyChanges()
{
qDebug("changing something");
}
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene s;
QGraphicsView view(&s);
s.setSceneRect(-50, -50, 500, 500);
view.show();
MyTextItem* t = new MyTextItem();
t->setPlainText("Hello World !"); // Note this triggers update
s.addItem(t);
qDebug("1");
qDebug() << t->font().family();
t->setItemFont(QFont("Arial")); // or t->setFont(QFont("Arial"));
qDebug() << t->font().family();
qDebug("2");
t->setItemAlign(Qt::AlignRight);
qDebug("3");
return app.exec();
}
My debug output:
changing something
1
"MS Shell Dlg 2" // so the font is changing - yet no signal
"Arial"
2
3
(and if I type in the item on the scene I get changing something as well)
But no changing something after the setItemFont() or setItemAlign().
So changing text affects it - but changing font or alignment doesn't...
Do I understand wrong the part I set bold in the signal description ?
Why do I not see changing something after changing font or alignment...
I also wonder - does my changing the document() when I set alignment or wrap or other document properties affect the connect ? (it doesn't seem to...)
(Note I wrote the setItem* functions to be able to call the updateTextOnPropertyChanges() function - so I can make things work the way I need - but would be great if I didn't need them and just use the Qt ones, if the signal worked as I thought it would)
It seems that what falls under formatting is loosely defined.
As you said, setItemFont() or setItemAlign() do not trigger the signal, but calling _document->setDocumentMargin(4.3); within your void MyTextItem::setItemAlign(Qt::Alignment a) method does.
Also, contentsChanged() is a signal emitted by the QTextDocument class, so I'm not sure if calling methods on the QGraphicsTextItem object that change its appearance classifies as modifying the formatting of the QTextDocument.
I am creating a Qt application and I added dynamic translation (I followed the example at http://www.qtcentre.org/wiki/index.php?title=Dynamic_translation_in_Qt4_applications) with a QCombobox which lists different languages. It works well but the problem is that I don't see how to translate dynamically the text in the dialog windows (for example YES and NO buttons).
In the main.cpp, before executing the app, I have :
QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
a.installTranslator(&qtTranslator);
which translate the dialog Windows in the user system language but I would like to do it dynamically like the rest of my app.
Here are the code of the example :
application.h :
#ifndef APPLICATION_H
#include <QApplication>
#include <QHash>
#include <QStringList>
class QDir;
class QTranslator;
typedef QHash<QString, QTranslator*> Translators;
class Application : public QApplication
{
Q_OBJECT
public:
explicit Application(int& argc, char* argv[]);
~Application();
static void loadTranslations(const QString& dir);
static void loadTranslations(const QDir& dir);
static const QStringList availableLanguages();
public slots:
static void setLanguage(const QString& locale);
private:
static QTranslator* current;
static Translators translators;
//static QTranslator* qtTranslator;//test to translate dialog windows
};
#endif // APPLICATION_H
application.cpp :
#include <QDir>
#include <QFileInfo>
#include <QTranslator>
#include <QLibraryInfo>
#include "application.h"
QTranslator* Application::current = 0;
//QTranslator* Application::qtTranslator = 0;//test to translate dialog windows
Translators Application::translators;
Application::Application(int& argc, char* argv[])
: QApplication(argc, argv)
{
}
Application::~Application()
{
}
void Application::loadTranslations(const QString& dir)
{
loadTranslations(QDir(dir));
QString locale = QLocale::system().name().section('_', 0, 0);
QString language=locale+ "_" + locale;
if(!QFile::exists(":Localization/Localization/"+language+".qm"))//if system language is not available, load english version
setLanguage("en_en");
else
setLanguage(language);
}
void Application::loadTranslations(const QDir& dir)
{
// <language>_<country>.qm
QString filter = "*_*.qm";
QDir::Filters filters = QDir::Files | QDir::Readable;
QDir::SortFlags sort = QDir::Name;
QFileInfoList entries = dir.entryInfoList(QStringList() << filter, filters, sort);
foreach (QFileInfo file, entries)
{
// pick country and language out of the file name
QStringList parts = file.baseName().split("_");
QString language = parts.at(parts.count() - 2);
QString country = parts.at(parts.count() - 1);
// construct and load translator
QTranslator* translator = new QTranslator(instance());
if (translator->load(file.absoluteFilePath()))
{
QString locale = language + "_" + country;
translators.insert(locale, translator);
}
}
}
const QStringList Application::availableLanguages()
{
// the content won't get copied thanks to implicit sharing and constness
return QStringList(translators.keys());
}
void Application::setLanguage(const QString& locale)
{
//test to translate dialog windows
/*
QTranslator qtTranslator;
QString qTLocale=locale.mid(0,2);
qtTranslator->load("qt_"+ qTLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
installTranslator(qtTranslator);
//*/
// remove previous
if (current)
{
removeTranslator(current);
}
// install new
current = translators.value(locale, 0);
if (current)
{
installTranslator(current);
}
}
I added the lines commented with "//test to translate dialog Windows" to try the dynamic translation of the dialog Windows but it doesn't work (no error at compilation but the application isn't launched with error message "the program stopped suddenly", I am on Qt Creator). Thanks!
So I finally got this to work after having the same problems. There are two things which were wrong in my case:
Name of the qt translation file:
QTranslator qtTranslator;
qtTranslator.load("qt_de"); // worked in older qt versions
qtTranslator.load("qtbase_de"); // works for qt5.2
a.installTranslator(&qtTranslator);
Have the correct parent for the QMessageBox. This is obvious after you think about it but pretty easy to miss.
QMessageBox::information(someChildOfMainWindow, ...);
For the latter, if you happen to be in a class which is a QObject but not a QWidget you can also use the following code to access your MainWindow from anywhere:
QMainWindow* mw = 0;
foreach(QWidget* widget, QApplication::topLevelWidgets()) {
if(widget->objectName() == "<your-main-window-class-name-here>") {
mw = qobject_cast<QMainWindow>(widget);
}
}
Ok Sébastian Lange, so finally I created the box and didn't use the static ones (
QMessageBox::question(..) for example)
QMessageBox quitMessageBox;
quitMessageBox.setWindowTitle(tr("Quit"));
quitMessageBox.setWindowIcon(QIcon("myIcon.jpg"));
quitMessageBox.setIcon(QMessageBox::Question);
quitMessageBox.setText(tr("Quit the application?"));
quitMessageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
quitMessageBox.setDefaultButton(QMessageBox::No);
quitMessageBox.button(QMessageBox::Yes)->setText(tr("Yes"));
quitMessageBox.button(QMessageBox::No)->setText(tr("No"));
And then
quitMessageBox.exec();
Like that it's ok. Thanks again!
When providing buttons for the dialog use
tr("Yes")
as for default dialogs, the created .ts-language file (to be edited via QtLinguist) should have default translations included.
The tr() marks the given argument to be translated. This concludes to if you do not know what will be written on a given label, you cannot translate it...
I'm creating a simple virtual keyboard in a QDockWidget...
When the widget is docked into the QMainWindow, the selected widget (for example a qdoublespinbox) is highlighted and if I click on the virtual keyboard clearFocus() works...
When the QDockWidget is floating above the window and I click a button, clearFocus doesn't work and I can't see the focused widget in QMainWindow...
How can I force the QDockWidget to not have any focus at all?
Thanks :-)
This is the code:
// class MyVirtualKeyboard : public QDockWidget
void MyVirtualKeyboard::sendKey(Qt::Key key, Qt::KeyboardModifier mod)
{
this->clearFocus();
QMainWindow *w = dynamic_cast<QMainWindow *>(this->parent());
if(w == NULL) return;
QWidget *widget = w->focusWidget();
QString repr = QKeySequence(key).toString();
QKeyEvent *pressEvent = new QKeyEvent(QEvent::KeyPress, key, mod, repr);
QKeyEvent *releaseEvent = new QKeyEvent(QEvent::KeyRelease, key, mod, repr);
qDebug("%s", pressEvent->text().toAscii().data());
MyApplication *app = MyApplication::myInstance();
app->postEvent(widget, pressEvent);
app->postEvent(widget, releaseEvent);
}
void MyVirtualKeyboard::on_BTN_1_clicked()
{
sendKey(Qt::Key_1);
}
...
The clearFocus() call should be unnecessary. Your dock widget and all of its widgets must have the Qt::NoFocus policy.
The code below shows how you might do it.
// https://github.com/KubaO/stackoverflown/tree/master/questions/vkb-focus-18558664
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
class Keyboard : public QDockWidget {
Q_OBJECT
QWidget m_widget;
QGridLayout m_layout{&m_widget};
QToolButton m_buttons[10];
void sendKey(Qt::Key key, Qt::KeyboardModifier mod)
{
if (! parentWidget()) return;
auto target = parentWidget()->focusWidget();
if (! target) return;
auto repr = QKeySequence(key).toString();
auto pressEvent = new QKeyEvent(QEvent::KeyPress, key, mod, repr);
auto releaseEvent = new QKeyEvent(QEvent::KeyRelease, key, mod, repr);
qApp->postEvent(target, pressEvent);
qApp->postEvent(target, releaseEvent);
qDebug() << repr;
}
Q_SLOT void clicked() {
auto key = sender()->property("key");
if (key.isValid()) sendKey((Qt::Key)key.toInt(), Qt::NoModifier);
}
public:
explicit Keyboard(const QString & title, QWidget *parent = nullptr) : Keyboard(parent) {
setWindowTitle(title);
}
explicit Keyboard(QWidget *parent = nullptr) : QDockWidget(parent) {
int i{};
for (auto & btn : m_buttons) {
btn.setText(QString::number(i));
btn.setProperty("key", Qt::Key_0 + i);
m_layout.addWidget(&btn, 0, i, 1, 1);
connect(&btn, SIGNAL(clicked()), SLOT(clicked()));
btn.setFocusPolicy(Qt::NoFocus);
++i;
}
setWidget(&m_widget);
setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
}
};
int main(int argc, char ** argv)
{
QApplication a(argc, argv);
QMainWindow w;
w.setCentralWidget(new QLineEdit);
w.addDockWidget(Qt::TopDockWidgetArea, new Keyboard("Keyboard", &w));
w.show();
return a.exec();
}
#include "main.moc"
You can prevent a widget from taking focus by setting QWidget::focusPolicy = Qt::NoFocus.
However, there are two concepts here that you're mixing - the focused control (per window), and the active window (per desktop). I think in the scenario you're describing (a torn-off popup window), the OS window manager is likely to still change the active top-level window even if Qt doesn't set a focused control. That will result in nobody having keyboard focus (which is a valid state!).
So I think a full answer to your question will involve some non-portable bits. I don't know what GUI environment you're working in, but I know some of the answer for Win32, so I'll keep going and hope that's useful:
Win32
There's a pretty good discussion of the state tracking for Win32 on MSDN in the article Win32 Activation and Focus. I'm not aware that Qt does anything to wrap this level, so you'd have to use QWidget::nativeEvent or QCoreApplication::installNativeEventFilter to get at the low-level event. If you can subclass the window, I'd prefer the former, since it's more self-contained.
bool FooWidget::nativeEvent(const QByteArray & eventType, void * message, long * result)
{
#ifdef Q_OS_WIN
if(eventType == "windows_generic_MSG") {
const MSG *msg = reinterpret_cast<MSG *>(message);
if(msg->message == WM_MOUSEACTIVATE) {
*result = MA_NOACTIVATE;
return true;
}
}
#else
#error Need platform-specific code to suppress click-activation
#endif
return false;
}
This should block the click from activating the window (MA_NOACTIVATE), and block Qt from processing it further (return true), while leaving other all events (including the the click, since we didn't use MA_NOACTIVATEANDEAT to block it too) to be processed into QEvents and Qt signals normally (return false at the end).
If you need further low-level access (though I don't think you will), see also QWidget::effectiveWinId() and QWidget::windowHandle
Thanks a lot to Martin Gräßlin for the answer!
My recommendation: check out the virtual keyboard code in KDE Plasma: http://quickgit.kde.org/?p=kdeplasma-addons.git&a=blob&h=5628d6325afe57f85917dad865a07d4116335726&hb=a658d1e257cfca2a43c12714d026ec26f1fdb755&f=applets%2Fplasmaboard%2Fwidget.cpp
Looks like the key is setWindowFlags(Qt::X11BypassWindowManagerHint) and setFocusPolicy(Qt::NoFocus)
MyVirtualKeyboard::MyVirtualKeyboard(QWidget *parent) :
QDockWidget(parent),
ui(new Ui::MyVirtualKeyboard)
{
ui->setupUi(this);
this->connect(this, SIGNAL(topLevelChanged(bool)), this, SLOT(topLevelChanged()));
}
void MyVirtualKeyboard::topLevelChanged()
{
if(this->isWindow())
{
this->setWindowFlags(Qt::Popup | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
this->setFocusPolicy(Qt::NoFocus);
this->show();
}
}
I think I've found a better way to do it!
Just use this->setAttribute(Qt::WA_X11DoNotAcceptFocus); and voila!
Example:
MyVirtualKeyboard::MyVirtualKeyboard(QWidget *parent) :
QDockWidget(parent),
ui(new Ui::MyVirtualKeyboard)
{
ui->setupUi(this);
this->setAttribute(Qt::WA_X11DoNotAcceptFocus);
}
I am using Ubuntu 12.04 and, while I can create a tray icon with a usable menu, I cannot control its actions:
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(QIcon(":/icons/Pictures/icon.png"));
trayIcon->setToolTip(QString("Hello there..."));
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(clickSysTrayIcon(QSystemTrayIcon::ActivationReason)));
connect(this,SIGNAL(minimized()),this,SLOT(hide()),Qt::QueuedConnection);
QMenu *changer_menu = new QMenu;
Show_action = new QAction(tr("S&how"),this);
Show_action->setIconVisibleInMenu(true);
connect(Show_action, SIGNAL(triggered()), this, SLOT(showClicked()));
changer_menu->addAction(Show_action);
changer_menu->addSeparator();
Quit_action = new QAction(tr("&Quit"), this);
Quit_action->setIconVisibleInMenu(true);;
connect(Quit_action, SIGNAL(triggered()), this, SLOT(close_minimize()));
changer_menu->addAction(Quit_action);
trayIcon->setContextMenu(changer_menu);
trayIcon->show();
The clickSysTrayIcon(QSystemTrayIcon::ActivationReason) is the following:
void MainWindow::clickSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
//reason is a variable that holds the type of activation or click done on the icon tray
qDebug() << "I'm in!";
}
and, defined at the header file as:
private Q_SLOTS:
void clickSysTrayIcon(QSystemTrayIcon::ActivationReason reason);
However, I cannot get the "I'm in!" message to be shown. I've tried to make it work with left/right clicks, with middle click and with mouse wheel, but I never see this message being outputed.
What is wrong?
EDIT: It seems that something's wrong with the specific system, Ubuntu 12.04, because it doesn't use tray icons any more and only indicators. So, there's a program which uses the tray icons and they convert them into indicators. But, then the features of indicators are gone. I know that it's the system to blame, because the same program, under the very same code, works perfectly under Lubuntu 12.04 with the LXDE desktop.
I blame Ubuntu for this. The sni-qt package doesn't do a very good migration from tray icons to indicators, providing that indicators can interact on click, on roller etc. It's a shame!
Any solutions to this problem?
My bounty ends, so if there's someone who can address the problem I would be thankful!
Bring up the issue to the people that have the most influence on the projects.
https://help.ubuntu.com/community/ReportingBugs#How_to_report_bugs
https://bugreports.qt.io/
Work-Around
I would make a floating frameless qwidget on top of the indicator area where your indicator gets painted, and then add the appropriate mouseEvent functions on to it.
Here is starting point for this style of work-around. I don't know how kosher this is, but it works pretty well in Windows. I know there are some UI tweaks and tools for Windows that use this style of layered elements, like DisplayFusion and TeamViewer. I haven't tested it in Ubuntu yet, but it should work the same way.
#include <QtGui/QWidget>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QMouseEvent>
#include <QPixmap>
#include <QAction>
#include <QDebug>
#include <QPaintEvent>
#include <QPainter>
#include <QApplication>
#include <QTimerEvent>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0)
: QWidget(parent)
{
// setup this widget to be borderless, transparent around the image
// and always on top
// and not to have a presence in the "visible window list"
this->setWindowFlags( Qt::WindowStaysOnTopHint |
Qt::FramelessWindowHint | Qt::Tool);
this->setAttribute(Qt::WA_TranslucentBackground);
// necessary if you want to track when you enter and leave the widget's rect with the mouse
this->setMouseTracking(true);
m_trayIcon = new QSystemTrayIcon(this);
m_trayIcon->setIcon(QIcon("icon1.ico"));
m_trayIcon->setToolTip(QString("Hello there..."));
m_changer_menu = new QMenu;
m_show_action = new QAction(tr("S&how"),this);
m_show_action->setIconVisibleInMenu(true);
connect(m_show_action, SIGNAL(triggered()), this, SLOT(showClicked()));
m_changer_menu->addAction(m_show_action);
m_changer_menu->addSeparator();
m_quit_action = new QAction(tr("&Quit"), this);
m_quit_action->setIconVisibleInMenu(true);;
connect(m_quit_action, SIGNAL(triggered()), this, SLOT(close_minimize()));
m_changer_menu->addAction(m_quit_action);
m_trayIcon->setContextMenu(m_changer_menu);
m_trayIcon->show();
QPixmap p("icon2.ico");
m_pix = p.scaled(QSize(m_trayIcon->geometry().width(),
m_trayIcon->geometry().height()),
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
this->move(m_trayIcon->geometry().x() ,m_trayIcon->geometry().y());
this->resize(m_trayIcon->geometry().width(), m_trayIcon->geometry().height());
// qDebug() << m_trayIcon->geometry();
// qDebug() << this->geometry();
// This assumes that the notification is stationary. If you want it to move
// with the tray icon underneath, you will need to subclass QSystemTrayIcon
// and track its move and resize and probably also its show and hide events
// raise itself 15x a second
this->startTimer(1000/15);
}
~Widget(){ }
public slots:
void mouseDoubleClickEvent(QMouseEvent *)
{
qDebug() << Q_FUNC_INFO;
}
void mouseReleaseEvent(QMouseEvent * me)
{
qDebug() << Q_FUNC_INFO;
switch(me->button())
{
case Qt::LeftButton:
qDebug() << "Left Click";
break;
case Qt::RightButton:
qDebug() << "Right Click";
m_changer_menu->popup(this->geometry().topLeft() + me->pos());
break;
default:
qDebug() << "other click";
break;
}
}
void showClicked()
{
qDebug() << Q_FUNC_INFO;
}
void close_minimize()
{
qDebug() << Q_FUNC_INFO;
qApp->exit();
}
void paintEvent(QPaintEvent *)
{
QPainter aPainter(this);
aPainter.drawPixmap(rect(), m_pix);
}
void timerEvent(QTimerEvent *)
{
if(!m_changer_menu->isVisible())
this->raise();
}
private:
QPixmap m_pix;
QSystemTrayIcon * m_trayIcon;
QMenu * m_changer_menu;
QAction * m_quit_action;
QAction * m_show_action;
};
and here is the main function...
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}