PDA

View Full Version : Getting Window Coordinates during QResizeEvent



jeffd589
8th February 2013, 19:52
I have a dialog class that opens a new window. When I drag the window around the viewing desktop, I want to report the new window geometry (X, Y, width and height) values. I have tried to implement, but get error during compilation that refers to syntax of "connect" statement arguments. Below are dialog source and header; not much to them, but can't seem to get it work.

Also, I can't tell if I've implement the QResizeEvent virtual function to catch the window resize event correctly. Can someone review this problem? I'm not too worried about getting the window position coordinates right now; just want to get the syntax and general design OK. I will continue looking through Qt documentation for answers.
Thanks. Jeff

dialog_xy_plot.cpp:


#include "dialog_xy_plot.h"
#include "ui_dialog_xy_plot.h"
#include "dialog_xy_plot_configure.h"

#include <QDesktopWidget>
#include <QResizeEvent>
#include <QDebug>

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

connect(this, resizeEvent(QResizeEvent* event), this, SLOT(get_current_window_position()));

QString my_title = "Window Position";
ui->groupBox->setTitle(my_title);
}

void Dialog_xy_plot::get_current_window_position()
{
// get window geometry (x,y, width, height)
qDebug() << "window geometry coorinates are ";
}

void Dialog_xy_plot::resizeEvent(QResizeEvent *event)
{
qDebug() << "in resize ";
QDialog::resizeEvent(event);
}

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

dialog_xy_plot.h:

#ifndef DIALOG_XY_PLOT_H
#define DIALOG_XY_PLOT_H

#include <QDialog>

namespace Ui {
class Dialog_xy_plot;
}

class Dialog_xy_plot : public QDialog
{
Q_OBJECT

public:
explicit Dialog_xy_plot(QWidget *parent = 0);
~Dialog_xy_plot();

private slots:
void on_buttonBox_layout_open_accepted();
void get_current_window_position();

private:
Ui::Dialog_xy_plot *ui;

protected:
void resizeEvent(QResizeEvent*);

};

#endif // DIALOG_XY_PLOT_H

Santosh Reddy
8th February 2013, 20:02
connect(this, resizeEvent(QResizeEvent* event), this, SLOT(get_current_window_position()));
Quick observations
1. SIGNAL() macro is missing in the send argument.
2. Second parameter must be a signal (declared under signal: section in the class declaration)
3. An event cannot to connected to a slot. (resizeEvent(), is function but not a signal), i.e only signals can be connected to slots.

You could do this, do not use connect at all and


void Dialog_xy_plot::resizeEvent(QResizeEvent *event)
{
qDebug() << "in resize ";
get_current_window_position();
QDialog::resizeEvent(event);
}

jeffd589
8th February 2013, 21:36
Hi Santosh - Thanks for your help.
So, if "get_current_window_position" is called in "resizeEvent", how should the slot arg in the connect call resemble?

connect(this, SIGNAL(resizeEvent(QResizeEvent* event)), this, SLOT(resizeEvent(QResizeEvent*)));

Also in "dialog.h", should "get_current_position()" function not be declared as a "private slot"?

private slots:
void on_buttonBox_layout_open_accepted();
void get_current_window_position();

Added after 14 minutes:

Hi Santosh -
Removed the "connect" command and works great when resizing the window. Thanks, again. What event can I treat simlarly to handle changing window position when the window is dragged around?
Thanks. Jeff

Santosh Reddy
8th February 2013, 22:49
Obviously move event
void QWidget::moveEvent ( QMoveEvent * event ) [virtual protected]