In tried to solve your zoom window need and I encountered several problems
- A factor 5 seems a lot.
- You need to set a size for the ZoomWindow to have it painted correctly
- You dont need to get through SIGNAL/SLOT since mouseMoveEvent in parent widget is a virtual method.
I think your main problem is that when you grab the widget in the child window, you will grab all the parent, so with the child(the zoom) in it!! so you need to make it invisible and restore it after.
And ones I didnt solve :- the zoom is painted under the layout in background (=>maybe a window flag?),
- the mouseMoveEvent is not trigger on child widget of the layout (=>use eventFilter?)
Here is the parent widget :
#ifndef MYMAINWIDGET_H
#define MYMAINWIDGET_H
#include <QtGui/QDialog>
#include "ui_MyMainWidget.h"
#include <QMouseEvent>
#include "ZoomWindow.h"
class MyMainWidget
: public QDialog {
Q_OBJECT
public:
: QDialog(parent
), m_zoomWindow
(this) { ui.setupUi(this);
setMouseTracking(true);
}
virtual ~MyMainWidget() {
}
protected:
m_zoomWindow.update(event->pos());
}
private:
Ui::MyMainWidgetClass ui;
ZoomWindow m_zoomWindow;
};
#endif // MYMAINWIDGET_H
#ifndef MYMAINWIDGET_H
#define MYMAINWIDGET_H
#include <QtGui/QDialog>
#include "ui_MyMainWidget.h"
#include <QMouseEvent>
#include "ZoomWindow.h"
class MyMainWidget : public QDialog {
Q_OBJECT
public:
MyMainWidget(QWidget *parent = 0)
: QDialog(parent), m_zoomWindow(this) {
ui.setupUi(this);
setMouseTracking(true);
}
virtual ~MyMainWidget() {
}
protected:
void mouseMoveEvent(QMouseEvent * event){
QWidget::mouseMoveEvent(event);
m_zoomWindow.update(event->pos());
}
private:
Ui::MyMainWidgetClass ui;
ZoomWindow m_zoomWindow;
};
#endif // MYMAINWIDGET_H
To copy to clipboard, switch view to plain text mode
And here is the Zoom Window
#ifndef ZOOMWINDOW_H
#define ZOOMWINDOW_H
#include <QtGui/QWidget>
#include <QPixmap>
#include <QPainter>
class ZoomWindow
: public QWidget {
Q_OBJECT
public:
ZoomWindow
(QWidget *parent
= 0,
int factor
=2) :QWidget(parent
), m_factor
(factor
) { resize(200, 200);
}
virtual ~ZoomWindow() {
}
void update(const QPoint& point) {
setVisible(false);
m_pixmap
= QPixmap::grabWidget(parentWidget
(),
QRect(point, size
()/m_factor
));
setVisible(true);
move(point);
}
protected:
painter.setPen(Qt::black);
painter.drawPixmap(r, m_pixmap);
painter.drawRect(r);
}
private:
int m_factor;
};
#endif // ZOOMWINDOW_H
#ifndef ZOOMWINDOW_H
#define ZOOMWINDOW_H
#include <QtGui/QWidget>
#include <QPixmap>
#include <QPainter>
class ZoomWindow : public QWidget {
Q_OBJECT
public:
ZoomWindow(QWidget *parent = 0, int factor=2)
:QWidget(parent), m_factor(factor) {
resize(200, 200);
}
virtual ~ZoomWindow() {
}
void update(const QPoint& point) {
setVisible(false);
m_pixmap = QPixmap::grabWidget(parentWidget(), QRect(point, size()/m_factor));
setVisible(true);
move(point);
QWidget::update();
}
protected:
virtual void paintEvent(QPaintEvent * /*event*/) {
QRect r(rect());
QPainter painter(this);
painter.setPen(Qt::black);
painter.drawPixmap(r, m_pixmap);
painter.drawRect(r);
}
private:
int m_factor;
QPixmap m_pixmap;
};
#endif // ZOOMWINDOW_H
To copy to clipboard, switch view to plain text mode
Bookmarks