PDA

View Full Version : Minimized qwdiget always restore to maximized state in Mac



jiayang98@qq.com
4th November 2014, 08:21
A simple widget demo with system tray menu enabled.

Here I put 3 buttons in the widget: "max button", "min button" and "normal button"

Clicking "max button" calls 'showMaximized()'
Clicking "min button" calls 'showMinimized()'
Clicking "normal button" calls 'showNormal()'

All above 3 buttons works fine.

I also add systray menu "restore".

Clicking "restore" from systray icon will call 'shorNormal()'

The following is the steps to reproduce my bug.

#1. Run app

#2. Click 'max button' and the widget will scaled to maximum

#3. Click 'normal button' and the widget will scaled back to normal size

#4. Click 'min button' and the widget will minimized to Dock

#5. Click systray menu 'restore' and the widget will restored from the Dock. But at the same time, the restored widget is always be scaled to maximum. As expected, it should be restored to normal size.

I attached my project here, hope someone could give some hints.


#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);

connect(ui->maxButton, SIGNAL(clicked()), this, SLOT(onMax()));
connect(ui->minButton, SIGNAL(clicked()), this, SLOT(onMin()));
connect(ui->normalButton, SIGNAL(clicked()), this, SLOT(onNormal()));

createTrayIcon();
}

Widget::~Widget()
{
delete ui;
}

void Widget::onMax()
{
showMaximized();
}

void Widget::onMin()
{
showMinimized();
}

void Widget::onNormal()
{
showNormal();
}

void Widget::onRestore()
{
showNormal();
raise();
activateWindow();
}

void Widget::createTrayIcon()
{
restoreAction = new QAction(this);
restoreAction->setText("Restore");

connect(restoreAction, SIGNAL(triggered()), this, SLOT(onRestore()));

trayMenu = new QMenu(this);
trayMenu->addAction(restoreAction);


trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(QIcon("/Users/hynesg/offline.png"));
trayIcon->setContextMenu(trayMenu);

trayIcon->show();
}