[Qt-interest] Windows that move together

John Weeks john at wavemetrics.com
Tue Aug 24 19:09:19 CEST 2010


On Aug 23, 2010, at 4:55 PM, 机械唯物主义 : linjunhalida wrote:

> post your code as attachment.

Thanks- It's still rough :) And it's part of an application that has  
gotten surprisingly large as I explore Qt. Consequently the code has  
some cruft in it that has to do with aspects that aren't that  
important here. Perhaps I will put together a small application that  
just demonstrates the problem I'm asking about and post that later.

On Windows the eventFilter gets called frequently while the filtered  
window is being dragged around; on Macintosh it gets called only when  
the dragging pauses briefly. Otherwise, it works pretty well.

int IgorQtWindowGroup::AddWidgetToGroup(QWidget * widget)
{
	groupedWidgets.push_back(widget);
	if (groupedWidgets.size() == 1)			// if you filter more than one  
widget, the moves become recursive and all heck breaks loose
	{
		IgorWindow * iw = qobject_cast<IgorWindow *>(widget);
		if (iw)
			iw->rootWidget()->installEventFilter(this);
		else
			widget->installEventFilter(this);
	}
	
	return 0;
}

bool IgorQtWindowGroup::eventFilter(QObject *obj, QEvent *event)
{
	switch (event->type())
	{
		case QEvent::Move:
			{
				int nWidgets = groupedWidgets.size();
				QMoveEvent * mevent = static_cast<QMoveEvent *>(event);
				QPoint delta = mevent->pos() - mevent->oldPos();
				
				for (int i = 0; i < nWidgets; ++i)
				{
					QWidget * thisWidget = groupedWidgets[i];
					IgorWindow * iw = qobject_cast<IgorWindow *>(thisWidget);
					QWidget * rw = iw->rootWidget();
					if (obj != rw)
					{
						rw->move(rw->pos()+delta);
					}
				}
			}
			return true;
			break;
		default:
			break;
	}
	
	return QObject::eventFilter(obj, event);		// we don't want to deal  
with this event- pass it along for regular processing
}


In a header file, an emulation of a Macintosh window group call:

inline int SetWindowGroup(IgorWindowRef inWindow, WindowGroupRef  
inNewGroup)
{
	return inNewGroup->AddWidgetToGroup(inWindow);
}


And a fragment from the place where the windows are created  
(IgorWindowRef is a pointer to a widget derived from QWidget, both  
have Qt::Window set, the second one is a frameless window):

					{
						Rect r;

						IgorWindowRef w1 = WMGetNewCWindow(GRAFID, NULL);
						QRect qr = w1->geometry();
						qr.setWidth(200);
						w1->setGeometry(qr);

						SetRect(&r, 5, 42, 200, 250);
						IgorWindowRef w = WMCreateNewWindow(PANEL_WIN_ID, &r,  
kFloatingWindowClass, kWindowCloseBoxAttribute, NULL);
						w->move(qr.x()+qr.width(), qr.y());
						
						WindowGroupRef wg;
						CreateWindowGroup(0, &wg);
						SetWindowGroup(w1, wg);
						SetWindowGroup(w, wg);
						
						w1->show();
						w->show();
					}


Regards, John Weeks





More information about the Qt-interest-old mailing list